BaseController.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\index;
  4. use app\index\exception\ApiException;
  5. use app\common\until\Token;
  6. use think\App;
  7. use think\exception\ValidateException;
  8. use think\Validate;
  9. /**
  10. * 控制器基础类
  11. */
  12. abstract class BaseController
  13. {
  14. /**
  15. * Request实例
  16. * @var \think\Request
  17. */
  18. protected $request;
  19. /**
  20. * 应用实例
  21. * @var \think\App
  22. */
  23. protected $app;
  24. /**
  25. * 是否批量验证
  26. * @var bool
  27. */
  28. protected $batchValidate = false;
  29. /**
  30. * 控制器中间件
  31. * @var array
  32. */
  33. protected $middleware = [];
  34. public $userId = 0;
  35. /**
  36. * 构造方法
  37. * @access public
  38. * @param App $app 应用对象
  39. */
  40. public function __construct(App $app)
  41. {
  42. $this->app = $app;
  43. $this->request = $this->app->request;
  44. $this->request['Accept'] = 'application/json';
  45. $route = [
  46. '/index/Token/getToken',
  47. '/index/index',
  48. '/index/user/login',
  49. '/index/admin/login',
  50. '/index/setting/read',
  51. ''
  52. ];
  53. if (!in_array($this->request->baseUrl(),$route)){
  54. if (empty($this->request->header('token'))) {
  55. throw new ApiException('token不为空');
  56. }else{
  57. $token = new Token();
  58. $decodeToken = $token->decodeToken();
  59. $this->userId = $decodeToken['userId'];
  60. }
  61. // $token = new Token();
  62. // $decodeToken = $token->decodeToken();
  63. // $this->userId = $decodeToken['userId'];
  64. }
  65. // 控制器初始化
  66. $this->initialize();
  67. }
  68. // 初始化
  69. protected function initialize()
  70. {}
  71. protected function isAdmin() {
  72. if (request()->header('flag') === 'admin'){
  73. return true;
  74. }
  75. return false;
  76. }
  77. }