import { request } from '../../utils/request' import * as auth from '../../utils/auth' import { STORAGE_KEYS, QuotaType } from '../../utils/config' import { LoginResult, UserInfo, QuotaItem } from '../../types/models' import { formatDate } from '../../utils/format' interface GiftItem { icon: string name: string value: string } Page({ data: { loading: false, userInfo: {} as UserInfo, giftVisible: false, giftItems: [] as GiftItem[], }, onLoad() { if (auth.isLoggedIn()) { wx.switchTab({ url: '/pages/wardrobe/wardrobe' }) } }, onLogin() { if (this.data.loading) return this.setData({ loading: true }) wx.login({ success: (res) => { if (!res.code) { this.setData({ loading: false }) wx.showToast({ title: '登录失败,请重试', icon: 'none' }) return } request({ url: '/user/wxlogin', method: 'POST', data: { code: res.code }, auth: false, }) .then((data) => { auth.setTokens({ accessToken: data.access_token, refreshToken: data.refresh_token, expireTime: data.expire_time, }) wx.setStorageSync(STORAGE_KEYS.USER_INFO, data.user_info) // 新登录用户重置未读红点,进入 tab 页后由 tabBar 拉取真实值 wx.setStorageSync(STORAGE_KEYS.UNREAD, 0) this.setData({ userInfo: data.user_info }) if (data.user_info.is_new_user) { this.showWelcomeGift(data.user_info) } else if (!data.user_info.birth_day) { this.showBirthGuide() } else { wx.switchTab({ url: '/pages/wardrobe/wardrobe' }) } }) .catch(() => { this.setData({ loading: false }) }) }, fail: () => { this.setData({ loading: false }) wx.showToast({ title: '登录失败,请重试', icon: 'none' }) }, }) }, showWelcomeGift(userInfo: UserInfo) { request<{ quotas: QuotaItem[] }>({ url: '/quota/info', showError: false }) .then((res) => this.setData({ giftItems: this.buildGift(userInfo, res.quotas), giftVisible: true })) .catch(() => this.setData({ giftItems: this.buildGift(userInfo, []), giftVisible: true })) }, buildGift(userInfo: UserInfo, quotas: QuotaItem[]): GiftItem[] { const total = (t: number) => { const q = quotas.find((it) => it.quota_type === t) return q ? q.total : 0 } const items: GiftItem[] = [] if (userInfo.vip_type === 4 && userInfo.vip_expire) { items.push({ icon: 'icon-crown', name: '体验会员', value: `有效期至 ${formatDate(userInfo.vip_expire)}` }) } items.push({ icon: 'icon-camera', name: '上传额度', value: `${total(QuotaType.UPLOAD)} 次 · 永久` }) items.push({ icon: 'icon-sparkles', name: 'AI 推荐额度', value: `${total(QuotaType.AI)} 次/天` }) items.push({ icon: 'icon-shirt', name: '搭配分享额度', value: `${total(QuotaType.OUTFIT_SHARE)} 次/天` }) items.push({ icon: 'icon-hanger', name: '衣柜分享额度', value: `${total(QuotaType.CLOSET_SHARE)} 次/天` }) return items }, onGiftConfirm() { this.setData({ giftVisible: false }) if (!this.data.userInfo.birth_day) { this.showBirthGuide() } else { wx.switchTab({ url: '/pages/wardrobe/wardrobe' }) } }, showBirthGuide() { wx.showModal({ title: '完善个人信息', content: '设置生日后即可查看今日星座运势', confirmText: '去完善', cancelText: '稍后再说', success: (res) => { if (res.confirm) wx.switchTab({ url: '/pages/profile/profile' }) else wx.switchTab({ url: '/pages/wardrobe/wardrobe' }) }, }) }, openAgreement() { wx.showToast({ title: '协议内容敬请期待', icon: 'none' }) }, })