const couponApi = require('../../api/coupon') const auth = require('../../utils/auth') const navigation = require('../../utils/navigation') const theme = require('../../utils/theme') function getBenefitText(coupon) { if (Number(coupon.couponType) === 1) { return `${coupon.discountValue} 折` } return `满 ${coupon.minAmount} 减 ${coupon.reduceAmount}` } /** * coupons page logic. * Call guide: page data drives the WXML template, and each method is called by user events or lifecycle callbacks. */ Page({ data: Object.assign(theme.pageThemeData(getApp().globalData.shopTheme), { loading: false, isLoggedIn: false, coupons: [], filteredCoupons: [], activeFilter: 'all', filterOptions: [ { value: 'all', label: '全部' }, { value: 'available', label: '未使用' }, { value: 'used', label: '已使用' }, { value: 'expired', label: '已过期' } ] }), /** * `onShow` page method. Use this from WXML event bindings or lifecycle hooks. * Call guide: read or update page data with `this.setData(...)` inside the method when state changes. */ onShow() { this.loadCoupons() }, async loadCoupons() { const isLoggedIn = auth.isLoggedIn() this.setData({ isLoggedIn, loading: isLoggedIn }) /** * `if` page method. Use this from WXML event bindings or lifecycle hooks. * Call guide: read or update page data with `this.setData(...)` inside the method when state changes. */ if (!isLoggedIn) { this.setData({ coupons: [], filteredCoupons: [], activeFilter: 'all', loading: false }) return } try { const coupons = await couponApi.listUserCoupons() const list = Array.isArray(coupons) ? coupons.map(item => ({ ...item, benefitText: getBenefitText(item), periodText: `${String(item.startTime || '').slice(0, 10)} - ${String(item.endTime || '').slice(0, 10)}`, sourceText: item.sourceType === 'SYSTEM_VIP' ? 'VIP 自动匹配' : '用户优惠券' })) : [] this.setData({ coupons: list }) this.applyFilter(this.data.activeFilter, list) } catch (error) { this.setData({ coupons: [], filteredCoupons: [] }) wx.showToast({ title: error.message || '优惠券加载失败', icon: 'none' }) } finally { this.setData({ loading: false }) } }, /** * `applyFilter` page method. Use this from WXML event bindings or lifecycle hooks. * Call guide: read or update page data with `this.setData(...)` inside the method when state changes. */ applyFilter(filterValue, sourceCoupons) { const activeFilter = filterValue || 'all' const coupons = Array.isArray(sourceCoupons) ? sourceCoupons : this.data.coupons let filteredCoupons = coupons /** * `if` page method. Use this from WXML event bindings or lifecycle hooks. * Call guide: read or update page data with `this.setData(...)` inside the method when state changes. */ if (activeFilter === 'available') { filteredCoupons = coupons.filter(item => Number(item.status) === 1) } else if (activeFilter === 'used') { filteredCoupons = coupons.filter(item => Number(item.status) === 2) } else if (activeFilter === 'expired') { filteredCoupons = coupons.filter(item => Number(item.status) === 3) } this.setData({ activeFilter, filteredCoupons }) }, /** * `handleFilterChange` page method. Use this from WXML event bindings or lifecycle hooks. * Call guide: read or update page data with `this.setData(...)` inside the method when state changes. */ handleFilterChange(event) { const value = event.currentTarget.dataset.value /** * `if` page method. Use this from WXML event bindings or lifecycle hooks. * Call guide: read or update page data with `this.setData(...)` inside the method when state changes. */ if (!value || value === this.data.activeFilter) { return } this.applyFilter(value) }, /** * `handleOpenAuth` page method. Use this from WXML event bindings or lifecycle hooks. * Call guide: read or update page data with `this.setData(...)` inside the method when state changes. */ handleOpenAuth() { navigation.openAuth('/pages/coupons/index') }, /** * `handleBack` page method. Use this from WXML event bindings or lifecycle hooks. * Call guide: read or update page data with `this.setData(...)` inside the method when state changes. */ handleBack() { navigation.goBack('/pages/profile/index') } })