const app = getApp(); const auth = require('../../utils/auth'); const favSvc = require('../../services/favorite'); const planSvc = require('../../services/plan'); const EMOJIS = ['💪', '🏋️', '🔥', '🏃', '🧘', '🤸', '🥊', '🚴', '⚡', '🌟', '🐯', '🦁']; Page({ data: { statusBarHeight: 20, loggedIn: false, profile: { nickname: '', avatar: '💪' }, counts: { fav: 0, plans: 0, favPlans: 0 }, loading: false, // 编辑态 showEmojiPicker: false, emojis: EMOJIS, draftNickname: '', draftAvatar: '💪' }, onLoad() { this.setData({ statusBarHeight: app.globalData.statusBarHeight || 20 }); }, onShow() { this.loadData(); }, loadData() { if (!auth.isLogin()) { this.setData({ loggedIn: false }); return; } const profile = auth.getProfile(); this.setData({ loggedIn: true, profile, loading: true }); Promise.all([ favSvc.listExercises().catch(() => []), planSvc.listMine().catch(() => []), favSvc.listPlans().catch(() => []) ]).then(([fav, plans, favPlans]) => { this.setData({ counts: { fav: (fav || []).length, plans: (plans || []).length, favPlans: (favPlans || []).length }, loading: false }); }).catch(() => { this.setData({ loading: false }); }); }, onLogin() { auth.login().then(() => { this.loadData(); wx.showToast({ title: '登录成功', icon: 'success' }); }).catch(() => {}); }, onEdit() { const cur = this.data.profile; wx.showModal({ title: '修改昵称', editable: true, placeholderText: '输入昵称', content: cur.nickname || '', success: (res) => { if (res.confirm) { this.setData({ draftNickname: (res.content || '').trim() || cur.nickname, draftAvatar: cur.avatar || '💪', showEmojiPicker: true }); } } }); }, onPickEmoji(e) { this.setData({ draftAvatar: e.currentTarget.dataset.emoji }); }, onCancelEdit() { this.setData({ showEmojiPicker: false }); }, onConfirmEdit() { const nickname = (this.data.draftNickname || '').trim(); const avatar = this.data.draftAvatar; if (!nickname) { wx.showToast({ title: '昵称不能为空', icon: 'none' }); return; } wx.showLoading({ title: '保存中…', mask: true }); auth.updateProfile({ nickname, avatar }).then((p) => { wx.hideLoading(); this.setData({ profile: p, showEmojiPicker: false }); wx.showToast({ title: '已保存', icon: 'success' }); }).catch(() => { wx.hideLoading(); }); }, onLogout() { wx.showModal({ title: '退出登录', content: '确定要退出当前账号吗?', success: (res) => { if (res.confirm) { auth.logout(); this.setData({ loggedIn: false, profile: { nickname: '', avatar: '💪' }, counts: { fav: 0, plans: 0, favPlans: 0 } }); } } }); }, goFav() { wx.navigateTo({ url: '/pages/favorites/favorites' }); }, goPlans() { wx.navigateTo({ url: '/pages/my-plans/my-plans' }); }, goFavPlans() { wx.navigateTo({ url: '/pages/plan-favorites/plan-favorites' }); }, goOfficial() { wx.navigateTo({ url: '/pages/collection/collection' }); }, noop() {} });