Passport.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 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 think\response\Json;
  14. use app\store\model\store\User as StoreUserModel;
  15. use app\store\service\store\User as StoreUserService;
  16. /**
  17. * 商家后台认证
  18. * Class Passport
  19. * @package app\store\controller
  20. */
  21. class Passport extends Controller
  22. {
  23. /**
  24. * 强制验证当前访问的控制器方法method
  25. * 例: [ 'login' => 'POST' ]
  26. * @var array
  27. */
  28. protected array $methodRules = [
  29. 'login' => 'POST',
  30. 'logout' => 'POST',
  31. ];
  32. /**
  33. * 商家用户登录
  34. * @return Json
  35. * @throws \think\db\exception\DataNotFoundException
  36. * @throws \think\db\exception\DbException
  37. * @throws \think\db\exception\ModelNotFoundException
  38. */
  39. public function login(): Json
  40. {
  41. $model = new StoreUserModel;
  42. if (($userInfo = $model->login($this->postData())) === false) {
  43. return $this->renderError($model->getError() ?: '登录失败');
  44. }
  45. return $this->renderSuccess([
  46. 'userId' => (int)$userInfo['store_user_id'],
  47. 'token' => $model->getToken()
  48. ], '登录成功');
  49. }
  50. /**
  51. * 退出登录
  52. * @return Json
  53. */
  54. public function logout(): Json
  55. {
  56. // 清空登录状态
  57. StoreUserService::logout();
  58. return $this->renderSuccess('操作成功');
  59. }
  60. }