Passport.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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\Cookie;
  15. use think\facade\Session;
  16. use think\response\Redirect;
  17. /**
  18. * 用户认证模块
  19. * Class Passport
  20. * @package app\api\controller
  21. */
  22. class Passport extends Controller
  23. {
  24. /**
  25. * 登录接口 (需提交手机号、短信验证码、第三方用户信息)
  26. * @return \think\response\Json|\think\response\View
  27. * @throws \app\common\exception\BaseException
  28. * @throws \think\Exception
  29. * @throws \think\db\exception\DataNotFoundException
  30. * @throws \think\db\exception\DbException
  31. * @throws \think\db\exception\ModelNotFoundException
  32. */
  33. public function login()
  34. {
  35. if ($this->request->method() =='GET'){
  36. return view('logIn');
  37. }
  38. // 执行登录
  39. $LoginService = new LoginService;
  40. if (!$LoginService->login($this->postForm())) {
  41. return $this->renderError($LoginService->getError());
  42. }
  43. // 用户信息
  44. $userInfo = $LoginService->getUserInfo();
  45. $token = $LoginService->getToken((int)$userInfo['user_id']);
  46. Session::set('access_token',$token);
  47. Session::set('user_id',$userInfo['user_id']);
  48. // $indexContr = new Index($this->app);
  49. // return $indexContr->index();
  50. // $url = url('index/index/index');
  51. return $this->renderSuccess([
  52. 'userId' => (int)$userInfo['user_id'],
  53. 'token' => $LoginService->getToken((int)$userInfo['user_id'])
  54. ], '登录成功');
  55. }
  56. }