From c16c6b63cf52de6540da1d4aa5705c764d0d541d Mon Sep 17 00:00:00 2001 From: shaw Date: Wed, 16 Jul 2025 17:26:02 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E8=8F=9C=E5=8D=95=E6=98=BE=E7=A4=BA=E9=97=AE=E9=A2=98=E5=92=8C?= =?UTF-8?q?=E7=9C=9F=E5=AE=9E=E7=94=A8=E6=88=B7=E5=90=8D=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修复登录接口返回真实用户名而非输入用户名 - 新增获取当前用户信息的API接口(/web/auth/user) - 修复前端用户名显示逻辑,页面初始化时获取真实用户名 - 提高下拉菜单z-index确保正确显示 - 解决用户名显示为Admin而非data/init.json中真实用户名的问题 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- src/routes/web.js | 51 +++++++++++++++++++++++++++++++++++++++++++- web/admin/app.js | 27 +++++++++++++++++++++-- web/admin/index.html | 3 ++- 3 files changed, 77 insertions(+), 4 deletions(-) diff --git a/src/routes/web.js b/src/routes/web.js index 97b8bc6c..8de87952 100644 --- a/src/routes/web.js +++ b/src/routes/web.js @@ -114,7 +114,8 @@ router.post('/auth/login', async (req, res) => { res.json({ success: true, token: sessionId, - expiresIn: config.security.adminSessionTimeout + expiresIn: config.security.adminSessionTimeout, + username: adminData.username // 返回真实用户名 }); } catch (error) { @@ -252,6 +253,54 @@ router.post('/auth/change-password', async (req, res) => { } }); +// 👤 获取当前用户信息 +router.get('/auth/user', async (req, res) => { + try { + const token = req.headers['authorization']?.replace('Bearer ', '') || req.cookies?.adminToken; + + if (!token) { + return res.status(401).json({ + error: 'No token provided', + message: 'Authentication required' + }); + } + + // 获取当前会话 + const sessionData = await redis.getSession(token); + if (!sessionData) { + return res.status(401).json({ + error: 'Invalid token', + message: 'Session expired or invalid' + }); + } + + // 获取管理员信息 + const adminData = await redis.getSession('admin_credentials'); + if (!adminData) { + return res.status(500).json({ + error: 'Admin data not found', + message: 'Administrator credentials not found' + }); + } + + res.json({ + success: true, + user: { + username: adminData.username, + loginTime: sessionData.loginTime, + lastActivity: sessionData.lastActivity + } + }); + + } catch (error) { + logger.error('❌ Get user info error:', error); + res.status(500).json({ + error: 'Get user info failed', + message: 'Internal server error' + }); + } +}); + // 🔄 刷新token router.post('/auth/refresh', async (req, res) => { try { diff --git a/web/admin/app.js b/web/admin/app.js index b358c66e..6150aa74 100644 --- a/web/admin/app.js +++ b/web/admin/app.js @@ -228,6 +228,9 @@ const app = createApp({ if (this.authToken) { this.isLoggedIn = true; + // 加载当前用户信息 + this.loadCurrentUser(); + // 初始化日期筛选器和图表数据 this.initializeDateFilter(); @@ -808,8 +811,8 @@ const app = createApp({ localStorage.setItem('authToken', this.authToken); this.isLoggedIn = true; - // 记录当前用户名 - this.currentUser.username = this.loginForm.username; + // 记录当前用户名(使用服务器返回的真实用户名) + this.currentUser.username = data.username; this.loadDashboard(); } else { @@ -823,6 +826,26 @@ const app = createApp({ } }, + // 加载当前用户信息 + async loadCurrentUser() { + try { + const response = await fetch('/web/auth/user', { + headers: { 'Authorization': 'Bearer ' + this.authToken } + }); + + const data = await response.json(); + + if (data.success) { + this.currentUser.username = data.user.username; + console.log('Loaded current user:', data.user.username); + } else { + console.warn('Failed to load current user:', data.message); + } + } catch (error) { + console.error('Error loading current user:', error); + } + }, + // 用户菜单相关方法 openChangePasswordModal() { this.userMenuOpen = false; diff --git a/web/admin/index.html b/web/admin/index.html index 66d655d3..a41f0f08 100644 --- a/web/admin/index.html +++ b/web/admin/index.html @@ -97,7 +97,8 @@