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.
44 lines
965 B
44 lines
965 B
<?php
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
use App\Models\User;
|
|
use Closure;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
class ApiAuth
|
|
{
|
|
/**
|
|
* Handle an incoming request.
|
|
*
|
|
* @param Request $request
|
|
* @param Closure $next
|
|
* @return mixed
|
|
*/
|
|
public function handle(Request $request, Closure $next)
|
|
{
|
|
$auth = $request->header('Authentication');
|
|
|
|
if (empty($auth)) {
|
|
return response()->json([
|
|
'code' => -1,
|
|
'msg' => '关键认证参数缺失',
|
|
'data' => [],
|
|
'status' => 500,
|
|
]);
|
|
}
|
|
|
|
//检查用户 TODO 登录部分待优化
|
|
$user_id = Cache::get($auth);
|
|
if (empty($user_id) || $user_id != User::query()->where(['id' => $user_id, 'status' => 1])->value('id')) {
|
|
return response()->json([
|
|
'code' => -2,
|
|
'msg' => '用户不存在或登录已超时,请重新登录',
|
|
'data' => [],
|
|
'status' => 403,
|
|
]);
|
|
}
|
|
return $next($request);
|
|
}
|
|
}
|