刮刮后端接口
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.0 KiB

<?php
declare (strict_types = 1);
namespace app\middleware;
use think\Exception;
use \think\facade\Request;
use think\facade\Session;
class CheckToken
{
/**
*
* 处理请求
* @param $request
* @param \Closure $next
* @return mixed|\think\response\Json
* @author whj
* @date 2023-08-22 14:57
*/
public function handle($request, \Closure $next)
{
try {
$token = Request::header()['token']??false;
if(!$token)
throw new Exception('Without Token',201);
$userinfo = checkToken($token);
if($userinfo['code'] != 200)
throw new Exception('Token checked error',202);
$request->userInfo = $userinfo['data'];
if (!Session::get('login_user_data')) {
throw new Exception('用户未登录,请先登陆后操作',203);
}
}
catch (\Exception $err){
return json(['code'=>$err->getCode(),'msg'=>$err->getMessage()]);
}
return $next($request);
}
}