<?php
namespace app\admin\controller;
use app\admin\model\User;
use app\BaseController;
use app\Response;
use think\App;
class MBase {
protected $app;
protected $request;
public function __construct(App $app)
{
$this->app = $app;
$this->request = $this->app->request;
$this->initialize();
}
protected function initialize()
{
$this->checkToken();
}
public function checkToken()
{
$extraList = ["login"];
$pathinfo = $this->request->pathinfo();
$tokenStr = $this->request->header('Authorization');
if(in_array($pathinfo, $extraList)){
return true;
}else{
try{
$user = User::where("token","2222")->find();
if(!$user){
//send() exit;才能拦截,直接返回。
echo Response::error2(999,"token error")->send();
exit;
}
}catch(\Exception $e){
echo Response::error2(999,"token error")->send();
exit;
}
}
}
}
<?php
// 应用公共文件
namespace app;
// 应用公共文件
class Response{
static function result($status, $message = '', $data = null, $httpStatus = 200)
{
$rersult = [
"code" => $status,
"message" => $message,
"data" => $data,
];
return json($rersult, $httpStatus);
}
public static function success($data = []){
return self::result(1,"success",$data,200);
}
public static function error($error = null){
return self::result(0,$error?:"error",null,200);
}
public static function error2($code=0,$error = null){
return self::result($code,$error?:"error",null,200);
}
}