login
This commit is contained in:
44
app/Http/Middleware/AuthMiddleware.php
Normal file
44
app/Http/Middleware/AuthMiddleware.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use App\Const\Responses;
|
||||
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
|
||||
*/
|
||||
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 \App\Models\Customer\CustomerUser();
|
||||
$oCustomerUser = $oCustomerUser->findUserByUidWithCache($aUserInfo['uid']);
|
||||
if(empty($oCustomerUser)){
|
||||
return response()->json([
|
||||
'code'=>Responses::CODE_ERROR,
|
||||
'msg'=>'用户不存在',
|
||||
]);
|
||||
}
|
||||
$oAuthService->setCurrentUser($oCustomerUser->toArray());
|
||||
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user