Passport.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\store\controller;
  13. use app\store\model\store\User as StoreUserModel;
  14. use app\store\service\store\User as StoreUserService;
  15. /**
  16. * 商家后台认证
  17. * Class Passport
  18. * @package app\store\controller
  19. */
  20. class Passport extends Controller
  21. {
  22. /**
  23. * 强制验证当前访问的控制器方法method
  24. * 例: [ 'login' => 'POST' ]
  25. * @var array
  26. */
  27. protected $methodRules = [
  28. 'login' => 'POST',
  29. 'logout' => 'POST',
  30. ];
  31. /**
  32. * 商家用户登录
  33. * @return array|bool|string
  34. * @throws \think\db\exception\DataNotFoundException
  35. * @throws \think\db\exception\DbException
  36. * @throws \think\db\exception\ModelNotFoundException
  37. */
  38. public function login()
  39. {
  40. $model = new StoreUserModel;
  41. if (($userInfo = $model->login($this->postData())) === false) {
  42. return $this->renderError($model->getError() ?: '登录失败');
  43. }
  44. return $this->renderSuccess([
  45. 'userId' => (int)$userInfo['store_user_id'],
  46. 'token' => $model->getToken()
  47. ], '登录成功');
  48. }
  49. /**
  50. * 退出登录
  51. * @return array
  52. */
  53. public function logout()
  54. {
  55. // 清空登录状态
  56. StoreUserService::logout();
  57. return $this->renderSuccess('操作成功');
  58. }
  59. }