Passport.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.thinkphp.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: thinkphp <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. $params = $this->postForm()? $this->postForm() : $this->request->param();
  60. if (!$LoginService->loginMpWx($params)) {
  61. return $this->renderError($LoginService->getError());
  62. }
  63. // 获取登录成功后的用户信息
  64. $userInfo = $LoginService->getUserInfo();
  65. return $this->renderSuccess([
  66. 'userId' => (int)$userInfo['user_id'],
  67. 'user_type' => (int)$userInfo['user_type'],
  68. 'token' => $LoginService->getToken((int)$userInfo['user_id'])
  69. ], '登录成功');
  70. }
  71. /**
  72. * 快捷登录: 微信小程序授权手机号登录
  73. * @return array|\think\response\Json
  74. * @throws \app\common\exception\BaseException
  75. * @throws \think\Exception
  76. * @throws \think\db\exception\DataNotFoundException
  77. * @throws \think\db\exception\DbException
  78. * @throws \think\db\exception\ModelNotFoundException
  79. */
  80. public function loginMpWxMobile()
  81. {
  82. // 微信小程序一键登录
  83. $LoginService = new LoginService;
  84. if (!$LoginService->loginMpWxMobile($this->postForm())) {
  85. return $this->renderError($LoginService->getError());
  86. }
  87. // 获取登录成功后的用户信息
  88. $userInfo = $LoginService->getUserInfo();
  89. return $this->renderSuccess([
  90. 'userId' => (int)$userInfo['user_id'],
  91. 'token' => $LoginService->getToken((int)$userInfo['user_id'])
  92. ], '登录成功');
  93. }
  94. }