feat: 我的、设置、打印指南页面开发完成

This commit is contained in:
R524809
2026-05-07 16:32:49 +08:00
parent 0737c6165e
commit c732111e12
43 changed files with 1702 additions and 837 deletions
+60 -6
View File
@@ -19,18 +19,40 @@ let currentUser: UserInfo | null = null;
/** 登录进行中的 Promise(防止并发重复调用) */
let loginPromise: Promise<UserInfo> | null = null;
function syncAppGlobalUser(user: UserInfo | null): void {
try {
const app = getApp<IAppOption>();
if (app?.globalData) {
app.globalData.user = user;
}
} catch {
// App 尚未初始化时忽略
}
}
function syncUserState(user: UserInfo, persist = true): UserInfo {
currentUser = user;
if (persist) {
saveToStorage(user);
}
syncAppGlobalUser(user);
return user;
}
/**
* 获取当前用户,优先内存 → Storage → 云函数登录
*/
export async function getUser(): Promise<UserInfo> {
// 1. 内存中有,直接返回
if (currentUser) return currentUser;
if (currentUser) {
syncAppGlobalUser(currentUser);
return currentUser;
}
// 2. 尝试从 Storage 读取
const cached = loadFromStorage();
if (cached) {
currentUser = cached;
return cached;
return syncUserState(cached, false);
}
// 3. 静默登录(防止并发)
@@ -56,9 +78,18 @@ async function silentLogin(): Promise<UserInfo> {
}
const user = result.data as UserInfo;
currentUser = user;
saveToStorage(user);
return user;
return syncUserState(user);
}
export function getCachedUser(): UserInfo | null {
if (currentUser) {
syncAppGlobalUser(currentUser);
return currentUser;
}
const cached = loadFromStorage();
if (!cached) return null;
return syncUserState(cached, false);
}
/**
@@ -96,6 +127,7 @@ function saveToStorage(user: UserInfo): void {
*/
export function clearUser(): void {
currentUser = null;
syncAppGlobalUser(null);
wx.removeStorageSync(STORAGE_KEY);
wx.removeStorageSync(STORAGE_EXPIRE_KEY);
}
@@ -108,4 +140,26 @@ export async function refreshUser(): Promise<UserInfo> {
return await silentLogin();
}
export async function updateUserProfile(payload: {
nickName?: string | null;
avatarUrl?: string | null;
}): Promise<UserInfo> {
await getUser();
const res = await wx.cloud.callFunction({
name: 'userLogin',
data: {
action: 'updateProfile',
...payload,
},
});
const result = (res?.result as any) || {};
if (result.code !== 0 || !result.data) {
throw new Error(result.message || '更新用户资料失败');
}
return syncUserState(result.data as UserInfo);
}
export type { UserInfo };