口腔客户管理系统
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

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