Passport.php 3.4 KB

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