// +---------------------------------------------------------------------- namespace app\adminapi\middleware; use app\Request; use app\services\system\admin\AdminAuthServices; use crmeb\interfaces\MiddlewareInterface; use think\facade\Config; use crmeb\services\CacheService; /** * 后台登陆验证中间件 * Class AdminAuthTokenMiddleware * @package app\adminapi\middleware */ class AdminAuthTokenMiddleware implements MiddlewareInterface { /** * @param Request $request * @param \Closure $next * @return mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @author 吴汐 * @email 442384644@qq.com * @date 2023/04/07 */ public function handle(Request $request, \Closure $next) { $token = trim(ltrim($request->header(Config::get('cookie.token_name', 'Authori-zation')), 'Bearer')); if (!$token) { $token = trim(ltrim($request->get('token'))); } /** @var AdminAuthServices $service */ $service = app()->make(AdminAuthServices::class); $adminInfo = $service->parseToken($token); $request->macro('isAdminLogin', function () use (&$adminInfo) { return !is_null($adminInfo); }); $request->macro('adminId', function () use (&$adminInfo) { return $adminInfo['id']; }); $request->macro('adminInfo', function () use (&$adminInfo) { return $adminInfo; }); return $next($request); } }