Controller.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace app;
  11. use think\App;
  12. use think\facade\View;
  13. use think\exception\HttpException;
  14. use think\exception\HttpResponseException;
  15. use think\exception\ValidateException;
  16. use think\Response;
  17. use think\Validate;
  18. use liliuwei\think\Jump;
  19. use think\facade\Route;
  20. use think\facade\Config;
  21. use think\facade\Env;
  22. /**
  23. * 控制器基础类
  24. */
  25. abstract class Controller
  26. {
  27. use Jump;
  28. /**
  29. * Request实例
  30. * @var \think\Request
  31. */
  32. protected $request;
  33. /**
  34. * 应用实例
  35. * @var \think\App
  36. */
  37. protected $app;
  38. /**
  39. * 是否批量验证
  40. * @var bool
  41. */
  42. protected $batchValidate = false;
  43. /**
  44. * 控制器中间件
  45. * @var array
  46. */
  47. protected $middleware = [];
  48. /**
  49. * 构造方法
  50. * @access public
  51. * @param App $app 应用对象
  52. */
  53. public function __construct()
  54. {
  55. // 控制器初始化
  56. $this->initialize();
  57. }
  58. // 初始化
  59. protected function initialize()
  60. {
  61. }
  62. /**
  63. * 加载模板输出
  64. * @access protected
  65. * @param string $template 模板文件名
  66. * @param array $vars 模板输出变量
  67. * @param array $config 模板参数
  68. * @return mixed
  69. */
  70. protected function fetch($template = '', $vars = [], $config = [])
  71. {
  72. if (!empty($config)) {
  73. $config_view = Config::get('view');
  74. $config_view['tpl_replace_string'] = array_merge($config_view['tpl_replace_string'], $config);
  75. Config::set($config_view, 'view');
  76. }
  77. return View::fetch($template, $vars);
  78. }
  79. /**
  80. * 渲染内容输出
  81. * @access protected
  82. * @param string $content 模板内容
  83. * @param array $vars 模板输出变量
  84. * @param array $config 模板参数
  85. * @return mixed
  86. */
  87. protected function display($content = '', $vars = [], $config = [])
  88. {
  89. return View::display($content, $vars, $config);
  90. }
  91. /**
  92. * 模板变量赋值
  93. * @access protected
  94. * @param mixed $name 要显示的模板变量
  95. * @param mixed $value 变量的值
  96. * @return $this
  97. */
  98. protected function assign($name, $value = '')
  99. {
  100. View::assign($name, $value);
  101. return $this;
  102. }
  103. /**
  104. * 操作成功跳转的快捷方法
  105. * @access protected
  106. * @param mixed $msg 提示信息
  107. * @param string $url 跳转的URL地址
  108. * @param mixed $data 返回的数据
  109. * @param integer $wait 跳转等待时间
  110. * @param array $header 发送的Header信息
  111. * @return void
  112. */
  113. protected function success($msg = '', string $url = null, $data = '', int $wait = 3, array $header = [])
  114. {
  115. if (is_null($url) && isset($_SERVER["HTTP_REFERER"])) {
  116. $url = $_SERVER["HTTP_REFERER"];
  117. } elseif ($url) {
  118. $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : $this->app->route->buildUrl($url);
  119. }
  120. $result = [
  121. 'code' => 1,
  122. 'msg' => $msg,
  123. 'data' => $data,
  124. 'url' => $url,
  125. 'wait' => $wait,
  126. ];
  127. $type = 'html';
  128. // 把跳转模板的渲染下沉,这样在 response_send 行为里通过getData()获得的数据是一致性的格式
  129. if ('html' == strtolower($type)) {
  130. $type = 'view';
  131. $response = Response::create(config('jump.dispatch_success_tmpl'), $type)->assign($result)->header($header);
  132. } else {
  133. $response = Response::create($result, $type)->header($header);
  134. }
  135. throw new HttpResponseException($response);
  136. }
  137. /**
  138. * 操作错误跳转的快捷方法
  139. * @access protected
  140. * @param mixed $msg 提示信息
  141. * @param string $url 跳转的URL地址
  142. * @param mixed $data 返回的数据
  143. * @param integer $wait 跳转等待时间
  144. * @param array $header 发送的Header信息
  145. * @return void
  146. */
  147. protected function error($msg = '', string $url = null, $data = '', int $wait = 3, array $header = [])
  148. {
  149. if (is_null($url)) {
  150. $url = 'javascript:history.back(-1);';
  151. } elseif ($url) {
  152. $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : $this->app->route->buildUrl($url);
  153. }
  154. $result = [
  155. 'code' => 0,
  156. 'msg' => $msg,
  157. 'data' => $data,
  158. 'url' => $url,
  159. 'wait' => $wait,
  160. ];
  161. $type = 'html';
  162. if ('html' == strtolower($type)) {
  163. $type = 'view';
  164. $response = Response::create(config('jump.dispatch_error_tmpl'), $type)->assign($result)->header($header);
  165. } else {
  166. $response = Response::create($result, $type)->header($header);
  167. }
  168. throw new HttpResponseException($response);
  169. }
  170. /**
  171. * 返回封装后的API数据到客户端
  172. * @access protected
  173. * @param mixed $data 要返回的数据
  174. * @param integer $code 返回的code
  175. * @param mixed $msg 提示信息
  176. * @param string $type 返回数据格式
  177. * @param array $header 发送的Header信息
  178. * @return void
  179. */
  180. protected function result($data, $code = 0, $msg = '', $type = '', array $header = [])
  181. {
  182. $result = [
  183. 'code' => $code,
  184. 'msg' => $msg,
  185. 'time' => time(),
  186. 'data' => $data,
  187. ];
  188. $type = 'html';
  189. $response = Response::create($result, $type)->header($header);
  190. throw new HttpResponseException($response);
  191. }
  192. /**
  193. * URL重定向
  194. * @access protected
  195. * @param string $url 跳转的URL表达式
  196. * @param integer $code http code
  197. * @param array $with 隐式传参
  198. * @return void
  199. */
  200. protected function redirect($url, $code = 302, $with = [])
  201. {
  202. $response = Response::create($url, 'redirect');
  203. $response->code($code)->with($with);
  204. throw new HttpResponseException($response);
  205. }
  206. /**
  207. * @param array $data 验证数据
  208. * @param $validate 验证类
  209. * @param $scene 验证场景
  210. */
  211. public function validate(array $data, $validate, $scene = '')
  212. {
  213. try{
  214. $class = new $validate;
  215. if(!empty($scene))
  216. {
  217. $res = $class->scene($scene)->check($data);
  218. }else{
  219. $res = $class->check($data);
  220. }
  221. if(!$res){
  222. return error(-1, $class->getError());
  223. }else
  224. return success(1);
  225. } catch (\Exception $e)
  226. {
  227. return error(-1, $e->getMessage());
  228. }
  229. }
  230. }