feat: 完成全部接口替换为新后端接口
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { uploadAsset, memberStats } from '../../services/member-api';
|
||||
import { getUser as getServerUser, startAccount } from '../../utils/auth';
|
||||
import { disablePageShareMenu } from '../../base/pageMixin';
|
||||
import { APP_VERSION } from '../../config/config';
|
||||
import { getDownloadLogCount } from '../../utils/downloadLogs';
|
||||
import { getFavoriteCount } from '../../utils/favorites';
|
||||
import {
|
||||
getCachedUser,
|
||||
getUser,
|
||||
@@ -22,6 +22,8 @@ Page({
|
||||
draftNickName: '',
|
||||
draftAvatarUrl: '/assets/imgs/doodle-head.png',
|
||||
isSavingProfile: false,
|
||||
serverAccountActionRequired: false,
|
||||
isStartingAccount: false,
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
@@ -35,17 +37,52 @@ Page({
|
||||
},
|
||||
|
||||
onShow() {
|
||||
void this.checkServerAccount();
|
||||
this.syncUserInfoFromApp();
|
||||
void this.refreshStats();
|
||||
},
|
||||
|
||||
/** 正常登录始终静默;账户不存在时才展示用户主动重新开始的入口。 */
|
||||
async checkServerAccount() {
|
||||
try {
|
||||
await getServerUser();
|
||||
this.setData({ serverAccountActionRequired: false });
|
||||
} catch (error) {
|
||||
const reason = (error as { reason?: string }).reason;
|
||||
this.setData({
|
||||
serverAccountActionRequired:
|
||||
reason === 'ACCOUNT_NOT_FOUND' ||
|
||||
reason === 'ACCOUNT_RESTART_REQUIRED',
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
async onStartServerAccount() {
|
||||
if (this.data.isStartingAccount) return;
|
||||
this.setData({ isStartingAccount: true });
|
||||
try {
|
||||
await startAccount();
|
||||
this.setData({ serverAccountActionRequired: false });
|
||||
wx.showToast({ title: '账户已就绪', icon: 'success' });
|
||||
} catch {
|
||||
wx.showToast({ title: '账户暂不可用,请稍后重试', icon: 'none' });
|
||||
} finally {
|
||||
this.setData({ isStartingAccount: false });
|
||||
}
|
||||
},
|
||||
|
||||
async syncUserInfoFromApp() {
|
||||
const app = getApp<IAppOption>();
|
||||
const cachedUser = app.globalData.user || getCachedUser();
|
||||
|
||||
if (cachedUser) {
|
||||
this.applyUserInfo(cachedUser);
|
||||
}
|
||||
if (cachedUser) this.applyUserInfo(cachedUser);
|
||||
else
|
||||
this.setData({
|
||||
userName: '微信用户',
|
||||
avatarUrl: this.DEFAULT_AVATAR_URL,
|
||||
printCount: 0,
|
||||
favoriteCount: 0,
|
||||
});
|
||||
|
||||
try {
|
||||
const latestUser = await getUser();
|
||||
@@ -69,13 +106,11 @@ Page({
|
||||
|
||||
async refreshStats() {
|
||||
try {
|
||||
const [printCount, favoriteCount] = await Promise.all([
|
||||
getDownloadLogCount(),
|
||||
getFavoriteCount(),
|
||||
]);
|
||||
const stats = await memberStats();
|
||||
const { downloadCount: printCount, favoriteCount } = stats;
|
||||
this.setData({ printCount, favoriteCount });
|
||||
} catch (e) {
|
||||
console.error('refreshStats failed', e);
|
||||
} catch {
|
||||
this.setData({ printCount: 0, favoriteCount: 0 });
|
||||
}
|
||||
},
|
||||
|
||||
@@ -121,12 +156,12 @@ Page({
|
||||
this.setData({ isSavingProfile: true });
|
||||
|
||||
try {
|
||||
const avatarUrl = await this.persistAvatarIfNeeded(
|
||||
const avatarAssetId = await this.persistAvatarIfNeeded(
|
||||
this.data.draftAvatarUrl,
|
||||
);
|
||||
const user = await updateUserProfile({
|
||||
nickName,
|
||||
avatarUrl,
|
||||
avatarAssetId,
|
||||
});
|
||||
|
||||
this.applyUserInfo(user);
|
||||
@@ -140,24 +175,11 @@ Page({
|
||||
}
|
||||
},
|
||||
|
||||
async persistAvatarIfNeeded(avatarUrl: string): Promise<string> {
|
||||
if (!avatarUrl) return avatarUrl;
|
||||
if (
|
||||
avatarUrl.startsWith('cloud://') ||
|
||||
avatarUrl.startsWith('http://') ||
|
||||
avatarUrl.startsWith('https://')
|
||||
) {
|
||||
return avatarUrl;
|
||||
}
|
||||
|
||||
async persistAvatarIfNeeded(avatarUrl: string): Promise<string | null> {
|
||||
const user = await getUser();
|
||||
const cloudPath = `user-avatars/${user._id}-${Date.now()}.png`;
|
||||
const uploadRes = await wx.cloud.uploadFile({
|
||||
cloudPath,
|
||||
filePath: avatarUrl,
|
||||
});
|
||||
|
||||
return uploadRes.fileID;
|
||||
if (!avatarUrl || avatarUrl === this.DEFAULT_AVATAR_URL) return null;
|
||||
if (avatarUrl === user.avatarUrl) return user.avatarAssetId || null;
|
||||
return (await uploadAsset(avatarUrl, 'avatar')).id;
|
||||
},
|
||||
|
||||
onGoToDownloads() {
|
||||
|
||||
Reference in New Issue
Block a user