123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- namespace app\index\controller;
- use think\Controller;
- class Base extends Controller
- {
- protected $param;
- protected function initialize()
- {
- /*
- $url = request()->baseUrl();
- $orurl = $url;
- $orurlarr = array_filter(explode('/', $url));
- $urlarr = array_filter(explode('/', strtolower($url)));
- if (count($orurlarr) >= 4) {
- $url = $urlarr[3] . "/" . $urlarr[4];
- $ischeck = true;
- $ignorelist = $this->getignoreurl();
- foreach ($ignorelist as $key => $value) {
- if ($url == $value) {
- $ischeck = false;
- }
- }
- //if ($ischeck) {
- // $this->checkToken();
- //}
- //$functionname = $orurlarr[4];
- //$this->checkrules($functionname);
- }
- */
- }
- private function getignoreurl()
- {
- $list = [];
- return $list;
- }
- /**
- * 校验token
- * 20220107
- * wj
- */
- /*private function checkToken()
- {
- $l_w = new webuserlogic();
- $token = request()->server('HTTP_TOKEN');
- $param = ['token' => $token];
- $result = $l_w->queryinfobytoken($param);
- if (1 != $result['status']) {
- $str = backjson2(0, '登录失效', $result['data']);
- exit($str);
- }
- }*/
- /**
- * 设置请求数据规则
- * 20220107
- * wj
- */
- protected function setrules()
- {
- $list = [];
- return $list;
- }
- /**
- * 校验请求数据
- * name 变量名
- * title 名称
- * regex 正则
- * require 必填
- * type 类型
- * 20220107
- * wj
- */
- protected function checkrules($functionname)
- {
- $param = request()->param();
- $rules = $this->setrules();
- if (isset($rules[$functionname])) {
- try {
- $list = $rules[$functionname];
- $namelist = array_column($list, 'name');
- if (count($namelist) != count($list) || count($list) != count(array_filter($namelist))) {
- throw new \Exception("规则name设置错误");
- }
- $titlelist = array_column($list, 'title');
- if (count($titlelist) != count($list) || count($list) != count(array_filter($titlelist))) {
- throw new \Exception("规则title设置错误");
- }
- foreach ($list as $key => $value) {
- $name = $value['name'];
- $title = $value['title'];
- //必填
- if ($value['require']) {
- if (!isset($param[$name])) {
- throw new \Exception($title . '未填');
- }
- }
- if (!isset($param[$name])) {
- continue;
- }
- $paramvalue = $param[$name];
- //类型
- if (isset($value['type'])) {
- $tpe = $value['type'];
- switch ($tpe) {
- case 'string':
- if (!is_string($paramvalue) || empty($paramvalue)) {
- throw new \Exception($title . '格式错误');
- }
- break;
- case 'numeric':
- if (!is_numeric($paramvalue)) {
- throw new \Exception($title . '格式错误');
- }
- break;
- case 'array':
- if (!is_array($paramvalue)) {
- throw new \Exception($title . '格式错误');
- }
- break;
- }
- }
- //正则
- if (isset($value['regex'])) {
- $regex = $value['regex'];
- if (preg_match($regex, $paramvalue)) {
- throw new \Exception($title . '正则格式错误');
- }
- }
- }
- } catch (\Exception $e) {
- $str = backjson2(0, $e->getMessage());
- exit($str);
- }
- }
- $this->param = $param;
- }
- }
|