Files
fitness-coach/miniprogram/pages/profile/profile.js
wm 70ff806042 feat: 小程序连接服务器IP + 同步未提交开发
- miniprogram/config.js: BASE_URL 指向 http://192.227.237.8:3001
- 动作标题中文化(nameZh),搜索匹配 name/nameZh/nameEn
- 新增计划/收藏/我的 等页面与组件
- 后端 Docker 化(Dockerfile/.dockerignore)与翻译脚本
- .gitignore 补充 .DS_Store
2026-07-22 14:05:30 +08:00

130 lines
3.4 KiB
JavaScript

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() {}
});