修复优化后台报错

This commit is contained in:
2026-03-06 11:34:36 +08:00
parent 01f71a4871
commit 943d8f7b5f
2 changed files with 65 additions and 27 deletions

View File

@@ -33,26 +33,39 @@ class SystemController extends BaseController
*/ */
public function userInfo(): Response public function userInfo(): Response
{ {
if ($this->adminInfo === null || !is_array($this->adminInfo) || !isset($this->adminInfo['id'])) { $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); 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 = [];
$info['id'] = $this->adminInfo['id']; $info['id'] = $adminInfo['id'];
$info['username'] = $this->adminInfo['username']; $info['username'] = $adminInfo['username'] ?? '';
$info['dashboard'] = $this->adminInfo['dashboard'] ?? ''; $info['dashboard'] = $adminInfo['dashboard'] ?? '';
$info['avatar'] = $this->adminInfo['avatar'] ?? ''; $info['avatar'] = $adminInfo['avatar'] ?? '';
$info['email'] = $this->adminInfo['email'] ?? ''; $info['email'] = $adminInfo['email'] ?? '';
$info['phone'] = $this->adminInfo['phone'] ?? ''; $info['phone'] = $adminInfo['phone'] ?? '';
$info['gender'] = $this->adminInfo['gender'] ?? ''; $info['gender'] = $adminInfo['gender'] ?? '';
$info['signed'] = $this->adminInfo['signed'] ?? ''; $info['signed'] = $adminInfo['signed'] ?? '';
$info['realname'] = $this->adminInfo['realname'] ?? ''; $info['realname'] = $adminInfo['realname'] ?? '';
$info['department'] = $this->adminInfo['deptList'] ?? []; $info['department'] = $adminInfo['deptList'] ?? [];
if ((int) $this->adminInfo['id'] === 1) { if (isset($adminInfo['id']) && $adminInfo['id'] == 1) {
$info['buttons'] = ['*']; $info['buttons'] = ['*'];
$info['roles'] = ['super_admin']; $info['roles'] = ['super_admin'];
} else { } else {
$info['buttons'] = UserAuthCache::getUserAuth($this->adminInfo['id']); $info['buttons'] = UserAuthCache::getUserAuth($adminInfo['id']);
$info['roles'] = Arr::getArrayColumn($this->adminInfo['roleList'] ?? [], 'code'); $info['roles'] = Arr::getArrayColumn($adminInfo['roleList'] ?? [], 'code');
} }
return $this->success($info); return $this->success($info);
} }
@@ -72,7 +85,7 @@ class SystemController extends BaseController
*/ */
public function menu(): Response public function menu(): Response
{ {
if ($this->adminInfo === null || !is_array($this->adminInfo) || !isset($this->adminInfo['id'])) { if (!$this->ensureAdminInfo()) {
return $this->fail('登录已过期或用户信息无效,请重新登录', 401); return $this->fail('登录已过期或用户信息无效,请重新登录', 401);
} }
$data = UserMenuCache::getUserMenu($this->adminInfo['id']); $data = UserMenuCache::getUserMenu($this->adminInfo['id']);

View File

@@ -45,23 +45,48 @@ class BaseController extends OpenController
*/ */
protected function init(): void protected function init(): void
{ {
// 登录模式赋值(仅当 check_admin 有效时赋值,避免登录接口等未带 token 时访问 null 导致报错) // 登录模式赋值:优先从中间件注入的 header 取,否则从 JWT 当前用户取
$isLogin = request()->header('check_login', false);
$result = request()->header('check_admin'); $result = request()->header('check_admin');
if ($isLogin && $result !== null && (is_array($result) || is_object($result))) { if (!is_array($result) || empty($result['id'])) {
$arr = is_array($result) ? $result : (array) $result; $result = getCurrentInfo();
$adminId = $arr['id'] ?? null; }
if ($adminId !== null) { if (is_array($result) && !empty($result['id'])) {
$this->adminId = (int) $adminId; $this->adminId = $result['id'];
$this->adminName = $arr['username'] ?? ''; $this->adminName = $result['username'] ?? '';
$this->adminInfo = UserInfoCache::getUserInfo($adminId); $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']);
}
/** /**
* 验证器调用 * 验证器调用
*/ */