const userApi = require('../../api/user') const navigation = require('../../utils/navigation') function money(value) { return Number(value || 0).toFixed(2) } function formatTime(value) { return value ? String(value).replace('T', ' ').slice(0, 16) : '--' } function sceneText(scene) { const map = { MANUAL_RECHARGE: '手工充值', MANUAL_DEDUCT: '手工扣减', ORDER_PAY: '订单消费', ORDER_REFUND: '订单退款', RECHARGE: '充值', CONSUME: '消费' } return map[scene] || scene || '余额变动' } /** * vip-center page logic. * Call guide: page data drives the WXML template, and each method is called by user events or lifecycle callbacks. */ Page({ data: { asset: { vipLevel: '普通', balanceText: '0.00', totalRechargeText: '0.00', lastRechargeText: '--', vipStartText: '--', manualOverride: false }, history: [], rechargePlans: [], rechargePanelVisible: false }, /** * `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() { if (!navigation.ensureLogin('查看 VIP 中心')) { return } this.loadData() }, async loadData() { try { const [asset, history, plans] = await Promise.all([ userApi.getMyAsset(), userApi.getMyBalanceHistory(), userApi.listMyVipRechargePlans().catch(() => []) ]) this.setData({ asset: { ...asset, balanceText: money(asset && asset.balance), totalRechargeText: money(asset && asset.totalRechargeAmount), lastRechargeText: formatTime(asset && asset.lastRechargeTime), vipStartText: formatTime(asset && asset.vipStartTime) }, history: (Array.isArray(history) ? history : []).map(item => { const amountNumber = Number(item.amount || 0) return { ...item, amountNumber, amountText: `${amountNumber > 0 ? '+' : ''}¥${money(amountNumber)}`, sceneText: sceneText(item.changeScene || item.type), changeTimeText: formatTime(item.changeTime) } }), rechargePlans: (Array.isArray(plans) ? plans : []).map(item => ({ ...item, amountText: `充 ${money(item.rechargeAmount)} 送 ${money(item.giftAmount)}` })) }) } catch (error) { wx.showToast({ title: error.message || 'VIP 信息加载失败', icon: 'none' }) } }, /** * `openRechargePanel` 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. */ openRechargePanel() { this.setData({ rechargePanelVisible: true }) }, /** * `closeRechargePanel` 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. */ closeRechargePanel() { this.setData({ rechargePanelVisible: false }) }, /** * `noop` 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. */ noop() { } })