Passport.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. if ($this->request->method() =='GET'){
  35. return view('logIn');
  36. }
  37. // 执行登录
  38. $LoginService = new LoginService;
  39. if (!$LoginService->login($this->postForm())) {
  40. return $this->renderError($LoginService->getError());
  41. }
  42. // 用户信息
  43. $userInfo = $LoginService->getUserInfo();
  44. return $this->renderSuccess([
  45. 'userId' => (int)$userInfo['user_id'],
  46. 'token' => $LoginService->getToken((int)$userInfo['user_id'])
  47. ], '登录成功');
  48. }
  49. /**
  50. * 微信小程序快捷登录 (需提交wx.login接口返回的code、微信用户公开信息)
  51. * 业务流程:判断openid是否存在 -> 存在: 更新用户登录信息 -> 返回userId和token
  52. * -> 不存在: 返回false, 跳转到注册页面
  53. * @return array|\think\response\Json
  54. * @throws \app\common\exception\BaseException
  55. * @throws \think\Exception
  56. * @throws \think\db\exception\DataNotFoundException
  57. * @throws \think\db\exception\DbException
  58. * @throws \think\db\exception\ModelNotFoundException
  59. */
  60. public function loginMpWx()
  61. {
  62. // 微信小程序一键登录
  63. $LoginService = new LoginService;
  64. if (!$LoginService->loginMpWx($this->postForm())) {
  65. return $this->renderError($LoginService->getError());
  66. }
  67. // 获取登录成功后的用户信息
  68. $userInfo = $LoginService->getUserInfo();
  69. return $this->renderSuccess([
  70. 'userId' => (int)$userInfo['user_id'],
  71. 'token' => $LoginService->getToken((int)$userInfo['user_id'])
  72. ], '登录成功');
  73. }
  74. /**
  75. * 快捷登录: 微信小程序授权手机号登录
  76. * @return array|\think\response\Json
  77. * @throws \app\common\exception\BaseException
  78. * @throws \think\Exception
  79. * @throws \think\db\exception\DataNotFoundException
  80. * @throws \think\db\exception\DbException
  81. * @throws \think\db\exception\ModelNotFoundException
  82. */
  83. public function loginMpWxMobile()
  84. {
  85. // 微信小程序一键登录
  86. $LoginService = new LoginService;
  87. if (!$LoginService->loginMpWxMobile($this->postForm())) {
  88. return $this->renderError($LoginService->getError());
  89. }
  90. // 获取登录成功后的用户信息
  91. $userInfo = $LoginService->getUserInfo();
  92. return $this->renderSuccess([
  93. 'userId' => (int)$userInfo['user_id'],
  94. 'token' => $LoginService->getToken((int)$userInfo['user_id'])
  95. ], '登录成功');
  96. }
  97. }