存客宝应用接口初始化

This commit is contained in:
Ghost
2026-01-05 10:25:32 +08:00
parent fa37227954
commit 02797ba50a
414 changed files with 85530 additions and 75 deletions

View File

@@ -0,0 +1,59 @@
<?php
namespace app\common\middleware;
/**
* 跨域请求中间件
*/
class AllowCrossDomain
{
/**
* 处理跨域请求
* @param \think\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, \Closure $next)
{
// // 获取当前请求的域名
// $origin = $request->header('origin');
//
// // 当请求使用 credentials 模式时,不能使用通配符
// // 必须指定具体的域名或提取请求中的 Origin
// $allowOrigin = '*';
// if ($origin) {
// // 如果需要限制特定域名,可以在这里判断
// // 以下是允许的域名列表,如果请求来自这些域名之一,则允许跨域
// $allowDomains = [ /* */ ];
//
// // 如果请求来源在允许列表中,直接使用该源
// if (in_array($origin, $allowDomains)) {
// $allowOrigin = $origin;
// }
// }
//
// // 设置允许的请求头信息
// $allowHeaders = [
// 'Authorization', 'Content-Type', 'If-Match', 'If-Modified-Since',
// 'If-None-Match', 'If-Unmodified-Since', 'X-Requested-With',
// 'X-Token', 'X-Api-Token', 'Accept', 'Origin'
// ];
//
// $response = $next($request);
//
// // 添加跨域响应头
// $response->header([
// 'Access-Control-Allow-Origin' => $allowOrigin,
// 'Access-Control-Allow-Headers' => implode(', ', $allowHeaders),
// 'Access-Control-Allow-Methods' => 'GET, POST, PUT, DELETE, OPTIONS',
// 'Access-Control-Allow-Credentials' => 'true',
// 'Access-Control-Max-Age' => '86400',
// ]);
//
// // 对于预检请求,直接返回成功响应
// if ($request->method(true) == 'OPTIONS') {
// return response()->code(200);
// }
//
// return $response;
}
}

View File

@@ -0,0 +1,49 @@
<?php
namespace app\common\middleware;
use app\common\util\JwtUtil;
use think\facade\Log;
/**
* JWT认证中间件
*/
class jwt
{
/**
* 处理请求
* @param \think\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, \Closure $next)
{
// 获取Token
$token = JwtUtil::getRequestToken();
// 验证Token
if (!$token) {
return json([
'code' => 401,
'msg' => '未授权访问,缺少有效的身份凭证',
'data' => null
])->header(['Content-Type' => 'application/json; charset=utf-8']);
}
$payload = JwtUtil::verifyToken($token);
if (!$payload) {
return json([
'code' => 401,
'msg' => '授权已过期或无效',
'data' => null
])->header(['Content-Type' => 'application/json; charset=utf-8']);
}
// 将用户信息附加到请求中
$request->userInfo = $payload;
// 写入日志
Log::info('JWT认证通过', ['user_id' => $payload['id'] ?? 0, 'username' => $payload['username'] ?? '']);
return $next($request);
}
}