修复优化后台报错

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
{
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']);

View File

@@ -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']);
}
/**
* 验证器调用
*/