BaseController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace cores;
  13. use think\App;
  14. use think\Validate;
  15. use think\response\Json;
  16. use think\exception\ValidateException;
  17. /**
  18. * 控制器基础类
  19. */
  20. abstract class BaseController
  21. {
  22. /**
  23. * Request实例
  24. * @var Request
  25. */
  26. protected $request;
  27. /**
  28. * 应用实例
  29. * @var App
  30. */
  31. protected $app;
  32. /**
  33. * 是否批量验证
  34. * @var bool
  35. */
  36. protected $batchValidate = false;
  37. /**
  38. * 控制器中间件
  39. * @var array
  40. */
  41. protected $middleware = [];
  42. /**
  43. * 构造方法
  44. * BaseController constructor.
  45. * @param App $app
  46. */
  47. public function __construct(App $app)
  48. {
  49. $this->app = $app;
  50. $this->request = $this->app->request;
  51. // 控制器初始化
  52. $this->initialize();
  53. }
  54. // 初始化
  55. protected function initialize()
  56. {
  57. }
  58. /**
  59. * 验证数据
  60. * @access protected
  61. * @param array $data 数据
  62. * @param string|array $validate 验证器名或者验证规则数组
  63. * @param array $message 提示信息
  64. * @param bool $batch 是否批量验证
  65. * @return array|string|true
  66. * @throws ValidateException
  67. */
  68. protected function validate(array $data, $validate, array $message = [], bool $batch = false)
  69. {
  70. if (is_array($validate)) {
  71. $v = new Validate();
  72. $v->rule($validate);
  73. } else {
  74. if (strpos($validate, '.')) {
  75. // 支持场景
  76. [$validate, $scene] = explode('.', $validate);
  77. }
  78. $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
  79. $v = new $class();
  80. if (!empty($scene)) {
  81. $v->scene($scene);
  82. }
  83. }
  84. $v->message($message);
  85. // 是否批量验证
  86. if ($batch || $this->batchValidate) {
  87. $v->batch(true);
  88. }
  89. return $v->failException(true)->check($data);
  90. }
  91. /**
  92. * 返回封装后的 API 数据到客户端
  93. * @param int|null $status
  94. * @param string $message
  95. * @param array $data
  96. * @return Json
  97. */
  98. protected final function renderJson(int $status = null, string $message = '', array $data = []): Json
  99. {
  100. return json(compact('status', 'message', 'data'));
  101. }
  102. /**
  103. * 返回操作成功json
  104. * @param array|string $data
  105. * @param string $message
  106. * @return Json
  107. */
  108. protected final function renderSuccess($data = [], string $message = 'success'): Json
  109. {
  110. if (is_string($data)) {
  111. $message = $data;
  112. $data = [];
  113. }
  114. return $this->renderJson(config('status.success'), $message, $data);
  115. }
  116. /**
  117. * 返回操作失败json
  118. * @param string $message
  119. * @param array $data
  120. * @return Json
  121. */
  122. protected final function renderError(string $message = 'error', array $data = []): Json
  123. {
  124. return $this->renderJson(config('status.error'), $message, $data);
  125. }
  126. /**
  127. * 获取post数据 (数组)
  128. * @param null $key
  129. * @param bool $filter
  130. * @return mixed
  131. */
  132. protected final function postData($key = null, bool $filter = false)
  133. {
  134. return $this->request->post(empty($key) ? '' : "{$key}/a", null, $filter ? '' : null);
  135. }
  136. /**
  137. * 获取post数据 (数组)
  138. * @param string|null $key
  139. * @param bool $filter
  140. * @return mixed
  141. */
  142. protected final function postForm(?string $key = 'form', bool $filter = true)
  143. {
  144. return $this->postData(empty($key) ? 'form' : $key, $filter);
  145. }
  146. }