// pages/badges/badges.js const userStore = require('../../userStore.js'); Page({ data: { points: 0, badges: [] }, onLoad() { this.refresh(); }, // 每次显示都刷新一次,保证从其他页面打卡回来后数据是最新的 onShow() { this.refresh(); }, refresh() { const data = userStore.getUserData(); // 将徽章数据处理为渲染友好的结构(格式化获得时间、补默认图标) const badges = (data.badges || []).map(b => ({ name: b.name, icon: this.getBadgeIcon(b.name), obtainedAtText: this.formatTime(b.obtainedAt) })); this.setData({ points: data.points || 0, badges }); }, // 根据徽章名称返回对应的 emoji 图标(也可换成图片路径) getBadgeIcon(name) { const map = { '探索者': '🧭', '旅行家': '🌍', '打卡达人': '🏆' }; return map[name] || '🎖️'; }, // 格式化时间戳为 "YYYY-MM-DD HH:mm" formatTime(ts) { if (!ts) return ''; const d = new Date(ts); const pad = n => (n < 10 ? '0' + n : '' + n); return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}`; } });