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.
 
 

70 lines
1.6 KiB

<?php
declare (strict_types=1);
namespace app\middleware;
use auth\JwtAuth;
class CheckJwtAuth
{
/**
* 处理请求
*
* @param \think\Request $request
* @param \Closure $next
* @return Response
*/
public function handle($request, \Closure $next)
{
//获取当前控制器模型
// $url = str_replace(['//', '\\', '/'], '', $request->root()) . '/' . $request->pathinfo();
// $filter_router = config('jwtauth.filter_router');
// foreach ($filter_router as $v) {
// if (strpos(strtolower($url), strtolower($v)) === 0) {
// return $next($request);
// }
// }
//测试开户app_debug,设置自定用户参数及站点参数
env('app_debug') ? $this->getLocalToken() : $this->getJwtToken();
return $next($request);
}
/**
* 正式环境
* @return \type
*/
private function getJwtToken()
{
$token = JwtAuth::getInstance()->checkToken(request()->header('Token'));
if ($token) {
/**
* 登录用户ID
*/
defined('UID') ?: define('UID', $token['user_id']);
/**
* 站点ID
*/
defined('SITE_ID') ?: define('SITE_ID', $token['site_id']);
}
}
/**
* 本地模拟
*/
private function getLocalToken()
{
/**
* 登录用户ID
*/
defined('UID') ?: define('UID', Request()->param('uid', 1));
/**
* 站点ID
*/
defined('SITE_ID') ?: define('SITE_ID', Request()->param('site_id', 1));
}
}