76 lines
2.0 KiB
TypeScript
76 lines
2.0 KiB
TypeScript
import { request } from '../../utils/request'
|
|
import { HoroscopeData } from '../../types/models'
|
|
|
|
const COLOR_MAP: Record<string, string> = {
|
|
红色: '#FF3B30',
|
|
橙色: '#FF9500',
|
|
黄色: '#FFCC00',
|
|
绿色: '#34C759',
|
|
蓝色: '#007AFF',
|
|
紫色: '#5856D6',
|
|
粉色: '#FF2D55',
|
|
白色: '#FFFFFF',
|
|
黑色: '#000000',
|
|
灰色: '#8E8E93',
|
|
}
|
|
|
|
Page({
|
|
data: {
|
|
horoscope: null as HoroscopeData | null,
|
|
statDate: '',
|
|
luckyColorHex: '#007AFF',
|
|
loading: true,
|
|
noBirth: false,
|
|
},
|
|
|
|
onShow() {
|
|
this.syncTabBar()
|
|
if (!this.data.horoscope) this.fetchHoroscope(0)
|
|
},
|
|
|
|
syncTabBar() {
|
|
const tabBar = (this as any).getTabBar && (this as any).getTabBar()
|
|
if (tabBar) tabBar.setData({ selected: 2 })
|
|
},
|
|
|
|
fetchHoroscope(retry: number) {
|
|
this.setData({ loading: true })
|
|
request<HoroscopeData>({ url: '/horoscope/today', showError: false })
|
|
.then((res) => {
|
|
this.setData({
|
|
horoscope: res,
|
|
statDate: this.formatStatDate(res.stat_date),
|
|
luckyColorHex: COLOR_MAP[res.lucky_color] || '#007AFF',
|
|
loading: false,
|
|
noBirth: false,
|
|
})
|
|
})
|
|
.catch((e: any) => {
|
|
const code = e && e.code
|
|
const details = (e && e.details) || []
|
|
const noBirth = details.some((d: string) => d.indexOf('生日') >= 0)
|
|
if (code === 10000 && noBirth) {
|
|
this.setData({ noBirth: true, loading: false })
|
|
} else if (code === 90300 && retry < 3) {
|
|
wx.showToast({ title: '运势生成中,请稍后重试', icon: 'none' })
|
|
setTimeout(() => this.fetchHoroscope(retry + 1), 4000)
|
|
} else {
|
|
this.setData({ loading: false })
|
|
}
|
|
})
|
|
},
|
|
|
|
formatStatDate(d: number): string {
|
|
const s = String(d)
|
|
return `${s.slice(0, 4)}.${s.slice(4, 6)}.${s.slice(6, 8)}`
|
|
},
|
|
|
|
onGoProfile() {
|
|
wx.switchTab({ url: '/pages/profile/profile' })
|
|
},
|
|
|
|
onGenerate() {
|
|
wx.navigateTo({ url: '/pages/ai-recommend/ai-recommend' })
|
|
},
|
|
})
|