Passport.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. * 微信小程序快捷登录 (需提交wx.login接口返回的code、微信用户公开信息)
  47. * 实现流程:判断openid是否存在 -> 存在: 更新用户登录信息 -> 返回userId和token
  48. * -> 不存在: 返回false, 跳转到注册页面
  49. */
  50. public function mpWxLogin()
  51. {
  52. // 执行登录
  53. $LoginService = new LoginService;
  54. if (!$LoginService->mpWxLogin($this->postForm())) {
  55. return $this->renderError($LoginService->getError());
  56. }
  57. // 用户信息
  58. $userInfo = $LoginService->getUserInfo();
  59. return $this->renderSuccess([
  60. 'userId' => (int)$userInfo['user_id'],
  61. 'token' => $LoginService->getToken((int)$userInfo['user_id'])
  62. ], '登录成功');
  63. }
  64. /**
  65. * 图形验证码
  66. * @return array|\think\response\Json
  67. */
  68. public function captcha()
  69. {
  70. $CaptchaService = new CaptchaService;
  71. return $this->renderSuccess($CaptchaService->create());
  72. }
  73. /**
  74. * 发送短信验证码
  75. * @return array|\think\response\Json
  76. */
  77. public function sendSmsCaptcha()
  78. {
  79. $SmsCaptchaService = new SmsCaptchaService;
  80. if (!$SmsCaptchaService->sendSmsCaptcha($this->postForm())) {
  81. return $this->renderError($SmsCaptchaService->getError());
  82. }
  83. return $this->renderSuccess('发送成功,请注意查收');
  84. }
  85. }