BaseController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // |
  4. // +----------------------------------------------------------------------
  5. // |
  6. // +----------------------------------------------------------------------
  7. // |
  8. // +----------------------------------------------------------------------
  9. // |
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 控制器基础类
  13. // +----------------------------------------------------------------------
  14. declare (strict_types = 1);
  15. namespace app\common\controller;
  16. use app\common\library\traits\Jump;
  17. use app\common\middleware\ActionBegin;
  18. use think\App;
  19. use think\exception\ValidateException;
  20. use think\facade\View;
  21. use think\Validate;
  22. abstract class BaseController
  23. {
  24. use Jump;
  25. /**
  26. * Request实例
  27. * @var \think\Request
  28. */
  29. protected $request;
  30. /**
  31. * 应用实例
  32. * @var \think\App
  33. */
  34. protected $app;
  35. /**
  36. * 视图实例
  37. * @var \think\View
  38. */
  39. protected $view;
  40. /**
  41. * @var bool 验证失败是否抛出异常
  42. */
  43. protected $failException = true;
  44. /**
  45. * 是否批量验证
  46. * @var bool
  47. */
  48. protected $batchValidate = false;
  49. /**
  50. * 控制器中间件
  51. * @var array
  52. */
  53. protected $middleware = [ActionBegin::class];
  54. /**
  55. * 构造方法
  56. * @access public
  57. * @param App $app 应用对象
  58. */
  59. public function __construct(App $app)
  60. {
  61. $this->app = $app;
  62. $this->request = $this->app->request;
  63. $this->view = $this->app->view;
  64. // 控制器初始化
  65. $this->initialize();
  66. }
  67. // 初始化
  68. protected function initialize()
  69. {}
  70. protected function fetch(string $template = '', array $vars = [])
  71. {
  72. return View::fetch($template, $vars);
  73. }
  74. protected function assign(...$vars)
  75. {
  76. View::assign(...$vars);
  77. }
  78. /**
  79. * 设置验证失败后是否抛出异常
  80. * @access protected
  81. * @param bool $fail 是否抛出异常
  82. * @return $this
  83. */
  84. protected function validateFailException($fail = true)
  85. {
  86. $this->failException = $fail;
  87. return $this;
  88. }
  89. /**
  90. * 验证数据
  91. * @access protected
  92. * @param array $data 数据
  93. * @param string|array $validate 验证器名或者验证规则数组
  94. * @param array $message 提示信息
  95. * @param bool $batch 是否批量验证
  96. * @return array|string|true
  97. * @throws ValidateException
  98. */
  99. protected function validate(array $data, string | array $validate, array $message = [], bool $batch = false)
  100. {
  101. if (is_array($validate)) {
  102. $v = new Validate();
  103. $v->rule($validate);
  104. } else {
  105. if (strpos($validate, '.')) {
  106. // 支持场景
  107. [$validate, $scene] = explode('.', $validate);
  108. }
  109. $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
  110. $v = new $class();
  111. if (!empty($scene)) {
  112. $v->scene($scene);
  113. }
  114. }
  115. $v->message($message);
  116. // 是否批量验证
  117. if ($batch || $this->batchValidate) {
  118. $v->batch(true);
  119. }
  120. return $v->failException($this->failException)->check($data);
  121. }
  122. }