feat: 我的、设置、打印指南页面开发完成
This commit is contained in:
@@ -1,31 +1,154 @@
|
||||
import { getTodayDownloadCount } from '../../utils/downloadPrint';
|
||||
import { getDownloadLogCount } from '../../utils/downloadLogs';
|
||||
import { getFavoriteCount } from '../../utils/favorites';
|
||||
import {
|
||||
getCachedUser,
|
||||
getUser,
|
||||
type UserInfo,
|
||||
updateUserProfile,
|
||||
} from '../../utils/auth';
|
||||
|
||||
Page({
|
||||
DEFAULT_AVATAR_URL: '/assets/imgs/doodle-head.png',
|
||||
data: {
|
||||
userName: '微信用户',
|
||||
avatarUrl: '',
|
||||
printCount: 0,
|
||||
favoriteCount: 0,
|
||||
version: '',
|
||||
isDevEnv: false,
|
||||
version: '3.0.0',
|
||||
showProfileEditor: false,
|
||||
draftNickName: '',
|
||||
draftAvatarUrl: '/assets/imgs/doodle-head.png',
|
||||
isSavingProfile: false,
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
const info = wx.getAccountInfoSync();
|
||||
const version = info.miniProgram.version || '开发版';
|
||||
const isDevEnv = info.miniProgram.envVersion === 'develop';
|
||||
this.setData({ version, isDevEnv });
|
||||
this.syncUserInfoFromApp();
|
||||
},
|
||||
|
||||
onShow() {
|
||||
this.refreshStats();
|
||||
this.syncUserInfoFromApp();
|
||||
void this.refreshStats();
|
||||
},
|
||||
|
||||
refreshStats() {
|
||||
const printCount = getTodayDownloadCount();
|
||||
// 收藏数待与收藏页数据源打通后接入
|
||||
const favoriteCount = 0;
|
||||
this.setData({ printCount, favoriteCount });
|
||||
async syncUserInfoFromApp() {
|
||||
const app = getApp<IAppOption>();
|
||||
const cachedUser = app.globalData.user || getCachedUser();
|
||||
|
||||
if (cachedUser) {
|
||||
this.applyUserInfo(cachedUser);
|
||||
}
|
||||
|
||||
try {
|
||||
const latestUser = await getUser();
|
||||
this.applyUserInfo(latestUser);
|
||||
} catch (error) {
|
||||
console.error('获取用户信息失败', error);
|
||||
}
|
||||
},
|
||||
|
||||
applyUserInfo(user: UserInfo) {
|
||||
const userName = user.nickName?.trim() || '微信用户';
|
||||
const avatarUrl =
|
||||
user.avatarUrl || (this as any).DEFAULT_AVATAR_URL || '';
|
||||
this.setData({
|
||||
userName,
|
||||
avatarUrl,
|
||||
draftNickName: userName === '微信用户' ? '' : userName,
|
||||
draftAvatarUrl: avatarUrl,
|
||||
});
|
||||
},
|
||||
|
||||
async refreshStats() {
|
||||
try {
|
||||
const [printCount, favoriteCount] = await Promise.all([
|
||||
getDownloadLogCount(),
|
||||
getFavoriteCount(),
|
||||
]);
|
||||
this.setData({ printCount, favoriteCount });
|
||||
} catch (e) {
|
||||
console.error('refreshStats failed', e);
|
||||
}
|
||||
},
|
||||
|
||||
onOpenProfileEditor() {
|
||||
this.setData({
|
||||
showProfileEditor: true,
|
||||
draftNickName:
|
||||
this.data.userName === '微信用户' ? '' : this.data.userName,
|
||||
draftAvatarUrl: this.data.avatarUrl,
|
||||
});
|
||||
},
|
||||
|
||||
onCloseProfileEditor() {
|
||||
if (this.data.isSavingProfile) return;
|
||||
this.setData({ showProfileEditor: false });
|
||||
},
|
||||
|
||||
onChooseAvatar(e: WechatMiniprogram.CustomEvent<{ avatarUrl?: string }>) {
|
||||
const avatarUrl = e.detail?.avatarUrl || '';
|
||||
if (!avatarUrl) return;
|
||||
this.setData({ draftAvatarUrl: avatarUrl });
|
||||
},
|
||||
|
||||
onNicknameInput(e: WechatMiniprogram.CustomEvent<{ value: string }>) {
|
||||
const nickName = (e.detail?.value || '').trim();
|
||||
this.setData({ draftNickName: nickName });
|
||||
},
|
||||
|
||||
async onSubmitProfile() {
|
||||
if (this.data.isSavingProfile) return;
|
||||
|
||||
const nickName = this.data.draftNickName.trim();
|
||||
if (!nickName) {
|
||||
wx.showToast({ title: '请输入昵称', icon: 'none' });
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.data.draftAvatarUrl) {
|
||||
wx.showToast({ title: '请选择头像', icon: 'none' });
|
||||
return;
|
||||
}
|
||||
|
||||
this.setData({ isSavingProfile: true });
|
||||
|
||||
try {
|
||||
const avatarUrl = await this.persistAvatarIfNeeded(
|
||||
this.data.draftAvatarUrl,
|
||||
);
|
||||
const user = await updateUserProfile({
|
||||
nickName,
|
||||
avatarUrl,
|
||||
});
|
||||
|
||||
this.applyUserInfo(user);
|
||||
this.setData({ showProfileEditor: false });
|
||||
wx.showToast({ title: '资料已更新', icon: 'success' });
|
||||
} catch (error) {
|
||||
console.error('更新用户资料失败', error);
|
||||
wx.showToast({ title: '更新失败,请稍后重试', icon: 'none' });
|
||||
} finally {
|
||||
this.setData({ isSavingProfile: false });
|
||||
}
|
||||
},
|
||||
|
||||
async persistAvatarIfNeeded(avatarUrl: string): Promise<string> {
|
||||
if (!avatarUrl) return avatarUrl;
|
||||
if (
|
||||
avatarUrl.startsWith('cloud://') ||
|
||||
avatarUrl.startsWith('http://') ||
|
||||
avatarUrl.startsWith('https://')
|
||||
) {
|
||||
return avatarUrl;
|
||||
}
|
||||
|
||||
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;
|
||||
},
|
||||
|
||||
onGoToDownloads() {
|
||||
@@ -47,7 +170,7 @@ Page({
|
||||
},
|
||||
|
||||
onGoToSettings() {
|
||||
wx.showToast({ title: '功能开发中', icon: 'none' });
|
||||
wx.navigateTo({ url: '/supportPages/settings/settings' });
|
||||
},
|
||||
|
||||
onGoToDebug() {
|
||||
|
||||
Reference in New Issue
Block a user