Passport.php 3.9 KB

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