Controller.php 6.6 KB

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