const app = getApp() Page({ data: { billData: {}, // 账单详情数据 loading: true, // 加载状态 errorMsg: '' // 错误信息 }, onLoad: function(options) { try { if (options.billInfo) { // 解析从URL参数传递的JSON字符串获取完整账单数据 const billDetail = JSON.parse(decodeURIComponent(options.billInfo)); // 格式化bills数组中的每个thismeterreadingdate字段 if (billDetail.bills && Array.isArray(billDetail.bills)) { billDetail.bills.forEach(bill => { if (bill.thismeterreadingdate) { // 将ISO日期格式转换为年月日格式 const date = new Date(bill.thismeterreadingdate); bill.thismeterreadingdate = date.getFullYear() + '-' + this.padZero(date.getMonth() + 1) + '-' + this.padZero(date.getDate()); } }); } // 设置页面数据 this.setData({ billData: billDetail, loading: false }); } else { throw new Error('未接收到账单数据'); } } catch (error) { console.error('解析账单数据失败:', error); this.setData({ loading: false, errorMsg: '数据解析失败,请返回重试' }); wx.showToast({ title: '数据解析失败', icon: 'none', duration: 2000 }); } }, // 格式化金额,保留两位小数 formatAmount: function(amount) { if (typeof amount !== 'number') { amount = parseFloat(amount) || 0; } return amount.toFixed(2); }, // 补零函数 padZero: function(num) { return num < 10 ? '0' + num : num; }, // 返回上一页 goBack: function() { wx.navigateBack(); }, // 如果需要根据billId重新获取详情数据 getBillDetailById: function(billId) { wx.showLoading({ title: '获取详情...', mask: true }); wx.request({ url: app.globalData.interfaceUrls.billDetail, method: 'POST', data: { billId: billId }, header: { 'content-type': 'application/json', 'token': app.globalData.userWxInfo.token, 'source': "wc", '!SAAS_LOGIN_TOKEN_!': app.globalData.currentAccountInfo.dsKey }, success: (res) => { wx.hideLoading(); if (res.data && res.data.code === 0) { this.setData({ billDetail: res.data.data, loading: false }); } else { this.setData({ loading: false, errorMsg: res.data.msg || '获取详情失败' }); wx.showToast({ title: '获取详情失败', icon: 'none' }); } }, fail: (error) => { wx.hideLoading(); this.setData({ loading: false, errorMsg: '网络错误,请稍后重试' }); wx.showToast({ title: '网络错误,请稍后重试', icon: 'none' }); } }); } })