Controller.php 6.6 KB

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