Passport.php 4.4 KB

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