Passport.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. UserAccessLog::doSave([
  40. 'user_id' => 0,
  41. 'path' => $this->request->action(),
  42. 'remark' => '',
  43. 'ip' => $this->request->ip(),
  44. 'store_id' => $this->getStoreId(),
  45. 'create_time' => time()
  46. ]);
  47. $userId = UserService::getCurrentLoginUserId();
  48. if ($userId){
  49. return redirect('/index/index/index.html');
  50. }
  51. return view('logIn');
  52. }
  53. // 执行登录
  54. $LoginService = new LoginService;
  55. if (!$LoginService->toLogin($this->postForm())) {
  56. return $this->renderError($LoginService->getError());
  57. }
  58. // 用户信息
  59. $userInfo = $LoginService->getUserInfo();
  60. $token = $LoginService->getToken((int)$userInfo['user_id']);
  61. Session::set('access_token', $token);
  62. Session::set('user_id', $userInfo['user_id']);
  63. $returnUri = Session::pull('returnuri');
  64. return $this->renderSuccess([
  65. 'userId' => (int)$userInfo['user_id'],
  66. 'token' => $LoginService->getToken((int)$userInfo['user_id']),
  67. 'returnUri' => $returnUri ?? ''
  68. ], '登录成功');
  69. }
  70. public function register()
  71. {
  72. $form = $this->postForm();
  73. UserAccessLog::doSave([
  74. 'user_id' => 0,
  75. 'path' => $this->request->action(),
  76. 'remark' => json_encode($form),
  77. 'ip' => $this->request->ip(),
  78. 'store_id' => $this->getStoreId(),
  79. 'create_time' => time()
  80. ]);
  81. // 执行登录
  82. $LoginService = new LoginService;
  83. if (!$LoginService->toRegister($form)) {
  84. return $this->renderError($LoginService->getError());
  85. }
  86. if (!empty($form['mobile'])) {
  87. $userInfo = $LoginService->getUserInfo();
  88. UserModel::setIncPoints(intval($userInfo['user_id']), UserAlias::POINTS_FOR_REGISTER, 'Register .');
  89. }
  90. return $this->renderSuccess([], 'Registration is successful');
  91. }
  92. /**
  93. * 退出登录
  94. * @return \think\response\Redirect
  95. */
  96. public function logout()
  97. {
  98. Session::clear();
  99. return redirect('/index/passport/login.html');
  100. //return view('logIn');
  101. }
  102. public function retrievePassword()
  103. {
  104. if ($this->request->method() == 'GET') {
  105. return view('retrievePassword');
  106. }
  107. $params = $this->postForm();
  108. if (empty($params['mobile']) || empty($params['smsCode'])) {
  109. if ($params['password'] !== $params['password1']) {
  110. return $this->renderError('password not match');
  111. }
  112. }
  113. $LoginService = new LoginService;
  114. $r = $LoginService->resetPassword($params['mobile'], $params['smsCode'], $params['password']);
  115. if ($r) {
  116. return $this->renderSuccess([], 'Reset success');
  117. }
  118. return $this->renderError('something wrong');
  119. }
  120. }