Passport.php 3.3 KB

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