feat: 我的、设置、打印指南页面开发完成
This commit is contained in:
@@ -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 };
|
||||
|
||||
@@ -54,3 +54,21 @@ export async function getDownloadLogs(
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户下载(打印)总数
|
||||
*/
|
||||
export async function getDownloadLogCount(): Promise<number> {
|
||||
try {
|
||||
await getUser();
|
||||
const res = await wx.cloud.callFunction({
|
||||
name: 'userDownloadLogs',
|
||||
data: { action: 'count' },
|
||||
});
|
||||
const result = (res?.result as any) || {};
|
||||
return result.code === 0 ? Number(result.data?.count || 0) : 0;
|
||||
} catch (e) {
|
||||
console.error('getDownloadLogCount failed', e);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,6 +76,24 @@ export async function getFavoriteList(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户收藏总数
|
||||
*/
|
||||
export async function getFavoriteCount(): Promise<number> {
|
||||
try {
|
||||
await getUser();
|
||||
const res = await wx.cloud.callFunction({
|
||||
name: 'userFavorites',
|
||||
data: { action: 'count' },
|
||||
});
|
||||
const result = (res?.result as any) || {};
|
||||
return result.code === 0 ? Number(result.data?.count || 0) : 0;
|
||||
} catch (e) {
|
||||
console.error('getFavoriteCount failed', e);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否已收藏
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user