Passport.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. * @return array|\think\response\Json
  50. * @throws \app\common\exception\BaseException
  51. * @throws \think\db\exception\DataNotFoundException
  52. * @throws \think\db\exception\DbException
  53. * @throws \think\db\exception\ModelNotFoundException
  54. */
  55. public function mpWxLogin()
  56. {
  57. // 微信小程序一键登录
  58. $LoginService = new LoginService;
  59. if (!$LoginService->mpWxLogin($this->postForm())) {
  60. return $this->renderError($LoginService->getError());
  61. }
  62. // 获取登录成功后的用户信息
  63. $userInfo = $LoginService->getUserInfo();
  64. return $this->renderSuccess([
  65. 'userId' => (int)$userInfo['user_id'],
  66. 'token' => $LoginService->getToken((int)$userInfo['user_id'])
  67. ], '登录成功');
  68. }
  69. /**
  70. * 图形验证码
  71. * @return array|\think\response\Json
  72. */
  73. public function captcha()
  74. {
  75. $CaptchaService = new CaptchaService;
  76. return $this->renderSuccess($CaptchaService->create());
  77. }
  78. /**
  79. * 发送短信验证码
  80. * @return array|\think\response\Json
  81. */
  82. public function sendSmsCaptcha()
  83. {
  84. $SmsCaptchaService = new SmsCaptchaService;
  85. if (!$SmsCaptchaService->sendSmsCaptcha($this->postForm())) {
  86. return $this->renderError($SmsCaptchaService->getError());
  87. }
  88. return $this->renderSuccess('发送成功,请注意查收');
  89. }
  90. }