1
This commit is contained in:
@@ -75,7 +75,18 @@ if (!function_exists('requestCurl')) {
|
||||
if (!function_exists('dataBuild')) {
|
||||
function dataBuild($array)
|
||||
{
|
||||
return is_array($array) ? http_build_query($array) : $array;
|
||||
if (!is_array($array)) {
|
||||
return $array;
|
||||
}
|
||||
|
||||
// 处理嵌套数组
|
||||
foreach ($array as $key => $value) {
|
||||
if (is_array($value)) {
|
||||
$array[$key] = json_encode($value);
|
||||
}
|
||||
}
|
||||
|
||||
return http_build_query($array);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
49
Server/application/http/middleware/Jwt.php
Normal file
49
Server/application/http/middleware/Jwt.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
namespace app\http\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);
|
||||
}
|
||||
}
|
||||
@@ -13,29 +13,37 @@
|
||||
{
|
||||
"name": "liu21st",
|
||||
"email": "liu21st@gmail.com"
|
||||
},
|
||||
{
|
||||
"name": "yunwuxin",
|
||||
"email": "448901948@qq.com"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=7.4.0",
|
||||
"topthink/framework": "5.0.*",
|
||||
"textalk/websocket": "^1.5",
|
||||
"aliyuncs/oss-sdk-php": "^2.6",
|
||||
"monolog/monolog": "^2.8",
|
||||
"guzzlehttp/guzzle": "^7.5",
|
||||
"overtrue/wechat": "~4.6",
|
||||
"endroid/qr-code": "^4.6",
|
||||
"phpoffice/phpspreadsheet": "^1.28",
|
||||
"workerman/workerman": "^4.1",
|
||||
"workerman/gateway-worker": "^3.1",
|
||||
"eison/image": "^1.0",
|
||||
"hashids/hashids": "^4.1",
|
||||
"khanamiryan/qrcode-detector-decoder": "^2.0",
|
||||
"lizhichao/word": "^2.2",
|
||||
"adbario/php-dot-notation": "^3.1"
|
||||
"php": ">=5.6.0",
|
||||
"topthink/framework": "5.1.*",
|
||||
"topthink/think-installer": "~1.0",
|
||||
"topthink/think-captcha": "^2.0",
|
||||
"topthink/think-helper": "^3.0",
|
||||
"topthink/think-image": "^1.0",
|
||||
"topthink/think-queue": "^2.0",
|
||||
"topthink/think-worker": "^2.0",
|
||||
"textalk/websocket": "^1.2",
|
||||
"aliyuncs/oss-sdk-php": "^2.3",
|
||||
"monolog/monolog": "^1.24",
|
||||
"guzzlehttp/guzzle": "^6.3",
|
||||
"overtrue/wechat": "~4.0",
|
||||
"endroid/qr-code": "^3.5",
|
||||
"phpoffice/phpspreadsheet": "^1.8",
|
||||
"workerman/workerman": "^3.5",
|
||||
"workerman/gateway-worker": "^3.0",
|
||||
"hashids/hashids": "^2.0",
|
||||
"khanamiryan/qrcode-detector-decoder": "^1.0",
|
||||
"lizhichao/word": "^2.0",
|
||||
"adbario/php-dot-notation": "^2.2"
|
||||
},
|
||||
"require-dev": {
|
||||
"symfony/var-dumper": "^5.4",
|
||||
"topthink/think-trace": "^1.0",
|
||||
"symfony/var-dumper": "^3.4",
|
||||
"topthink/think-migration": "^2.0"
|
||||
},
|
||||
"autoload": {
|
||||
@@ -44,18 +52,19 @@
|
||||
},
|
||||
"files": [
|
||||
"application/common.php"
|
||||
]
|
||||
],
|
||||
"classmap": []
|
||||
},
|
||||
"extra": {
|
||||
"think-path": "thinkphp"
|
||||
},
|
||||
"config": {
|
||||
"preferred-install": "dist"
|
||||
"preferred-install": "dist",
|
||||
"allow-plugins": {
|
||||
"topthink/think-installer": true,
|
||||
"easywechat-composer/easywechat-composer": true
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"post-autoload-dump": [
|
||||
"@php think service:discover",
|
||||
"@php think vendor:publish"
|
||||
]
|
||||
}
|
||||
"minimum-stability": "dev",
|
||||
"prefer-stable": true
|
||||
}
|
||||
|
||||
@@ -23,7 +23,8 @@ return [
|
||||
|
||||
// 全局中间件
|
||||
'alias' => [
|
||||
'cors' => 'app\\common\\middleware\\AllowCrossDomain'
|
||||
'cors' => 'app\\common\\middleware\\AllowCrossDomain',
|
||||
'jwt' => 'app\\http\\middleware\\Jwt'
|
||||
],
|
||||
|
||||
// 应用中间件
|
||||
|
||||
Reference in New Issue
Block a user