2022-05-25 19:35:57 +08:00
|
|
|
|
<?php
|
|
|
|
|
namespace app\middleware;
|
|
|
|
|
|
|
|
|
|
use Closure;
|
|
|
|
|
use app\service\Jwt as JwtService;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* API登录认证(需要先调用JWT解析用户信息)
|
|
|
|
|
* Class ApiLogin
|
|
|
|
|
* @package app\middleware
|
|
|
|
|
*/
|
|
|
|
|
class ApiLogin
|
|
|
|
|
{
|
|
|
|
|
public function handle($request, Closure $next) {
|
|
|
|
|
$authorization = $request->authorization ?? '';
|
|
|
|
|
if (empty($authorization)) {
|
|
|
|
|
return json(['code' => 6001, 'msg' => '请填写token']);
|
|
|
|
|
}
|
|
|
|
|
if (!JwtService::validate($authorization)) {
|
2022-08-08 17:57:14 +08:00
|
|
|
|
return json(['code' => 6001, 'msg' => '登录状态验证失败或已失效']);
|
2022-05-25 19:35:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$userInfo = $request->user ?? [];
|
|
|
|
|
if (!isset($userInfo['user_id']) || empty($userInfo['user_id'])) {
|
2022-08-08 17:57:14 +08:00
|
|
|
|
return json(['code' => 6001, 'msg' => '登录状态已失效']);
|
2022-05-25 19:35:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 自定义过期时间校验。
|
|
|
|
|
if(isset($userInfo['expire_time']) && time() >= $userInfo['expire_time']) {
|
2022-08-08 17:57:14 +08:00
|
|
|
|
return json(['code' => 6001, 'msg' => '登录状态已失效']);
|
2022-05-25 19:35:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
|
}
|
|
|
|
|
}
|