Passport.php 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. /**
  15. * 用户认证模块
  16. * Class Passport
  17. * @package app\api\controller
  18. */
  19. class Passport extends Controller
  20. {
  21. /**
  22. * 登录接口 (需提交手机号、短信验证码、第三方用户信息)
  23. * @return array|\think\response\Json
  24. * @throws \app\common\exception\BaseException
  25. * @throws \think\Exception
  26. * @throws \think\db\exception\DataNotFoundException
  27. * @throws \think\db\exception\DbException
  28. * @throws \think\db\exception\ModelNotFoundException
  29. */
  30. public function login()
  31. {
  32. // 执行登录
  33. $LoginService = new LoginService;
  34. if (!$LoginService->login($this->postForm())) {
  35. return $this->renderError($LoginService->getError());
  36. }
  37. // 用户信息
  38. $userInfo = $LoginService->getUserInfo();
  39. return $this->renderSuccess([
  40. 'userId' => (int)$userInfo['user_id'],
  41. 'token' => $LoginService->getToken((int)$userInfo['user_id'])
  42. ], '登录成功');
  43. }
  44. /**
  45. * 微信小程序快捷登录 (需提交wx.login接口返回的code、微信用户公开信息)
  46. * 业务流程:判断openid是否存在 -> 存在: 更新用户登录信息 -> 返回userId和token
  47. * -> 不存在: 返回false, 跳转到注册页面
  48. * @return array|\think\response\Json
  49. * @throws \app\common\exception\BaseException
  50. * @throws \think\Exception
  51. * @throws \think\db\exception\DataNotFoundException
  52. * @throws \think\db\exception\DbException
  53. * @throws \think\db\exception\ModelNotFoundException
  54. */
  55. public function loginMpWx()
  56. {
  57. // 微信小程序一键登录
  58. $LoginService = new LoginService;
  59. if (!$LoginService->loginMpWx($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. * @throws \app\common\exception\BaseException
  73. * @throws \think\Exception
  74. * @throws \think\db\exception\DataNotFoundException
  75. * @throws \think\db\exception\DbException
  76. * @throws \think\db\exception\ModelNotFoundException
  77. */
  78. public function loginMpWxMobile()
  79. {
  80. // 微信小程序一键登录
  81. $LoginService = new LoginService;
  82. if (!$LoginService->loginMpWxMobile($this->postForm())) {
  83. return $this->renderError($LoginService->getError());
  84. }
  85. // 获取登录成功后的用户信息
  86. $userInfo = $LoginService->getUserInfo();
  87. return $this->renderSuccess([
  88. 'userId' => (int)$userInfo['user_id'],
  89. 'token' => $LoginService->getToken((int)$userInfo['user_id'])
  90. ], '登录成功');
  91. }
  92. }