ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
``` ~~~ class base extends BaseController { protected $user; function __construct() { header("Content-type: application/json"); header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Methods: GET, HEAD,OPTIONS, POST"); header("Access-Control-Allow-Credentials: true"); header("Access-Control-Allow-Headers: Content-Type, Content-Length, Accept-Encoding, X-Requested-With, Origin,token,account"); $headers=$this->get_all_headers(); $check_rule=[ 'index/login', ]; $controller_name=request()->controller(); $method_name=request()->action(); $url=strtolower($controller_name."/".$method_name); if(!in_array($url,$check_rule)){ if(empty($headers['token'])){ $this->json_error("非法操作",505); }else{ $token=$headers['token']; $jwt = new Jwts(); if($jwt->checkToken($token)){ $jwt = new Jwts(); $info=$jwt->tokenInfo($token); $uid=$info->jti->id; $user_info=Db::table('admin')->where(['token'=>$token,'id'=>$uid])->find(); if(empty($user_info)){ $this->json_error("非法操作",505); } if($user_info['token_expire']<time()){ $this->json_error("登录失效,请重新登录",505); } if($user_info['status']!=1){ $this->json_error("账户被冻结",505); } $this->user=$user_info; }else{ $this->json_error("非法操作",505); } } } } /** * 获取自定义的header数据 */ protected function get_all_headers() { // 忽略获取的header数据 $ignore = array('host', 'accept', 'content-length', 'content-type'); $headers = array(); foreach ($_SERVER as $key => $value) { if (substr($key, 0, 5) === 'HTTP_') { $key = substr($key, 5); $key = str_replace('_', ' ', $key); $key = str_replace(' ', '-', $key); $key = strtolower($key); if (!in_array($key, $ignore)) { $headers[$key] = $value; } } } return $headers; } /** * json失败返回 * @param string $msg * @param array $data * @param int $code */ public function json_error($msg = '失败', $code = 0) { exit(json_encode(['code' => $code, 'msg' => $msg], JSON_UNESCAPED_UNICODE)); } /** * json成功返回 * @param array $data * @param string $msg * @param int $status */ public function json_success($data = [], $msg = '成功', $status = 200) { if (is_string($data)) { $msg = $data; $data = []; } exit(json_encode(['code' => $status, 'msg' => $msg, 'data' => $data], JSON_UNESCAPED_UNICODE)); } } ~~~ ```