charginglog.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // pages/ucenter/charginglog/charginglog.js
  2. let Util = require("../../../utils/util");
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. charginglogs: []
  9. },
  10. /**
  11. * 生命周期函数--监听页面加载
  12. */
  13. onLoad: function(options) {
  14. let userInfo = wx.getStorageSync('userInfo');
  15. let isLogin = wx.getStorageSync('isLogin');
  16. let that = this;
  17. // 页面显示
  18. if (userInfo && isLogin) {
  19. wx.request({
  20. url: getApp().globalData.postHeadAgreement +'://cdgl.xinyhy.cn/restapi/pileLog/wechatuserallcharglog',
  21. data: {
  22. userId: userInfo.userId
  23. },
  24. method: 'POST',
  25. success(res) {
  26. console.log(res);
  27. if (res.data.result) {
  28. let {
  29. result: charginglogs
  30. } = res.data;
  31. that.startTimeOrder(charginglogs);
  32. that.formatCharginglogs(charginglogs);
  33. that.setData({
  34. charginglogs
  35. });
  36. }
  37. }
  38. });
  39. }
  40. },
  41. formatCharginglogs(charginglogs) {
  42. charginglogs.forEach((item, index) => {
  43. try {
  44. var startDate = new Date(item.chargstarttime);
  45. var endDate = new Date(item.chargendtime);
  46. var totalTime = Math.round((endDate.getTime() - startDate.getTime()) / 60000);
  47. var totalTimeHour = Math.floor(totalTime / 60);
  48. var totalTimeMinute = totalTime % 60;
  49. var startTime = startDate.getFullYear() + "年" + Util.formatNumber(startDate.getMonth() + 1) + '月' + Util.formatNumber(startDate.getDate()) + '日 ' + Util.formatNumber(startDate.getHours()) + ':' + Util.formatNumber(startDate.getMinutes()) + ':' + Util.formatNumber(startDate.getSeconds());
  50. var endTime = endDate.getFullYear() + "年" + Util.formatNumber(endDate.getMonth() + 1) + '月' + Util.formatNumber(endDate.getDate()) + '日 ' + Util.formatNumber(endDate.getHours()) + ':' + Util.formatNumber(endDate.getMinutes()) + ':' + Util.formatNumber(endDate.getSeconds());
  51. totalTime = Util.formatNumber(totalTimeHour) + "时" + Util.formatNumber(totalTimeMinute) + "分";
  52. item.startTime = startTime;
  53. item.endTime = endTime;
  54. item.totalTime = totalTime;
  55. item.chargallmoney = item.chargallmoney.toFixed(2);
  56. item.chargservice = item.chargservice.toFixed(2);
  57. item.chargmoney = item.chargmoney.toFixed(2);
  58. } catch (err) {
  59. //在这里处理错误
  60. }
  61. });
  62. },
  63. startTimeOrder(charginglogs) {
  64. if (charginglogs && 0 != charginglogs.length) {
  65. charginglogs.sort(function(ma, mb) {
  66. return mb.chargstarttime.localeCompare(ma.chargstarttime);
  67. });
  68. }
  69. },
  70. /**
  71. * 生命周期函数--监听页面初次渲染完成
  72. */
  73. onReady: function() {
  74. },
  75. /**
  76. * 生命周期函数--监听页面显示
  77. */
  78. onShow: function() {
  79. },
  80. /**
  81. * 生命周期函数--监听页面隐藏
  82. */
  83. onHide: function() {
  84. },
  85. /**
  86. * 生命周期函数--监听页面卸载
  87. */
  88. onUnload: function() {
  89. },
  90. /**
  91. * 页面相关事件处理函数--监听用户下拉动作
  92. */
  93. onPullDownRefresh: function() {
  94. },
  95. /**
  96. * 页面上拉触底事件的处理函数
  97. */
  98. onReachBottom: function() {
  99. },
  100. /**
  101. * 用户点击右上角分享
  102. */
  103. onShareAppMessage: function() {
  104. }
  105. })