BaseController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. declare(strict_types=1);
  3. namespace app;
  4. use think\App;
  5. use think\exception\ValidateException;
  6. use think\Validate;
  7. use think\facade\View;
  8. use think\exception\HttpResponseException;
  9. use think\Response;
  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. /**
  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. // 控制器初始化
  45. $this->initialize();
  46. }
  47. // 初始化
  48. protected function initialize()
  49. {
  50. }
  51. /**
  52. * 验证数据
  53. * @access protected
  54. * @param array $data 数据
  55. * @param string|array $validate 验证器名或者验证规则数组
  56. * @param array $message 提示信息
  57. * @param bool $batch 是否批量验证
  58. * @return array|string|true
  59. * @throws ValidateException
  60. */
  61. protected function validate(array $data, $validate, array $message = [], bool $batch = false)
  62. {
  63. if (is_array($validate)) {
  64. $v = new Validate();
  65. $v->rule($validate);
  66. } else {
  67. if (strpos($validate, '.')) {
  68. // 支持场景
  69. [$validate, $scene] = explode('.', $validate);
  70. }
  71. $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
  72. $v = new $class();
  73. if (!empty($scene)) {
  74. $v->scene($scene);
  75. }
  76. }
  77. $v->message($message);
  78. // 是否批量验证
  79. if ($batch || $this->batchValidate) {
  80. $v->batch(true);
  81. }
  82. return $v->failException(true)->check($data);
  83. }
  84. /**
  85. * 操作成功跳转的快捷方法
  86. * @access protected
  87. * @param mixed $msg 提示信息
  88. * @param string $url 跳转的URL地址
  89. * @param mixed $data 返回的数据
  90. * @param integer $wait 跳转等待时间
  91. * @param array $header 发送的Header信息
  92. * @return void
  93. */
  94. protected function success($msg = '', string $url = null, $data = '', int $wait = 3, array $header = [])
  95. {
  96. if (is_null($url) && isset($_SERVER["HTTP_REFERER"])) {
  97. $url = $_SERVER["HTTP_REFERER"];
  98. } elseif ($url) {
  99. $url = (string)$url;
  100. $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : (string)$this->app->route->buildUrl($url);
  101. }
  102. $result = [
  103. 'code' => 1,
  104. 'msg' => $msg,
  105. 'data' => $data,
  106. 'url' => $url,
  107. 'wait' => $wait,
  108. ];
  109. $type = $this->getResponseType();
  110. // 把跳转模板的渲染下沉,这样在 response_send 行为里通过getData()获得的数据是一致性的格式
  111. if ('html' == strtolower($type)) {
  112. $type = 'view';
  113. $response = Response::create($this->app->config->get('jump.dispatch_success_tmpl'), $type)->assign($result)->header($header);
  114. } else {
  115. $response = Response::create($result, $type)->header($header);
  116. }
  117. throw new HttpResponseException($response);
  118. }
  119. /**
  120. * 操作错误跳转的快捷方法
  121. * @access protected
  122. * @param mixed $msg 提示信息
  123. * @param string $url 跳转的URL地址
  124. * @param mixed $data 返回的数据
  125. * @param integer $wait 跳转等待时间
  126. * @param array $header 发送的Header信息
  127. * @return void
  128. */
  129. protected function error($msg = '', string $url = null, $data = '', int $wait = 3, array $header = [])
  130. {
  131. if (is_null($url)) {
  132. $url = $this->request->isAjax() ? '' : 'javascript:history.back(-1);';
  133. } elseif ($url) {
  134. $url = (string)$url;
  135. $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : (string)$this->app->route->buildUrl($url);
  136. }
  137. $result = [
  138. 'code' => 0,
  139. 'msg' => $msg,
  140. 'data' => $data,
  141. 'url' => $url,
  142. 'wait' => $wait,
  143. ];
  144. $type = $this->getResponseType();
  145. if ('html' == strtolower($type)) {
  146. $type = 'view';
  147. $response = Response::create($this->app->config->get('jump.dispatch_error_tmpl'), $type)->assign($result)->header($header);
  148. } else {
  149. $response = Response::create($result, $type)->header($header);
  150. }
  151. throw new HttpResponseException($response);
  152. }
  153. /**
  154. * URL重定向
  155. * @access protected
  156. * @param string $url 跳转的URL表达式
  157. * @param integer $code http code
  158. * @param array $with 隐式传参
  159. * @return void
  160. */
  161. protected function redirect($url, $params = [], $code = 302, $with = [])
  162. {
  163. $url = (string)$url;
  164. $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : (string)$this->app->route->buildUrl($url, $params);
  165. $response = Response::create($url, 'redirect');
  166. $response->code($code)->with($with);
  167. throw new HttpResponseException($response);
  168. }
  169. /**
  170. * 获取当前的 response 输出类型
  171. * @access protected
  172. * @return string
  173. */
  174. protected function getResponseType()
  175. {
  176. return request()->isAjax()
  177. ? 'json'
  178. : 'html';
  179. }
  180. }