BaseController.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. Session::start();
  46. // $this->request['Accept'] = 'application/json';
  47. $route = [
  48. '/index/Token/getToken',
  49. '/index/index',
  50. '/index/user/login',
  51. '/index/admin/login',
  52. '/index/setting/read',
  53. '/index/user/clearSession'
  54. ];
  55. if (!in_array($this->request->baseUrl(),$route)){
  56. if (empty($this->request->header('token'))) {
  57. throw new ApiException('token不为空');
  58. }else{
  59. $token = new Token();
  60. $decodeToken = $token->decodeToken();
  61. $this->userId = $decodeToken['userId'];
  62. }
  63. // $token = new Token();
  64. // $decodeToken = $token->decodeToken();
  65. // $this->userId = $decodeToken['userId'];
  66. }
  67. // 控制器初始化
  68. $this->initialize();
  69. }
  70. // 初始化
  71. protected function initialize()
  72. {}
  73. protected function isAdmin() {
  74. if (request()->header('flag') === 'admin'){
  75. return true;
  76. }
  77. return false;
  78. }
  79. }