Files
wx_pbzc/pages/leaderboard/leaderboard.js
T
lc 2e2594d154 feat: 修复清除数据、语音缓存、排行榜限制,新增项目配置文件
- 修复 cloud.clearAll 静默失败导致重启后数据恢复
- 清除数据弹窗改用自定义 ui-modal 替代 wx.showModal
- TTS 语音合成增加 fileID 持久化缓存,重启不再重新合成
- 排行榜限制前15名,maxRank 参数化到 config.js
- 新增 config.js 统一管理版本号/更新日期/开发者/排行榜限制
- progress-ring 消除 getSystemInfoSync 弃用警告
- voice.js 增加 InnerAudioContext 错误监听
- storage/util 完善用户资料、打卡日期精度、记录日志
2026-06-11 11:30:47 +08:00

121 lines
3.4 KiB
JavaScript

const themeMod = require('../../utils/theme')
const iconsMod = require('../../utils/icons')
const util = require('../../utils/util')
const storage = require('../../utils/storage')
const config = require('../../config')
Page({
data: {
theme: { primary: '#FF6B35', primaryLight: '#FF8C5A', primaryBg: '#FFF3ED', primaryRgb: '255,107,53' },
themeStyle: themeMod.BASE_VARS,
periods: [
{ key: 'day', label: '日榜' },
{ key: 'month', label: '月榜' },
{ key: 'year', label: '年榜' }
],
activePeriod: 'day',
rankedList: [],
myEntry: null,
loading: false,
empty: false,
icons: iconsMod.build()
},
onLoad() {
themeMod.applyThemeToPage(this)
this._firstShow = true
},
onShow() {
themeMod.applyThemeToPage(this)
const theme = themeMod.getCurrentTheme()
this.setData({ theme, icons: iconsMod.build(theme) })
try {
const tb = this.getTabBar()
if (tb) tb.setData({ selected: 2 })
} catch (e) {}
if (this._firstShow) {
this._firstShow = false
this.fetchRank()
return
}
this.fetchRank()
},
onPullDownRefresh() {
this.fetchRank(() => wx.stopPullDownRefresh())
},
switchPeriod(e) {
const period = e.currentTarget.dataset.period
if (period === this.data.activePeriod) return
this.setData({ activePeriod: period, rankedList: [], myEntry: null })
this.fetchRank()
},
fetchRank(cb) {
this.setData({ loading: true, empty: false })
wx.cloud.callFunction({
name: 'leaderboard',
data: {
period: this.data.activePeriod,
maxRank: config.leaderboardMaxRank
}
}).then(res => {
this._applyResult(res.result || {})
if (cb) cb()
}).catch(() => {
// Cloud function not deployed — fall back to local data
this._applyLocal()
if (cb) cb()
})
},
_applyResult(result) {
const list = (result.ranked || []).map(item => ({
...item,
durationText: util.formatDuration(item.duration)
}))
const myEntry = result.myEntry ? {
...result.myEntry,
durationText: util.formatDuration(result.myEntry.duration),
isMe: true
} : null
this.setData({
rankedList: list,
myEntry,
loading: false,
empty: list.length === 0 && !myEntry
})
},
/** Local fallback: use local storage when cloud function isn't deployed */
_applyLocal() {
const period = this.data.activePeriod
const allRecords = Object.values(storage.getRecords()).flat()
const today = storage.getToday()
const thisMonth = today.substring(0, 7)
const thisYear = String(new Date().getFullYear())
const profile = storage.getProfile()
const localName = profile.nickname || '我'
let duration = 0
let sessions = 0
allRecords.forEach(r => {
if (period === 'day' && r.date === today) { duration += r.duration; sessions++ }
if (period === 'month' && r.date.startsWith(thisMonth)) { duration += r.duration; sessions++ }
if (period === 'year' && r.date.startsWith(thisYear)) { duration += r.duration; sessions++ }
})
if (duration > 0) {
const entry = {
rank: 1, openid: 'local', name: localName, nickname: profile.nickname || '',
duration, durationText: util.formatDuration(duration), sessions
}
this.setData({ rankedList: [entry], myEntry: { ...entry, isMe: true }, loading: false, empty: false })
} else {
this.setData({ loading: false, empty: true })
}
}
})