jiaofeixiangqing.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. const app = getApp()
  2. Page({
  3. data: {
  4. billData: {}, // 账单详情数据
  5. loading: true, // 加载状态
  6. errorMsg: '' // 错误信息
  7. },
  8. onLoad: function(options) {
  9. try {
  10. if (options.billInfo) {
  11. // 解析从URL参数传递的JSON字符串获取完整账单数据
  12. const billDetail = JSON.parse(decodeURIComponent(options.billInfo));
  13. // 格式化bills数组中的每个thismeterreadingdate字段
  14. if (billDetail.bills && Array.isArray(billDetail.bills)) {
  15. billDetail.bills.forEach(bill => {
  16. if (bill.thismeterreadingdate) {
  17. // 将ISO日期格式转换为年月日格式
  18. const date = new Date(bill.thismeterreadingdate);
  19. bill.thismeterreadingdate = date.getFullYear() + '-' +
  20. this.padZero(date.getMonth() + 1) + '-' +
  21. this.padZero(date.getDate());
  22. }
  23. });
  24. // 按抄表日期降序排序
  25. billDetail.bills.sort((a, b) => {
  26. return new Date(b.thismeterreadingdate) - new Date(a.thismeterreadingdate);
  27. });
  28. }
  29. // 设置页面数据
  30. this.setData({
  31. billData: billDetail,
  32. loading: false
  33. });
  34. } else {
  35. throw new Error('未接收到账单数据');
  36. }
  37. } catch (error) {
  38. console.error('解析账单数据失败:', error);
  39. this.setData({
  40. loading: false,
  41. errorMsg: '数据解析失败,请返回重试'
  42. });
  43. wx.showToast({
  44. title: '数据解析失败',
  45. icon: 'none',
  46. duration: 2000
  47. });
  48. }
  49. },
  50. // 格式化金额,保留两位小数
  51. formatAmount: function(amount) {
  52. if (typeof amount !== 'number') {
  53. amount = parseFloat(amount) || 0;
  54. }
  55. return amount.toFixed(2);
  56. },
  57. // 补零函数
  58. padZero: function(num) {
  59. return num < 10 ? '0' + num : num;
  60. },
  61. // 返回上一页
  62. goBack: function() {
  63. wx.navigateBack();
  64. },
  65. // 如果需要根据billId重新获取详情数据
  66. getBillDetailById: function(billId) {
  67. wx.showLoading({
  68. title: '获取详情...',
  69. mask: true
  70. });
  71. wx.request({
  72. url: app.globalData.interfaceUrls.billDetail,
  73. method: 'POST',
  74. data: {
  75. billId: billId
  76. },
  77. header: {
  78. 'content-type': 'application/json',
  79. 'token': app.globalData.userWxInfo.token,
  80. 'source': "wc",
  81. '!SAAS_LOGIN_TOKEN_!': app.globalData.currentAccountInfo.dsKey
  82. },
  83. success: (res) => {
  84. wx.hideLoading();
  85. if (res.data && res.data.code === 0) {
  86. this.setData({
  87. billDetail: res.data.data,
  88. loading: false
  89. });
  90. } else {
  91. this.setData({
  92. loading: false,
  93. errorMsg: res.data.msg || '获取详情失败'
  94. });
  95. wx.showToast({
  96. title: '获取详情失败',
  97. icon: 'none'
  98. });
  99. }
  100. },
  101. fail: (error) => {
  102. wx.hideLoading();
  103. this.setData({
  104. loading: false,
  105. errorMsg: '网络错误,请稍后重试'
  106. });
  107. wx.showToast({
  108. title: '网络错误,请稍后重试',
  109. icon: 'none'
  110. });
  111. }
  112. });
  113. }
  114. })