From 943d8f7b5f0fe8644d3f530820deb2c256b513cc Mon Sep 17 00:00:00 2001 From: zhenhui <1276357500@qq.com> Date: Fri, 6 Mar 2026 11:34:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BC=98=E5=8C=96=E5=90=8E?= =?UTF-8?q?=E5=8F=B0=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/controller/SystemController.php | 45 +++++++++++------- .../plugin/saiadmin/basic/BaseController.php | 47 ++++++++++++++----- 2 files changed, 65 insertions(+), 27 deletions(-) diff --git a/server/plugin/saiadmin/app/controller/SystemController.php b/server/plugin/saiadmin/app/controller/SystemController.php index ba33b0c..aa49da3 100644 --- a/server/plugin/saiadmin/app/controller/SystemController.php +++ b/server/plugin/saiadmin/app/controller/SystemController.php @@ -33,26 +33,39 @@ class SystemController extends BaseController */ public function userInfo(): Response { - if ($this->adminInfo === null || !is_array($this->adminInfo) || !isset($this->adminInfo['id'])) { - return $this->fail('登录已过期或用户信息无效,请重新登录', 401); + $adminInfo = $this->adminInfo; + if ($adminInfo === null || !is_array($adminInfo) || !isset($adminInfo['id'])) { + $token = getCurrentInfo(); + if (!is_array($token) || empty($token['id'])) { + return $this->fail('登录已过期或用户信息无效,请重新登录', 401); + } + $adminInfo = UserInfoCache::getUserInfo($token['id']); + if (empty($adminInfo) || !isset($adminInfo['id'])) { + $adminInfo = UserInfoCache::setUserInfo($token['id']); + } + if (empty($adminInfo) || !isset($adminInfo['id'])) { + return $this->fail('登录已过期或用户信息无效,请重新登录', 401); + } + $this->adminInfo = $adminInfo; } + $info = []; - $info['id'] = $this->adminInfo['id']; - $info['username'] = $this->adminInfo['username']; - $info['dashboard'] = $this->adminInfo['dashboard'] ?? ''; - $info['avatar'] = $this->adminInfo['avatar'] ?? ''; - $info['email'] = $this->adminInfo['email'] ?? ''; - $info['phone'] = $this->adminInfo['phone'] ?? ''; - $info['gender'] = $this->adminInfo['gender'] ?? ''; - $info['signed'] = $this->adminInfo['signed'] ?? ''; - $info['realname'] = $this->adminInfo['realname'] ?? ''; - $info['department'] = $this->adminInfo['deptList'] ?? []; - if ((int) $this->adminInfo['id'] === 1) { + $info['id'] = $adminInfo['id']; + $info['username'] = $adminInfo['username'] ?? ''; + $info['dashboard'] = $adminInfo['dashboard'] ?? ''; + $info['avatar'] = $adminInfo['avatar'] ?? ''; + $info['email'] = $adminInfo['email'] ?? ''; + $info['phone'] = $adminInfo['phone'] ?? ''; + $info['gender'] = $adminInfo['gender'] ?? ''; + $info['signed'] = $adminInfo['signed'] ?? ''; + $info['realname'] = $adminInfo['realname'] ?? ''; + $info['department'] = $adminInfo['deptList'] ?? []; + if (isset($adminInfo['id']) && $adminInfo['id'] == 1) { $info['buttons'] = ['*']; $info['roles'] = ['super_admin']; } else { - $info['buttons'] = UserAuthCache::getUserAuth($this->adminInfo['id']); - $info['roles'] = Arr::getArrayColumn($this->adminInfo['roleList'] ?? [], 'code'); + $info['buttons'] = UserAuthCache::getUserAuth($adminInfo['id']); + $info['roles'] = Arr::getArrayColumn($adminInfo['roleList'] ?? [], 'code'); } return $this->success($info); } @@ -72,7 +85,7 @@ class SystemController extends BaseController */ public function menu(): Response { - if ($this->adminInfo === null || !is_array($this->adminInfo) || !isset($this->adminInfo['id'])) { + if (!$this->ensureAdminInfo()) { return $this->fail('登录已过期或用户信息无效,请重新登录', 401); } $data = UserMenuCache::getUserMenu($this->adminInfo['id']); diff --git a/server/plugin/saiadmin/basic/BaseController.php b/server/plugin/saiadmin/basic/BaseController.php index d08f346..9491140 100644 --- a/server/plugin/saiadmin/basic/BaseController.php +++ b/server/plugin/saiadmin/basic/BaseController.php @@ -45,23 +45,48 @@ class BaseController extends OpenController */ protected function init(): void { - // 登录模式赋值(仅当 check_admin 有效时赋值,避免登录接口等未带 token 时访问 null 导致报错) - $isLogin = request()->header('check_login', false); + // 登录模式赋值:优先从中间件注入的 header 取,否则从 JWT 当前用户取 $result = request()->header('check_admin'); - if ($isLogin && $result !== null && (is_array($result) || is_object($result))) { - $arr = is_array($result) ? $result : (array) $result; - $adminId = $arr['id'] ?? null; - if ($adminId !== null) { - $this->adminId = (int) $adminId; - $this->adminName = $arr['username'] ?? ''; - $this->adminInfo = UserInfoCache::getUserInfo($adminId); + if (!is_array($result) || empty($result['id'])) { + $result = getCurrentInfo(); + } + if (is_array($result) && !empty($result['id'])) { + $this->adminId = $result['id']; + $this->adminName = $result['username'] ?? ''; + $this->adminInfo = UserInfoCache::getUserInfo($result['id']); + if (empty($this->adminInfo) || !isset($this->adminInfo['id'])) { + $this->adminInfo = UserInfoCache::setUserInfo($result['id']); + } - // 用户数据传递给逻辑层 - $this->logic && $this->logic->init($this->adminInfo); + // 用户数据传递给逻辑层 + if ($this->logic && !empty($this->adminInfo)) { + $this->logic->init($this->adminInfo); } } } + /** + * 确保当前请求已加载管理员信息(用于 init 未正确注入时的回退) + * @return bool 是否已有有效的 adminInfo + */ + protected function ensureAdminInfo(): bool + { + if ($this->adminInfo !== null && is_array($this->adminInfo) && isset($this->adminInfo['id'])) { + return true; + } + $token = getCurrentInfo(); + if (!is_array($token) || empty($token['id'])) { + return false; + } + $this->adminId = $token['id']; + $this->adminName = $token['username'] ?? ''; + $this->adminInfo = UserInfoCache::getUserInfo($token['id']); + if (empty($this->adminInfo) || !isset($this->adminInfo['id'])) { + $this->adminInfo = UserInfoCache::setUserInfo($token['id']); + } + return is_array($this->adminInfo) && isset($this->adminInfo['id']); + } + /** * 验证器调用 */