Passport.php 3.9 KB

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