baseUrl(); $urlarr = array_filter(explode('/', strtolower($url))); $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 = $urlarr[4]; $this->checkrules($functionname); } private function getignoreurl() { $list = [ 'weblogin/login', 'weblogin/queryinfobytoken', ]; 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); } $data = $result['data']; $this->webuserid = $data['id']; } /** * 设置请求数据规则 * 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; } }