Passport.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\api\controller;
  13. use app\api\service\passport\MailCaptcha;
  14. use think\Exception;
  15. use think\response\Json;
  16. use think\db\exception\DataNotFoundException;
  17. use think\db\exception\DbException;
  18. use think\db\exception\ModelNotFoundException;
  19. use app\api\service\passport\Login as LoginService;
  20. use cores\exception\BaseException;
  21. /**
  22. * 用户认证模块
  23. * Class Passport
  24. * @package app\api\controller
  25. */
  26. class Passport extends Controller
  27. {
  28. /**
  29. * 登录接口 (需提交手机号、短信验证码、第三方用户信息)
  30. * @return Json
  31. * @throws BaseException
  32. * @throws \think\Exception
  33. * @throws \think\db\exception\DataNotFoundException
  34. * @throws \think\db\exception\DbException
  35. * @throws \think\db\exception\ModelNotFoundException
  36. */
  37. public function login(): Json
  38. {
  39. // $a = new MailCaptcha();
  40. // $a->sendCaptcha('541469799@qq.com');
  41. // 执行登录
  42. $LoginService = new LoginService;
  43. if (!$LoginService->login($this->postForm())) {
  44. return $this->renderError($LoginService->getError());
  45. }
  46. // 用户信息
  47. $userInfo = $LoginService->getUserInfo();
  48. return $this->renderSuccess([
  49. 'userId' => (int)$userInfo['user_id'],
  50. 'token' => $LoginService->getToken((int)$userInfo['user_id'])
  51. ], '登录成功');
  52. }
  53. /**
  54. * 微信小程序快捷登录 (需提交wx.login接口返回的code、微信用户公开信息)
  55. * 业务流程:判断openid是否存在 -> 存在: 更新用户登录信息 -> 返回userId和token
  56. * -> 不存在: 返回false, 跳转到注册页面
  57. * @return Json
  58. * @throws BaseException
  59. * @throws \think\Exception
  60. * @throws \think\db\exception\DataNotFoundException
  61. * @throws \think\db\exception\DbException
  62. * @throws \think\db\exception\ModelNotFoundException
  63. */
  64. public function loginMpWx(): Json
  65. {
  66. // 微信小程序一键登录
  67. $LoginService = new LoginService;
  68. if (!$LoginService->loginMpWx($this->postForm())) {
  69. return $this->renderError($LoginService->getError());
  70. }
  71. // 获取登录成功后的用户信息
  72. $userInfo = $LoginService->getUserInfo();
  73. return $this->renderSuccess([
  74. 'userId' => (int)$userInfo['user_id'],
  75. 'token' => $LoginService->getToken((int)$userInfo['user_id'])
  76. ], '登录成功');
  77. }
  78. /**
  79. * 快捷登录: 微信小程序授权手机号登录
  80. * @return Json
  81. * @throws BaseException
  82. * @throws \think\Exception
  83. * @throws \think\db\exception\DataNotFoundException
  84. * @throws \think\db\exception\DbException
  85. * @throws \think\db\exception\ModelNotFoundException
  86. */
  87. public function loginMpWxMobile(): Json
  88. {
  89. // 微信小程序一键登录
  90. $LoginService = new LoginService;
  91. if (!$LoginService->loginMpWxMobile($this->postForm())) {
  92. return $this->renderError($LoginService->getError());
  93. }
  94. // 获取登录成功后的用户信息
  95. $userInfo = $LoginService->getUserInfo();
  96. return $this->renderSuccess([
  97. 'userId' => (int)$userInfo['user_id'],
  98. 'token' => $LoginService->getToken((int)$userInfo['user_id'])
  99. ], '登录成功');
  100. }
  101. /**
  102. * 是否需要填写昵称头像 (微信小程序端)
  103. * @param string $code
  104. * @return Json
  105. * @throws BaseException
  106. * @throws Exception
  107. * @throws DataNotFoundException
  108. * @throws DbException
  109. * @throws ModelNotFoundException
  110. */
  111. public function isPersonalMpweixin(string $code): Json
  112. {
  113. $LoginService = new LoginService;
  114. $isPersonalMpweixin = $LoginService->isPersonalMpweixin($code);
  115. return $this->renderSuccess(compact('isPersonalMpweixin'));
  116. }
  117. }