Files
cycle_api/app/Http/Middleware/AuthMiddleware.php
2024-02-26 00:41:25 +08:00

47 lines
1.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Http\Middleware;
use App\Const\Responses;
use App\Models\Customer\CustomerUserModel;
use App\Service\AuthService;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Symfony\Component\HttpFoundation\Response;
class AuthMiddleware
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
* @throws \Exception
*/
public function handle(Request $request, Closure $next): Response
{
//检查是否登录并且将登录信息放在di中
$oAuthService = new AuthService();
$sToken = $oAuthService->getTokenFromReq($request);
$aUserInfo = $oAuthService->getUserInfoByToken($sToken);
if($aUserInfo == null){
return response()->json([
'code'=>Responses::CODE_ERROR,
'msg'=>'未登录',
]);
}
$oCustomerUser = new CustomerUserModel();
$aCustomerUser = $oCustomerUser->findUserByUidWithCache($aUserInfo['uid']);
if(empty($aCustomerUser)){
return response()->json([
'code'=>Responses::CODE_ERROR,
'msg'=>'用户不存在',
]);
}
$oAuthService->setCurrentUser($aCustomerUser);
return $next($request);
}
}