jiaofeixiangqing.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. // 设置页面数据
  26. this.setData({
  27. billData: billDetail,
  28. loading: false
  29. });
  30. } else {
  31. throw new Error('未接收到账单数据');
  32. }
  33. } catch (error) {
  34. console.error('解析账单数据失败:', error);
  35. this.setData({
  36. loading: false,
  37. errorMsg: '数据解析失败,请返回重试'
  38. });
  39. wx.showToast({
  40. title: '数据解析失败',
  41. icon: 'none',
  42. duration: 2000
  43. });
  44. }
  45. },
  46. // 格式化金额,保留两位小数
  47. formatAmount: function(amount) {
  48. if (typeof amount !== 'number') {
  49. amount = parseFloat(amount) || 0;
  50. }
  51. return amount.toFixed(2);
  52. },
  53. // 补零函数
  54. padZero: function(num) {
  55. return num < 10 ? '0' + num : num;
  56. },
  57. // 返回上一页
  58. goBack: function() {
  59. wx.navigateBack();
  60. },
  61. // 如果需要根据billId重新获取详情数据
  62. getBillDetailById: function(billId) {
  63. wx.showLoading({
  64. title: '获取详情...',
  65. mask: true
  66. });
  67. wx.request({
  68. url: app.globalData.interfaceUrls.billDetail,
  69. method: 'POST',
  70. data: {
  71. billId: billId
  72. },
  73. header: {
  74. 'content-type': 'application/json',
  75. 'token': app.globalData.userWxInfo.token,
  76. 'source': "wc",
  77. '!SAAS_LOGIN_TOKEN_!': app.globalData.currentAccountInfo.dsKey
  78. },
  79. success: (res) => {
  80. wx.hideLoading();
  81. if (res.data && res.data.code === 0) {
  82. this.setData({
  83. billDetail: res.data.data,
  84. loading: false
  85. });
  86. } else {
  87. this.setData({
  88. loading: false,
  89. errorMsg: res.data.msg || '获取详情失败'
  90. });
  91. wx.showToast({
  92. title: '获取详情失败',
  93. icon: 'none'
  94. });
  95. }
  96. },
  97. fail: (error) => {
  98. wx.hideLoading();
  99. this.setData({
  100. loading: false,
  101. errorMsg: '网络错误,请稍后重试'
  102. });
  103. wx.showToast({
  104. title: '网络错误,请稍后重试',
  105. icon: 'none'
  106. });
  107. }
  108. });
  109. }
  110. })