tp6混合easyswoole DLL 多应用模式 修改版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
1.9 KiB

5 years ago
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\common\controller;
  4. use think\App;
  5. use think\Validate;
  6. /**
  7. * 控制器基础类
  8. */
  9. abstract class BaseController
  10. {
  11. /**
  12. * Request实例
  13. * @var \think\Request
  14. */
  15. protected $request;
  16. /**
  17. * 应用实例
  18. * @var \think\App
  19. */
  20. protected $app;
  21. /**
  22. * 是否批量验证
  23. * @var bool
  24. */
  25. protected $batchValidate = false;
  26. /**
  27. * 控制器中间件
  28. * @var array
  29. */
  30. protected $middleware = [];
  31. /**
  32. * 构造方法
  33. * @access public
  34. * @param App $app 应用对象
  35. */
  36. public function __construct(App $app)
  37. {
  38. $this->app = $app;
  39. $this->request = $this->app->request;
  40. // 控制器初始化
  41. $this->initialize();
  42. }
  43. // 初始化
  44. protected function initialize()
  45. {}
  46. /**
  47. * 验证数据
  48. * @access protected
  49. * @param array $data 数据
  50. * @param string|array $validate 验证器名或者验证规则数组
  51. * @param array $message 提示信息
  52. * @param bool $batch 是否批量验证
  53. * @return array|string|true
  54. */
  55. protected function validate(array $data, $validate, array $message = [], bool $batch = false)
  56. {
  57. if (is_array($validate)) {
  58. $v = new Validate();
  59. $v->rule($validate);
  60. } else {
  61. if (strpos($validate, '.')) {
  62. // 支持场景
  63. [$validate, $scene] = explode('.', $validate);
  64. }
  65. $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
  66. $v = new $class();
  67. if (!empty($scene)) {
  68. $v->scene($scene);
  69. }
  70. }
  71. $v->message($message);
  72. // 是否批量验证
  73. if ($batch || $this->batchValidate) {
  74. $v->batch(true);
  75. }
  76. return $v->failException(true)->check($data);
  77. }
  78. }