bigfix
This commit is contained in:
@@ -15,6 +15,13 @@ const COLOR_MAP: Record<string, string> = {
|
||||
灰色: '#8E8E93',
|
||||
}
|
||||
|
||||
// 今日日期(YYYYMMDD),用于判断运势数据是否跨天
|
||||
function todayStatDate(): number {
|
||||
const d = new Date()
|
||||
const pad = (n: number) => (n < 10 ? `0${n}` : String(n))
|
||||
return Number(`${d.getFullYear()}${pad(d.getMonth() + 1)}${pad(d.getDate())}`)
|
||||
}
|
||||
|
||||
Page({
|
||||
data: {
|
||||
horoscope: null as HoroscopeData | null,
|
||||
@@ -22,6 +29,8 @@ Page({
|
||||
luckyColorHex: '#007AFF',
|
||||
loading: true,
|
||||
noBirth: false,
|
||||
error: false,
|
||||
errorText: '',
|
||||
},
|
||||
|
||||
onShow() {
|
||||
@@ -31,7 +40,10 @@ Page({
|
||||
redirectToLogin()
|
||||
return
|
||||
}
|
||||
if (!this.data.horoscope) this.fetchHoroscope(0)
|
||||
// 跨天回到本页时旧数据仍是昨天的,需重新拉取
|
||||
const stale =
|
||||
!this.data.horoscope || Number(this.data.horoscope.stat_date) !== todayStatDate()
|
||||
if (stale) this.fetchHoroscope(0)
|
||||
},
|
||||
|
||||
syncTabBar() {
|
||||
@@ -40,7 +52,8 @@ Page({
|
||||
},
|
||||
|
||||
fetchHoroscope(retry: number) {
|
||||
this.setData({ loading: true })
|
||||
this.clearRetryTimer()
|
||||
this.setData({ loading: true, error: false, errorText: '' })
|
||||
request<HoroscopeData>({ url: '/horoscope/today', showError: false })
|
||||
.then((res) => {
|
||||
this.setData({
|
||||
@@ -49,6 +62,7 @@ Page({
|
||||
luckyColorHex: COLOR_MAP[res.lucky_color] || '#007AFF',
|
||||
loading: false,
|
||||
noBirth: false,
|
||||
error: false,
|
||||
})
|
||||
})
|
||||
.catch((e: any) => {
|
||||
@@ -59,13 +73,31 @@ Page({
|
||||
this.setData({ noBirth: true, loading: false })
|
||||
} else if (code === 90300 && retry < 3) {
|
||||
wx.showToast({ title: '运势生成中,请稍后重试', icon: 'none' })
|
||||
setTimeout(() => this.fetchHoroscope(retry + 1), 4000)
|
||||
// 定时器挂实例并在页面卸载时清理,避免离开页面后继续请求
|
||||
;(this as any).retryTimer = setTimeout(() => this.fetchHoroscope(retry + 1), 4000)
|
||||
} else {
|
||||
this.setData({ loading: false })
|
||||
// 失败必须有可见反馈:否则会渲染一张数据为 null 的空卡
|
||||
this.setData({
|
||||
loading: false,
|
||||
error: true,
|
||||
errorText: (e && e.message) || '运势加载失败,请重试',
|
||||
})
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
clearRetryTimer() {
|
||||
const timer = (this as any).retryTimer
|
||||
if (timer) {
|
||||
clearTimeout(timer)
|
||||
;(this as any).retryTimer = null
|
||||
}
|
||||
},
|
||||
|
||||
onRetry() {
|
||||
this.fetchHoroscope(0)
|
||||
},
|
||||
|
||||
formatStatDate(d: number): string {
|
||||
const s = String(d)
|
||||
return `${s.slice(0, 4)}.${s.slice(4, 6)}.${s.slice(6, 8)}`
|
||||
@@ -78,4 +110,8 @@ Page({
|
||||
onGenerate() {
|
||||
wx.navigateTo({ url: '/pages/ai-recommend/ai-recommend' })
|
||||
},
|
||||
|
||||
onUnload() {
|
||||
this.clearRetryTimer()
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user