Passport.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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\api\controller;
  13. use app\api\service\passport\Login as LoginService;
  14. use app\api\service\passport\Captcha as CaptchaService;
  15. use app\api\service\passport\SmsCaptcha as SmsCaptchaService;
  16. /**
  17. * 用户认证模块
  18. * Class Passport
  19. * @package app\api\controller
  20. */
  21. class Passport extends Controller
  22. {
  23. /**
  24. * 登录接口
  25. * @return array|\think\response\Json
  26. * @throws \app\common\exception\BaseException
  27. * @throws \think\db\exception\DataNotFoundException
  28. * @throws \think\db\exception\DbException
  29. * @throws \think\db\exception\ModelNotFoundException
  30. */
  31. public function login()
  32. {
  33. // 执行登录
  34. $LoginService = new LoginService;
  35. if (!$LoginService->login($this->postForm())) {
  36. return $this->renderError($LoginService->getError());
  37. }
  38. // 用户信息
  39. $userInfo = $LoginService->getUserInfo();
  40. return $this->renderSuccess([
  41. 'userId' => (int)$userInfo['user_id'],
  42. 'token' => $LoginService->getToken((int)$userInfo['user_id'])
  43. ], '登录成功');
  44. }
  45. /**
  46. * 图形验证码
  47. * @return array|\think\response\Json
  48. */
  49. public function captcha()
  50. {
  51. $CaptchaService = new CaptchaService;
  52. return $this->renderSuccess($CaptchaService->create());
  53. }
  54. /**
  55. * 发送短信验证码
  56. * @return array|\think\response\Json
  57. */
  58. public function sendSmsCaptcha()
  59. {
  60. $SmsCaptchaService = new SmsCaptchaService;
  61. if (!$SmsCaptchaService->sendSmsCaptcha($this->postForm())) {
  62. return $this->renderError($SmsCaptchaService->getError());
  63. }
  64. return $this->renderSuccess('发送成功,请注意查收');
  65. }
  66. }