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