口腔客户管理系统
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

BaseController.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 app;
  12. // header('Access-Control-Allow-Origin:*');
  13. // header('Access-Control-Allow-Headers:x-requested-with,content-type,Authorization,token');
  14. // header('Access-Control-Allow-Methods:GET,POST,OPTIONS,DELETE,PUT');
  15. // header('Access-Control-Allow-Credentials:true');
  16. use think\App;
  17. use think\Container;
  18. use think\exception\ValidateException;
  19. use think\Response;
  20. use traits\controller\Jump;
  21. class BaseController
  22. {
  23. use Jump;
  24. /**
  25. * 视图类实例
  26. * @var \think\View
  27. */
  28. protected $view;
  29. /**
  30. * Request实例
  31. * @var \think\Request
  32. */
  33. protected $request;
  34. /**
  35. * 验证失败是否抛出异常
  36. * @var bool
  37. */
  38. protected $failException = false;
  39. /**
  40. * 是否批量验证
  41. * @var bool
  42. */
  43. protected $batchValidate = false;
  44. /**
  45. * 前置操作方法列表(即将废弃)
  46. * @var array $beforeActionList
  47. */
  48. protected $beforeActionList = [];
  49. /**
  50. * 控制器中间件
  51. * @var array
  52. */
  53. protected $middleware = [];
  54. /**
  55. * 无需签名
  56. * @var array
  57. */
  58. protected $noSize = [];
  59. /**
  60. * 用户信息
  61. * @var array
  62. */
  63. protected $userInfo = [];
  64. /**
  65. * 无需登录的方法,同时也就不需要鉴权了
  66. * @var array
  67. */
  68. protected $noNeedLogin = [];
  69. /**
  70. * 无需安全验证
  71. * @var array
  72. */
  73. protected $noSecure = [];
  74. protected $param = [];
  75. /**
  76. * 构造方法
  77. * @access public
  78. */
  79. public function __construct(App $app = null)
  80. {
  81. $this->app = $app ?: Container::get('app');
  82. $this->request = $this->app['request'];
  83. $this->view = $this->app['view'];
  84. $this->initialize();
  85. $this->registerMiddleware();
  86. // 前置操作方法 即将废弃
  87. foreach ((array)$this->beforeActionList as $method => $options) {
  88. is_numeric($method) ?
  89. $this->beforeAction($options) :
  90. $this->beforeAction($method, $options);
  91. }
  92. }
  93. //是否需要登录
  94. protected static function checklogin($arr)
  95. {
  96. if (isset($_SERVER['REQUEST_URI'])) {
  97. $action = $_SERVER['REQUEST_URI'];
  98. } else {
  99. return false;
  100. }
  101. $action = substr($action, strrpos($action, '/') + 1);
  102. if (strrpos($action, '?') !== false) {
  103. $action = substr($action, 0, strrpos($action, '?'));
  104. }
  105. $arr = is_array($arr) ? $arr : explode(',', $arr);
  106. if (!$arr) {
  107. return false;
  108. }
  109. $arr = array_map('strtolower', $arr);
  110. // 是否存在
  111. if (in_array(strtolower($action), $arr) || in_array('*', $arr)) {
  112. return true;
  113. }
  114. // 没找到匹配
  115. return false;
  116. }
  117. private function getParam($method)
  118. {
  119. $arr = [];
  120. switch ($method) {
  121. case 'post':
  122. $arr = $_POST;
  123. break;
  124. case 'get':
  125. $arr = $_GET;
  126. break;
  127. case 'delete':
  128. parse_str(file_get_contents('php://input'), $arr);
  129. break;
  130. case 'put':
  131. parse_str(file_get_contents('php://input'), $arr);
  132. break;
  133. case 'patch':
  134. parse_str(file_get_contents('php://input'), $arr);
  135. break;
  136. }
  137. return $arr;
  138. }
  139. // 初始化
  140. protected function initialize()
  141. {
  142. $function = request()->action(true);
  143. $module = request()->module();
  144. $controller = request()->controller();
  145. $classname = "\app\\$module\\validate\\$controller";
  146. if (!self::checklogin($this->noNeedLogin)) {//true为不用验证
  147. $controller_classname = "\app\\$module\\model\\$controller";
  148. $directory = new $controller_classname();
  149. if (method_exists($directory, $function)) {
  150. $token = $this->request->header();//获取请求头token
  151. if (!isset($token['token'])) {
  152. tojson(10001);
  153. } else {
  154. $this->userInfo = getToken($token['token']);
  155. }
  156. }
  157. }
  158. $method = $this->request->method();
  159. $method = strtolower($method);
  160. $param = $this->getParam($method);
  161. if (class_exists($classname)) {
  162. $validate = new $classname;
  163. if ($validate->hasScene($function)) {//验证场景
  164. if (!$validate->scene($function)->check($param)) {//验证器
  165. tojson('10000', $validate->getError());
  166. }
  167. }
  168. }
  169. //安全验证
  170. nosecure($method, $this->noSecure, $function);
  171. //验签
  172. verifysign($param, $this->noSize);//加上验签
  173. if (isset($param['page']) && !empty($param['page'])) {
  174. $param['page'] = $param['page'] - 1;
  175. if (!isset($param['pageSize'])) {
  176. $page = 0;
  177. $pageSize = 0;
  178. } else {
  179. $page = $param['pageSize'] * $param['page'];
  180. $pageSize = $param['pageSize'];
  181. }
  182. } else {
  183. $page = 0;
  184. $pageSize = 0;
  185. }
  186. $this->request->page = $page;
  187. $this->request->pageSize = $pageSize;
  188. $this->param = $param;
  189. }
  190. // 注册控制器中间件
  191. public function registerMiddleware()
  192. {
  193. foreach ($this->middleware as $key => $val) {
  194. if (!is_int($key)) {
  195. $only = $except = null;
  196. if (isset($val['only'])) {
  197. $only = array_map(function ($item) {
  198. return strtolower($item);
  199. }, $val['only']);
  200. } elseif (isset($val['except'])) {
  201. $except = array_map(function ($item) {
  202. return strtolower($item);
  203. }, $val['except']);
  204. }
  205. if (isset($only) && !in_array($this->request->action(), $only)) {
  206. continue;
  207. } elseif (isset($except) && in_array($this->request->action(), $except)) {
  208. continue;
  209. } else {
  210. $val = $key;
  211. }
  212. }
  213. $this->app['middleware']->controller($val);
  214. }
  215. }
  216. /**
  217. * 前置操作
  218. * @access protected
  219. * @param string $method 前置操作方法名
  220. * @param array $options 调用参数 ['only'=>[...]] 或者['except'=>[...]]
  221. */
  222. protected function beforeAction($method, $options = [])
  223. {
  224. if (isset($options['only'])) {
  225. if (is_string($options['only'])) {
  226. $options['only'] = explode(',', $options['only']);
  227. }
  228. $only = array_map(function ($val) {
  229. return strtolower($val);
  230. }, $options['only']);
  231. if (!in_array($this->request->action(), $only)) {
  232. return;
  233. }
  234. } elseif (isset($options['except'])) {
  235. if (is_string($options['except'])) {
  236. $options['except'] = explode(',', $options['except']);
  237. }
  238. $except = array_map(function ($val) {
  239. return strtolower($val);
  240. }, $options['except']);
  241. if (in_array($this->request->action(), $except)) {
  242. return;
  243. }
  244. }
  245. call_user_func([$this, $method]);
  246. }
  247. /**
  248. * 加载模板输出
  249. * @access protected
  250. * @param string $template 模板文件名
  251. * @param array $vars 模板输出变量
  252. * @param array $config 模板参数
  253. * @return mixed
  254. */
  255. protected function fetch($template = '', $vars = [], $config = [])
  256. {
  257. return Response::create($template, 'view')->assign($vars)->config($config);
  258. }
  259. /**
  260. * 渲染内容输出
  261. * @access protected
  262. * @param string $content 模板内容
  263. * @param array $vars 模板输出变量
  264. * @param array $config 模板参数
  265. * @return mixed
  266. */
  267. protected function display($content = '', $vars = [], $config = [])
  268. {
  269. return Response::create($content, 'view')->assign($vars)->config($config)->isContent(true);
  270. }
  271. /**
  272. * 模板变量赋值
  273. * @access protected
  274. * @param mixed $name 要显示的模板变量
  275. * @param mixed $value 变量的值
  276. * @return $this
  277. */
  278. protected function assign($name, $value = '')
  279. {
  280. $this->view->assign($name, $value);
  281. return $this;
  282. }
  283. /**
  284. * 视图过滤
  285. * @access protected
  286. * @param Callable $filter 过滤方法或闭包
  287. * @return $this
  288. */
  289. protected function filter($filter)
  290. {
  291. $this->view->filter($filter);
  292. return $this;
  293. }
  294. /**
  295. * 初始化模板引擎
  296. * @access protected
  297. * @param array|string $engine 引擎参数
  298. * @return $this
  299. */
  300. protected function engine($engine)
  301. {
  302. $this->view->engine($engine);
  303. return $this;
  304. }
  305. /**
  306. * 设置验证失败后是否抛出异常
  307. * @access protected
  308. * @param bool $fail 是否抛出异常
  309. * @return $this
  310. */
  311. protected function validateFailException($fail = true)
  312. {
  313. $this->failException = $fail;
  314. return $this;
  315. }
  316. /**
  317. * 验证数据
  318. * @access protected
  319. * @param array $data 数据
  320. * @param string|array $validate 验证器名或者验证规则数组
  321. * @param array $message 提示信息
  322. * @param bool $batch 是否批量验证
  323. * @param mixed $callback 回调方法(闭包)
  324. * @return array|string|true
  325. * @throws ValidateException
  326. */
  327. protected function validate($data, $validate, $message = [], $batch = false, $callback = null)
  328. {
  329. if (is_array($validate)) {
  330. $v = $this->app->validate();
  331. $v->rule($validate);
  332. } else {
  333. if (strpos($validate, '.')) {
  334. // 支持场景
  335. list($validate, $scene) = explode('.', $validate);
  336. }
  337. $v = $this->app->validate($validate);
  338. if (!empty($scene)) {
  339. $v->scene($scene);
  340. }
  341. }
  342. // 是否批量验证
  343. if ($batch || $this->batchValidate) {
  344. $v->batch(true);
  345. }
  346. if (is_array($message)) {
  347. $v->message($message);
  348. }
  349. if ($callback && is_callable($callback)) {
  350. call_user_func_array($callback, [$v, &$data]);
  351. }
  352. if (!$v->check($data)) {
  353. if ($this->failException) {
  354. throw new ValidateException($v->getError());
  355. }
  356. return $v->getError();
  357. }
  358. return true;
  359. }
  360. public function __debugInfo()
  361. {
  362. $data = get_object_vars($this);
  363. unset($data['app'], $data['request']);
  364. return $data;
  365. }
  366. }