BaseController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\index;
  4. use app\api\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. /**
  72. * 验证数据
  73. * @access protected
  74. * @param array $data 数据
  75. * @param string|array $validate 验证器名或者验证规则数组
  76. * @param array $message 提示信息
  77. * @param bool $batch 是否批量验证
  78. * @return array|string|true
  79. * @throws ValidateException
  80. */
  81. protected function validate(array $data, $validate, array $message = [], bool $batch = false)
  82. {
  83. if (is_array($validate)) {
  84. $v = new Validate();
  85. $v->rule($validate);
  86. } else {
  87. if (strpos($validate, '.')) {
  88. // 支持场景
  89. [$validate, $scene] = explode('.', $validate);
  90. }
  91. $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
  92. $v = new $class();
  93. if (!empty($scene)) {
  94. $v->scene($scene);
  95. }
  96. }
  97. $v->message($message);
  98. // 是否批量验证
  99. if ($batch || $this->batchValidate) {
  100. $v->batch(true);
  101. }
  102. return $v->failException(true)->check($data);
  103. }
  104. protected function isAdmin() {
  105. if (request()->header('flag') === 'admin'){
  106. return true;
  107. }
  108. return false;
  109. }
  110. }