Controller.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <[email protected]>
  10. // +----------------------------------------------------------------------
  11. namespace think;
  12. use think\exception\ValidateException;
  13. use traits\controller\Jump;
  14. use think\Log;
  15. Loader::import('controller/Jump', TRAIT_PATH, EXT);
  16. class Controller
  17. {
  18. use Jump;
  19. /**
  20. * @var \think\View 视图类实例
  21. */
  22. protected $view;
  23. /**
  24. * @var \think\Request Request 实例
  25. */
  26. protected $request;
  27. /**
  28. * @var bool 验证失败是否抛出异常
  29. */
  30. protected $failException = false;
  31. /**
  32. * @var bool 是否批量验证
  33. */
  34. protected $batchValidate = false;
  35. /**
  36. * @var array 前置操作方法列表
  37. */
  38. protected $beforeActionList = [];
  39. /**
  40. * 构造方法
  41. * @access public
  42. * @param Request $request Request 对象
  43. */
  44. public function __construct(Request $request = null)
  45. {
  46. $this->view = View::instance(Config::get('template'), Config::get('view_replace_str'));
  47. $this->request = is_null($request) ? Request::instance() : $request;
  48. // 控制器初始化
  49. $this->_initialize();
  50. // 前置操作方法
  51. if ($this->beforeActionList) {
  52. foreach ($this->beforeActionList as $method => $options) {
  53. is_numeric($method) ?
  54. $this->beforeAction($options) :
  55. $this->beforeAction($method, $options);
  56. }
  57. }
  58. }
  59. /**
  60. * 初始化操作
  61. * @access protected
  62. */
  63. protected function _initialize()
  64. {
  65. }
  66. /**
  67. * 前置操作
  68. * @access protected
  69. * @param string $method 前置操作方法名
  70. * @param array $options 调用参数 ['only'=>[...]] 或者 ['except'=>[...]]
  71. * @return void
  72. */
  73. protected function beforeAction($method, $options = [])
  74. {
  75. if (isset($options['only'])) {
  76. if (is_string($options['only'])) {
  77. $options['only'] = explode(',', $options['only']);
  78. }
  79. if (!in_array($this->request->action(), $options['only'])) {
  80. return;
  81. }
  82. } elseif (isset($options['except'])) {
  83. if (is_string($options['except'])) {
  84. $options['except'] = explode(',', $options['except']);
  85. }
  86. if (in_array($this->request->action(), $options['except'])) {
  87. return;
  88. }
  89. }
  90. call_user_func([$this, $method]);
  91. }
  92. /**
  93. * 加载模板输出
  94. * @access protected
  95. * @param string $template 模板文件名
  96. * @param array $vars 模板输出变量
  97. * @param array $replace 模板替换
  98. * @param array $config 模板参数
  99. * @return mixed
  100. */
  101. protected function fetch($template = '', $vars = [], $replace = [], $config = [])
  102. {
  103. // 如果是新版本
  104. if($GLOBALS['config']['site']['new_version'] == 1 || !isset($GLOBALS['config']['site']['new_version']) || empty($GLOBALS['config']['site']['new_version'])){
  105. // 如果模板路径以admin@开头
  106. if (strpos($template, 'admin@') === 0) {
  107. $parts = explode('@', $template);
  108. $result = $parts[1];
  109. $template = APP_PATH . request()->module() . '/view_new/' . $result . '.html';
  110. }
  111. }
  112. return $this->view->fetch($template, $vars, $replace, $config);
  113. }
  114. /**
  115. * 渲染内容输出
  116. * @access protected
  117. * @param string $content 模板内容
  118. * @param array $vars 模板输出变量
  119. * @param array $replace 替换内容
  120. * @param array $config 模板参数
  121. * @return mixed
  122. */
  123. protected function display($content = '', $vars = [], $replace = [], $config = [])
  124. {
  125. return $this->view->display($content, $vars, $replace, $config);
  126. }
  127. /**
  128. * 模板变量赋值
  129. * @access protected
  130. * @param mixed $name 要显示的模板变量
  131. * @param mixed $value 变量的值
  132. * @return $this
  133. */
  134. protected function assign($name, $value = '')
  135. {
  136. $this->view->assign($name, $value);
  137. return $this;
  138. }
  139. /**
  140. * 初始化模板引擎
  141. * @access protected
  142. * @param array|string $engine 引擎参数
  143. * @return $this
  144. */
  145. protected function engine($engine)
  146. {
  147. $this->view->engine($engine);
  148. return $this;
  149. }
  150. /**
  151. * 设置验证失败后是否抛出异常
  152. * @access protected
  153. * @param bool $fail 是否抛出异常
  154. * @return $this
  155. */
  156. protected function validateFailException($fail = true)
  157. {
  158. $this->failException = $fail;
  159. return $this;
  160. }
  161. /**
  162. * 验证数据
  163. * @access protected
  164. * @param array $data 数据
  165. * @param string|array $validate 验证器名或者验证规则数组
  166. * @param array $message 提示信息
  167. * @param bool $batch 是否批量验证
  168. * @param mixed $callback 回调方法(闭包)
  169. * @return array|string|true
  170. * @throws ValidateException
  171. */
  172. protected function validate($data, $validate, $message = [], $batch = false, $callback = null)
  173. {
  174. if (is_array($validate)) {
  175. $v = Loader::validate();
  176. $v->rule($validate);
  177. } else {
  178. // 支持场景
  179. if (strpos($validate, '.')) {
  180. list($validate, $scene) = explode('.', $validate);
  181. }
  182. $v = Loader::validate($validate);
  183. !empty($scene) && $v->scene($scene);
  184. }
  185. // 批量验证
  186. if ($batch || $this->batchValidate) {
  187. $v->batch(true);
  188. }
  189. // 设置错误信息
  190. if (is_array($message)) {
  191. $v->message($message);
  192. }
  193. // 使用回调验证
  194. if ($callback && is_callable($callback)) {
  195. call_user_func_array($callback, [$v, &$data]);
  196. }
  197. if (!$v->check($data)) {
  198. if ($this->failException) {
  199. throw new ValidateException($v->getError());
  200. }
  201. return $v->getError();
  202. }
  203. return true;
  204. }
  205. }