fix: 登录用户换设备后头像同步,初始化从后台获取最新用户信息
This commit is contained in:
37
www/app.js
37
www/app.js
@@ -321,15 +321,41 @@ document.addEventListener('DOMContentLoaded', async () => {
|
|||||||
// 初始化 appContainer
|
// 初始化 appContainer
|
||||||
appContainer = document.getElementById('app');
|
appContainer = document.getElementById('app');
|
||||||
|
|
||||||
|
// 加载后台配置(包含 LLM、智能体等)
|
||||||
|
await loadBackendConfig();
|
||||||
|
|
||||||
// 加载用户登录状态(优先检查)
|
// 加载用户登录状态(优先检查)
|
||||||
const savedUser = localStorage.getItem('currentUser');
|
const savedUser = localStorage.getItem('currentUser');
|
||||||
if (savedUser) {
|
if (savedUser) {
|
||||||
currentUser = JSON.parse(savedUser);
|
currentUser = JSON.parse(savedUser);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果用户已登录且有ID,从 backend 加载对话数据
|
// 如果用户已登录且有ID,从 backend 加载最新用户信息和对话数据
|
||||||
if (currentUser && currentUser.id) {
|
if (currentUser && currentUser.id) {
|
||||||
try {
|
try {
|
||||||
|
// 从后台获取最新用户信息(包括头像)
|
||||||
|
const userRes = await fetch(`/api/user/${currentUser.id}`);
|
||||||
|
if (userRes.ok) {
|
||||||
|
const userData = await userRes.json();
|
||||||
|
if (userData && userData.id) {
|
||||||
|
// 更新 currentUser 为后台最新数据
|
||||||
|
currentUser = {
|
||||||
|
id: userData.id,
|
||||||
|
username: userData.username,
|
||||||
|
phone: userData.phone || '',
|
||||||
|
email: userData.email || '',
|
||||||
|
avatar: userData.avatar || '👤',
|
||||||
|
signature: userData.signature || '',
|
||||||
|
gender: userData.gender || '',
|
||||||
|
age: userData.age || '',
|
||||||
|
region: userData.region || '',
|
||||||
|
registeredAt: Date.now()
|
||||||
|
};
|
||||||
|
saveCurrentUser();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 从 backend 加载对话数据
|
||||||
const res = await fetch(`/api/user/${currentUser.id}/conversations`);
|
const res = await fetch(`/api/user/${currentUser.id}/conversations`);
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
if (Array.isArray(data) && data.length > 0) {
|
if (Array.isArray(data) && data.length > 0) {
|
||||||
@@ -437,13 +463,8 @@ document.addEventListener('DOMContentLoaded', async () => {
|
|||||||
currentPage = savedPage;
|
currentPage = savedPage;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 加载后台配置并显示主页
|
// 显示主页
|
||||||
loadBackendConfig().then(() => {
|
showMainPage();
|
||||||
showMainPage();
|
|
||||||
}).catch(() => {
|
|
||||||
// 即使加载失败也显示主页
|
|
||||||
showMainPage();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// 根据用户配置更新显示的智能体
|
// 根据用户配置更新显示的智能体
|
||||||
|
|||||||
Reference in New Issue
Block a user