BaseController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app;
  13. use think\App;
  14. use think\exception\HttpResponseException;
  15. use think\exception\ValidateException;
  16. use think\Validate;
  17. use think\response\Redirect;
  18. use HttpEncodingException;
  19. use think\Response;
  20. /**
  21. * 控制器基础类
  22. */
  23. abstract class BaseController
  24. {
  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 bool
  38. */
  39. protected $batchValidate = false;
  40. /**
  41. * 控制器中间件
  42. * @var array
  43. */
  44. protected $middleware = [];
  45. /**
  46. * 构造方法
  47. * @access public
  48. * @param App $app 应用对象
  49. */
  50. public function __construct(App $app)
  51. {
  52. $this->app = $app;
  53. $this->request = $this->app->request;
  54. // 控制器初始化
  55. $this->initialize();
  56. }
  57. // 初始化
  58. protected function initialize()
  59. {
  60. }
  61. /**
  62. * 验证数据
  63. * @access protected
  64. * @param array $data 数据
  65. * @param string|array $validate 验证器名或者验证规则数组
  66. * @param array $message 提示信息
  67. * @param bool $batch 是否批量验证
  68. * @return array|string|true
  69. * @throws ValidateException
  70. */
  71. protected function validate(array $data, $validate, array $message = [], bool $batch = false)
  72. {
  73. if (is_array($validate)) {
  74. $v = new Validate();
  75. $v->rule($validate);
  76. } else {
  77. if (strpos($validate, '.')) {
  78. // 支持场景
  79. list($validate, $scene) = explode('.', $validate);
  80. }
  81. $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
  82. $v = new $class();
  83. if (!empty($scene)) {
  84. $v->scene($scene);
  85. }
  86. }
  87. $v->message($message);
  88. // 是否批量验证
  89. if ($batch || $this->batchValidate) {
  90. $v->batch(true);
  91. }
  92. return $v->failException(true)->check($data);
  93. }
  94. /**
  95. * 操作成功跳转的快捷方法
  96. * @access protected
  97. * @param mixed $msg 提示信息
  98. * @param string $url 跳转的URL地址
  99. * @param mixed $data 返回的数据
  100. * @param integer $wait 跳转等待时间
  101. * @param array $header 发送的Header信息
  102. * @return void
  103. */
  104. protected function success($msg = '', string $url = null, $data = '', int $wait = 3, array $header = [])
  105. {
  106. if (is_null($url) && isset($_SERVER["HTTP_REFERER"])) {
  107. $url = $_SERVER["HTTP_REFERER"];
  108. } elseif ($url) {
  109. $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : app('route')->buildUrl($url);
  110. }
  111. $result = [
  112. 'code' => 1,
  113. 'msg' => $msg,
  114. 'data' => $data,
  115. 'url' => $url,
  116. 'wait' => $wait,
  117. ];
  118. $type = $this->getResponseType();
  119. if ($type == 'html'){
  120. $response = view($this->app->config->get('app.dispatch_success_tmpl'), $result);
  121. } else if ($type == 'json') {
  122. $response = json($result);
  123. }
  124. throw new HttpResponseException($response);
  125. }
  126. /**
  127. * 操作错误跳转的快捷方法
  128. * @access protected
  129. * @param mixed $msg 提示信息
  130. * @param string $url 跳转的URL地址
  131. * @param mixed $data 返回的数据
  132. * @param integer $wait 跳转等待时间
  133. * @param array $header 发送的Header信息
  134. * @return void
  135. */
  136. protected function error($msg = '', string $url = null, $data = '', int $wait = 3, array $header = [])
  137. {
  138. if (is_null($url)) {
  139. // $url = $this->request->isAjax() ? '' : 'javascript:history.back(-1);';
  140. $url = $this->request->isAjax() ? '' : 'javascript:history.go(index)';
  141. } elseif ($url) {
  142. $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : $this->app->route->buildUrl($url);
  143. }
  144. $result = [
  145. 'code' => 0,
  146. 'msg' => $msg,
  147. 'data' => $data,
  148. 'url' => $url,
  149. 'wait' => $wait,
  150. ];
  151. $type = $this->getResponseType();
  152. if ($type == 'html'){
  153. $response = view($this->app->config->get('app.dispatch_error_tmpl'), $result);
  154. } else if ($type == 'json') {
  155. $response = json($result);
  156. }
  157. throw new HttpResponseException($response);
  158. }
  159. /**
  160. * URL重定向 自带重定向无效
  161. * @access protected
  162. * @param string $url 跳转的URL表达式
  163. * @param array|integer $params 其它URL参数
  164. * @param integer $code http code
  165. * @param array $with 隐式传参
  166. * @return void
  167. */
  168. protected function redirect($url, $params = [], $code = 302, $with = [])
  169. {
  170. $response = Response::create($url, 'redirect');
  171. if (is_integer($params)) {
  172. $code = $params;
  173. $params = [];
  174. }
  175. $response->code($code)->with($params);
  176. throw new HttpResponseException($response);
  177. }
  178. /**
  179. * 获取当前的response 输出类型
  180. * @access protected
  181. * @return string
  182. */
  183. protected function getResponseType()
  184. {
  185. return $this->request->isJson() || $this->request->isAjax() ? 'json' : 'html';
  186. }
  187. }