Passport.php 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. 'user_type' => (int)$userInfo['user_type'],
  67. 'token' => $LoginService->getToken((int)$userInfo['user_id'])
  68. ], '登录成功');
  69. }
  70. /**
  71. * 快捷登录: 微信小程序授权手机号登录
  72. * @return array|\think\response\Json
  73. * @throws \app\common\exception\BaseException
  74. * @throws \think\Exception
  75. * @throws \think\db\exception\DataNotFoundException
  76. * @throws \think\db\exception\DbException
  77. * @throws \think\db\exception\ModelNotFoundException
  78. */
  79. public function loginMpWxMobile()
  80. {
  81. // 微信小程序一键登录
  82. $LoginService = new LoginService;
  83. if (!$LoginService->loginMpWxMobile($this->postForm())) {
  84. return $this->renderError($LoginService->getError());
  85. }
  86. // 获取登录成功后的用户信息
  87. $userInfo = $LoginService->getUserInfo();
  88. return $this->renderSuccess([
  89. 'userId' => (int)$userInfo['user_id'],
  90. 'token' => $LoginService->getToken((int)$userInfo['user_id'])
  91. ], '登录成功');
  92. }
  93. }