Api.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. $actionname = $this->request->action();
  43. $noNeedLoginSet=is_string($this->noNeedLogin)?[$this->noNeedLogin]:$this->noNeedLogin;
  44. $noNeedLogin = in_array('*',$noNeedLoginSet) || in_array($actionname,$noNeedLoginSet);
  45. //需要登陆
  46. if(!$noNeedLogin && !ApiAuthService::getToken($token)){
  47. $response = Response::create(__('请先登录!'), 'html', 401);
  48. throw new HttpResponseException($response);
  49. }
  50. }
  51. }