Passport.php 4.4 KB

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