Passport.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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\index\controller;
  13. use app\common\model\User as UserAlias;
  14. use app\index\model\User as UserModel;
  15. use app\index\model\user\UserAccessLog;
  16. use app\index\service\User as UserService;
  17. use think\facade\Cache;
  18. use app\index\service\passport\Login as LoginService;
  19. use think\facade\Session;
  20. /**
  21. * 用户认证模块
  22. * Class Passport
  23. * @package app\api\controller
  24. */
  25. class Passport extends Controller
  26. {
  27. /**
  28. * 登录接口 (需提交手机号、短信验证码、第三方用户信息)
  29. * @return
  30. * @throws \app\common\exception\BaseException
  31. * @throws \think\Exception
  32. * @throws \think\db\exception\DataNotFoundException
  33. * @throws \think\db\exception\DbException
  34. * @throws \think\db\exception\ModelNotFoundException
  35. */
  36. public function login()
  37. {
  38. if ($this->request->method() == 'GET') {
  39. $userId = UserService::getCurrentLoginUserId();
  40. if ($userId){
  41. return redirect('/index/index/index.html#goodsPosition');
  42. }
  43. return view('logIn');
  44. }
  45. // 执行登录
  46. $LoginService = new LoginService;
  47. if (!$LoginService->toLogin($this->postForm())) {
  48. return $this->renderError($LoginService->getError());
  49. }
  50. // 用户信息
  51. $userInfo = $LoginService->getUserInfo();
  52. $token = $LoginService->getToken((int)$userInfo['user_id']);
  53. Session::set('access_token', $token);
  54. Session::set('user_id', $userInfo['user_id']);
  55. $returnUri = Session::pull('returnuri');
  56. return $this->renderSuccess([
  57. 'userId' => (int)$userInfo['user_id'],
  58. 'token' => $LoginService->getToken((int)$userInfo['user_id']),
  59. 'returnUri' => $returnUri ?? ''
  60. ], '登录成功');
  61. }
  62. public function register()
  63. {
  64. $form = $this->postForm();
  65. // 执行登录
  66. $LoginService = new LoginService;
  67. if (!$LoginService->toRegister($form)) {
  68. return $this->renderError($LoginService->getError());
  69. }
  70. if (!empty($form['mobile'])) {
  71. $userInfo = $LoginService->getUserInfo();
  72. UserModel::setIncPoints(intval($userInfo['user_id']), UserAlias::POINTS_FOR_REGISTER, 'Register .');
  73. }
  74. return $this->renderSuccess([], 'Registration is successful');
  75. }
  76. /**
  77. * 退出登录
  78. * @return \think\response\Redirect
  79. */
  80. public function logout()
  81. {
  82. Session::clear();
  83. return redirect('/index/passport/login.html');
  84. //return view('logIn');
  85. }
  86. public function retrievePassword()
  87. {
  88. if ($this->request->method() == 'GET') {
  89. return view('retrievePassword');
  90. }
  91. $params = $this->postForm();
  92. if (empty($params['mobile']) || empty($params['smsCode'])) {
  93. if ($params['password'] !== $params['password1']) {
  94. return $this->renderError('password not match');
  95. }
  96. }
  97. $LoginService = new LoginService;
  98. $r = $LoginService->resetPassword($params['mobile'], $params['smsCode'], $params['password']);
  99. if ($r) {
  100. return $this->renderSuccess([], 'Reset success');
  101. }
  102. return $this->renderError('something wrong');
  103. }
  104. public function authGgUri()
  105. {
  106. return redirect('/index/passport/login.html');
  107. }
  108. }