更新jwt问题
This commit is contained in:
parent
dbb40c38c4
commit
1a7303cedf
24
.gitignore
vendored
Normal file
24
.gitignore
vendored
Normal file
@ -0,0 +1,24 @@
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
pnpm-debug.log*
|
||||
lerna-debug.log*
|
||||
|
||||
node_modules
|
||||
dist
|
||||
dist-ssr
|
||||
*.local
|
||||
|
||||
# Editor directories and files
|
||||
.vscode/*
|
||||
!.vscode/extensions.json
|
||||
.idea
|
||||
.DS_Store
|
||||
*.suo
|
||||
*.ntvs*
|
||||
*.njsproj
|
||||
*.sln
|
||||
*.sw?
|
||||
@ -7,9 +7,9 @@ use think\App;
|
||||
use think\exception\ValidateException;
|
||||
use think\Validate;
|
||||
use think\facade\Request;
|
||||
use think\facade\Session;
|
||||
use think\facade\Cache;
|
||||
use app\admin\controller\OperationLog\OperationLogger;
|
||||
use app\service\JwtService;
|
||||
|
||||
/**
|
||||
* 控制器基础类
|
||||
@ -159,7 +159,37 @@ abstract class BaseController
|
||||
*/
|
||||
protected function getAdminUserInfo(): array
|
||||
{
|
||||
return JwtService::getUserFromHeader($this->request->header('Authorization', ''));
|
||||
$userId = $this->request->header('Authorization', '');
|
||||
|
||||
if (!preg_match('/Bearer\s+(.+)/i', $userId, $matches)) {
|
||||
return ['id' => 0, 'account' => '', 'name' => ''];
|
||||
}
|
||||
|
||||
$tokenData = $this->verifyTokenFromHeader($matches[1]);
|
||||
|
||||
if (!$tokenData || !isset($tokenData['user'])) {
|
||||
return ['id' => 0, 'account' => '', 'name' => ''];
|
||||
}
|
||||
|
||||
return (array)$tokenData['user'];
|
||||
}
|
||||
|
||||
private function verifyTokenFromHeader($token): ?array
|
||||
{
|
||||
$authHeader = $this->request->header('Authorization', '');
|
||||
if (!preg_match('/Bearer\s+(.+)/i', $authHeader, $matches)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
$decoded = \Firebase\JWT\JWT::decode(
|
||||
$matches[1],
|
||||
new \Firebase\JWT\Key(\app\service\JwtService::getSecret(), 'HS256')
|
||||
);
|
||||
return (array)$decoded;
|
||||
} catch (\Exception $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -17,7 +17,86 @@ use app\model\AdminUserGroup;
|
||||
class MenuController extends BaseController
|
||||
{
|
||||
/**
|
||||
* 获取用户菜单接口
|
||||
* 获取当前登录用户的菜单接口
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function getMyMenus()
|
||||
{
|
||||
try {
|
||||
$userInfo = $this->getAdminUserInfo();
|
||||
|
||||
if (!$userInfo || !isset($userInfo['id']) || $userInfo['id'] == 0) {
|
||||
return json([
|
||||
'code' => 401,
|
||||
'msg' => '用户ID不存在,请重新登录',
|
||||
'data' => null
|
||||
]);
|
||||
}
|
||||
|
||||
$user = AdminUser::where('id', $userInfo['id'])->find();
|
||||
|
||||
if (!$user) {
|
||||
return json([
|
||||
'code' => 404,
|
||||
'msg' => '用户不存在',
|
||||
'data' => null
|
||||
]);
|
||||
}
|
||||
|
||||
// 获取用户组权限信息
|
||||
$userGroup = AdminUserGroup::where('id', $user['group_id'])
|
||||
->find();
|
||||
|
||||
if (!$userGroup) {
|
||||
return json([
|
||||
'code' => 404,
|
||||
'msg' => '用户组不存在',
|
||||
'data' => null
|
||||
]);
|
||||
}
|
||||
|
||||
// 解析权限数组
|
||||
$menuIds = [];
|
||||
if (!empty($userGroup['rights'])) {
|
||||
$menuIds = is_array($userGroup['rights']) ? $userGroup['rights'] : json_decode($userGroup['rights'], true);
|
||||
}
|
||||
|
||||
// 如果权限为空,返回空数组
|
||||
if (empty($menuIds)) {
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => 'success',
|
||||
'data' => []
|
||||
]);
|
||||
}
|
||||
|
||||
// 获取有权限的菜单
|
||||
$menus = SystemMenu::where('delete_time', null)
|
||||
->where('status', 1)
|
||||
->whereIn('id', $menuIds)
|
||||
->field('id,pid,title,path,component_path,icon,sort')
|
||||
->order('sort', 'asc')
|
||||
->select();
|
||||
|
||||
// 将菜单转换为树形结构
|
||||
$treeMenus = $this->buildMenuTree($menus->toArray());
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => 'success',
|
||||
'data' => $treeMenus
|
||||
]);
|
||||
} catch (DbException $e) {
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => 'fail:' . $e->getMessage(),
|
||||
'data' => $e->getTraceAsString()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户菜单接口(需要传入用户ID)
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function getMenus(int $id)
|
||||
|
||||
@ -27,6 +27,10 @@ class CustomCors
|
||||
'https://www.yunzer.cn',
|
||||
'http://yunzer.cn',
|
||||
'https://yunzer.cn',
|
||||
'http://back.yunzer.cn',
|
||||
'https://back.yunzer.cn',
|
||||
'http://backend.yunzer.cn',
|
||||
'https://backend.yunzer.cn',
|
||||
];
|
||||
|
||||
// 检查是否为允许的域名
|
||||
|
||||
@ -16,6 +16,7 @@ Route::get('user/info', 'app\\admin\\controller\\LoginController@userInfo');
|
||||
|
||||
// 菜单路由
|
||||
Route::get('allmenu', 'app\\admin\\controller\\MenuController@getAllMenus');
|
||||
Route::get('menu/my', 'app\\admin\\controller\\MenuController@getMyMenus');
|
||||
Route::get('menu/:id', 'app\\admin\\controller\\MenuController@getMenus');
|
||||
Route::post('createMenu', 'app\\admin\\controller\\MenuController@createMenu');
|
||||
Route::put('updateMenu/:id', 'app\\admin\\controller\\MenuController@updateMenu');
|
||||
|
||||
@ -9,7 +9,7 @@ use Firebase\JWT\Key;
|
||||
|
||||
class JwtService
|
||||
{
|
||||
private static string $secret = 'meitian@#!';
|
||||
private static string $secret = '8s9G7k2P8m5Q9r4T7y8U9i0O8p7L6k5J8H9G7F';
|
||||
private static int $expire = 86400;
|
||||
|
||||
public static function generateToken(array $userInfo): string
|
||||
|
||||
Loading…
Reference in New Issue
Block a user