Passport.php 3.7 KB

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