BaseController.php 1.9 KB

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