Files
Pet3/miniprogram/pages/achievements/achievements.js
2026-06-25 18:24:29 +08:00

61 lines
2.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// pages/achievements/achievements — 7 完整成就页两入口共用docs/05 R10
const { call } = require('../../utils/cloud.js');
const { ACHIEVEMENTS, TOTAL } = require('../../utils/achievements.js');
const { toDateStr } = require('../../utils/format.js');
Page({
data: {
dogId: '',
dogName: '',
unlockedCount: 0,
total: TOTAL,
unlockedList: [],
lockedList: [],
},
onLoad(o) { this.dogId = o.dogId || ''; },
onShow() { if (this.dogId) this.load(); },
async load() {
const res = await call('getDogDetail', { dogId: this.dogId });
if (!res.ok) return;
const dog = res.data.dog;
const stats = dog.stats || { totalWalks: 0, totalDistance: 0, currentStreak: 0 };
const unlockedAtMap = {};
res.data.achievements.unlocked.forEach((u) => { unlockedAtMap[u.achievementKey] = u.unlockedAt; });
const unlockedList = [];
const lockedList = [];
ACHIEVEMENTS.forEach((a) => {
if (unlockedAtMap[a.key] != null) {
unlockedList.push({ key: a.key, name: a.name, desc: a.desc, when: toDateStr(unlockedAtMap[a.key]) });
} else {
lockedList.push({ key: a.key, name: a.name, desc: this.lockedDesc(a, stats) });
}
});
this.setData({
dogName: dog.name,
unlockedCount: unlockedList.length,
total: res.data.achievements.total,
unlockedList,
lockedList,
});
},
// 「还差 X」进度文案
lockedDesc(a, stats) {
const streak = stats.currentStreak || 0;
const dist = stats.totalDistance || 0;
const early = stats.earlyWalks || 0;
switch (a.key) {
case 'week_streak': return `连续遛狗满 7 天 · 还差 ${Math.max(0, 7 - streak)}`;
case 'month_streak': return `连续遛狗满 30 天 · 还差 ${Math.max(0, 30 - streak)}`;
case 'hundred_km': return `累计满 100 公里 · 还差 ${Math.max(0, +(100 - dist).toFixed(1))} km`;
case 'early_bird': return `7 点前遛狗 10 次 · 还差 ${Math.max(0, 10 - early)}`;
default: return a.desc;
}
},
});