Passport.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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\api\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->login($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. /**
  52. * 退出登录
  53. * @return \think\response\View
  54. */
  55. public function logout()
  56. {
  57. Session::clear();
  58. return view('logIn');
  59. }
  60. public function retrievePassword()
  61. {
  62. if ($this->request->method() =='GET'){
  63. return view('retrievePassword');
  64. }
  65. $params = $this->postForm();
  66. if (empty($params['mobile']) || empty($params['smsCode'])){
  67. if ($params['password'] !== $params['password1']){
  68. return $this->renderError('password not match');
  69. }
  70. }
  71. $LoginService = new LoginService;
  72. $r = $LoginService->resetPassword($params['mobile'],$params['smsCode'],$params['password']);
  73. if ($r){
  74. return $this->renderSuccess([], 'Reset success');
  75. }
  76. return $this->renderError('something wrong');
  77. }
  78. }