chore: 首次提交 - 关联 GitHub fnvtk/MBTI_wang
Made-with: Cursor
This commit is contained in:
48
api/app/middleware/Auth.php
Normal file
48
api/app/middleware/Auth.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
namespace app\middleware;
|
||||
|
||||
use app\common\service\JwtService;
|
||||
|
||||
/**
|
||||
* 认证中间件
|
||||
*/
|
||||
class Auth
|
||||
{
|
||||
/**
|
||||
* 处理请求
|
||||
*
|
||||
* @param \think\Request $request
|
||||
* @param \Closure $next
|
||||
* @return Response
|
||||
*/
|
||||
public function handle($request, \Closure $next)
|
||||
{
|
||||
$token = JwtService::getTokenFromRequest($request);
|
||||
|
||||
if (empty($token)) {
|
||||
return json([
|
||||
'code' => 401,
|
||||
'message' => '未登录或Token无效',
|
||||
'data' => null
|
||||
])->code(401);
|
||||
}
|
||||
|
||||
// 验证Token
|
||||
$payload = JwtService::verifyToken($token);
|
||||
|
||||
if (!$payload) {
|
||||
return json([
|
||||
'code' => 401,
|
||||
'message' => 'Token无效或已过期',
|
||||
'data' => null
|
||||
])->code(401);
|
||||
}
|
||||
|
||||
// 将用户信息存储到请求中,供控制器使用
|
||||
$request->user = $payload;
|
||||
$request->userId = $payload['userId'] ?? $payload['user_id'] ?? null;
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
|
||||
74
api/app/middleware/Cors.php
Normal file
74
api/app/middleware/Cors.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
namespace app\middleware;
|
||||
|
||||
/**
|
||||
* 跨域中间件
|
||||
*/
|
||||
class Cors
|
||||
{
|
||||
/**
|
||||
* 处理请求
|
||||
*
|
||||
* @param \think\Request $request
|
||||
* @param \Closure $next
|
||||
* @return Response
|
||||
*/
|
||||
public function handle($request, \Closure $next)
|
||||
{
|
||||
// 从配置文件获取跨域配置
|
||||
$config = config('cors');
|
||||
|
||||
$allowOrigin = $config['allow_origin'] ?? '*';
|
||||
$allowMethods = $config['allow_methods'] ?? 'GET,POST,PUT,DELETE,OPTIONS';
|
||||
$allowHeaders = $config['allow_headers'] ?? 'Content-Type,Authorization,X-Requested-With,Accept';
|
||||
$allowCredentials = $config['allow_credentials'] ?? false;
|
||||
$maxAge = $config['max_age'] ?? 86400;
|
||||
|
||||
// 获取请求的Origin
|
||||
$origin = $request->header('Origin', '');
|
||||
|
||||
// 确定允许的Origin
|
||||
$allowedOrigin = null;
|
||||
if ($allowOrigin === '*') {
|
||||
$allowedOrigin = '*';
|
||||
} else {
|
||||
// 支持多个域名(用逗号分隔)
|
||||
$origins = array_map('trim', explode(',', $allowOrigin));
|
||||
|
||||
// 如果请求的Origin在允许列表中,则使用该Origin
|
||||
// 同时支持带/不带尾部斜杠的匹配
|
||||
foreach ($origins as $allowed) {
|
||||
if ($origin === $allowed || $origin === rtrim($allowed, '/') || rtrim($origin, '/') === $allowed) {
|
||||
$allowedOrigin = $origin;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 处理预检请求(OPTIONS)
|
||||
if ($request->method(true) === 'OPTIONS') {
|
||||
$response = response('', 200);
|
||||
} else {
|
||||
$response = $next($request);
|
||||
}
|
||||
|
||||
// 设置CORS响应头
|
||||
if ($allowedOrigin !== null) {
|
||||
$headers = [
|
||||
'Access-Control-Allow-Origin' => $allowedOrigin,
|
||||
'Access-Control-Allow-Methods' => $allowMethods,
|
||||
'Access-Control-Allow-Headers' => $allowHeaders,
|
||||
'Access-Control-Max-Age' => (string)$maxAge,
|
||||
];
|
||||
|
||||
if ($allowCredentials) {
|
||||
$headers['Access-Control-Allow-Credentials'] = 'true';
|
||||
}
|
||||
|
||||
// 使用header方法设置响应头(ThinkPHP 8 需要传递数组)
|
||||
$response->header($headers);
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
57
api/app/middleware/SuperAdmin.php
Normal file
57
api/app/middleware/SuperAdmin.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
namespace app\middleware;
|
||||
|
||||
use app\common\service\JwtService;
|
||||
|
||||
/**
|
||||
* 超级管理员权限中间件
|
||||
*/
|
||||
class SuperAdmin
|
||||
{
|
||||
/**
|
||||
* 处理请求
|
||||
*
|
||||
* @param \think\Request $request
|
||||
* @param \Closure $next
|
||||
* @return Response
|
||||
*/
|
||||
public function handle($request, \Closure $next)
|
||||
{
|
||||
$token = JwtService::getTokenFromRequest($request);
|
||||
|
||||
if (empty($token)) {
|
||||
return json([
|
||||
'code' => 401,
|
||||
'message' => '未登录或Token无效',
|
||||
'data' => null
|
||||
])->code(401);
|
||||
}
|
||||
|
||||
// 验证Token
|
||||
$payload = JwtService::verifyToken($token);
|
||||
|
||||
if (!$payload) {
|
||||
return json([
|
||||
'code' => 401,
|
||||
'message' => 'Token无效或已过期',
|
||||
'data' => null
|
||||
])->code(401);
|
||||
}
|
||||
|
||||
// 验证是否为超级管理员
|
||||
if ($payload['role'] !== 'superadmin') {
|
||||
return json([
|
||||
'code' => 403,
|
||||
'message' => '无权限访问,需要超级管理员权限',
|
||||
'data' => null
|
||||
])->code(403);
|
||||
}
|
||||
|
||||
// 将用户信息存储到请求中,供控制器使用
|
||||
$request->user = $payload;
|
||||
$request->userId = $payload['userId'] ?? null;
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user