Api.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * ----------------------------------------------------------------------------
  4. * 行到水穷处,坐看云起时
  5. * 开发软件,找贵阳云起信息科技,官网地址:https://www.56q7.com/
  6. * ----------------------------------------------------------------------------
  7. * Author: 老成
  8. * email:85556713@qq.com
  9. */
  10. declare(strict_types=1);
  11. namespace app\api\controller;
  12. use app\api\service\ApiAuthService;
  13. use app\common\controller\BaseController;
  14. use think\exception\HttpResponseException;
  15. use think\facade\Config;
  16. use think\facade\Cookie;
  17. use think\Response;
  18. class Api extends BaseController
  19. {
  20. /**
  21. * 当前登录用户
  22. * @var \app\api\service\ApiAuthService
  23. */
  24. protected $auth;
  25. /**
  26. * 无需登录的方法,同时也就不需要鉴权了
  27. * @var array
  28. */
  29. protected $noNeedLogin = [];
  30. protected function _initialize()
  31. {
  32. $token=request()->header('token');
  33. if(!$token){
  34. $token=Cookie::get('token');
  35. }
  36. if(!$token){
  37. $token=request()->get('token');
  38. }
  39. if(!$token){
  40. $token=request()->post('token');
  41. }
  42. //$this->auth=ApiAuthService::newInstance(['adapter'=>new $class($token)]);
  43. $actionname = $this->request->action();
  44. $noNeedLoginSet=is_string($this->noNeedLogin)?[$this->noNeedLogin]:$this->noNeedLogin;
  45. $noNeedLogin = in_array('*',$noNeedLoginSet) || in_array($actionname,$noNeedLoginSet);
  46. //需要登陆
  47. // if(!$noNeedLogin && !$this->auth->isLogin()){
  48. // $response = Response::create(__('请先登录!'), 'html', 401);
  49. // throw new HttpResponseException($response);
  50. // }
  51. }
  52. }