parking-order.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // components/parking-order.js
  2. Component({
  3. /**
  4. * 组件的属性列表
  5. */
  6. properties: {
  7. occupyOrder:{
  8. type: Array,
  9. value: []
  10. },
  11. showPopup:{
  12. type: Boolean,
  13. value: false
  14. }
  15. },
  16. lifetimes:{
  17. attached(){
  18. console.log("加载占位中订单组件");
  19. // this.loadOccupyOrder();
  20. // 页面加载时显示弹窗
  21. let isLogin = wx.getStorageSync('isLogin');
  22. if (isLogin) {
  23. this.loadOccupyOrder().then(data => {
  24. this.startPolling();
  25. })
  26. .catch(error => {
  27. console.error("数据加载失败", error);
  28. });
  29. }
  30. },
  31. detached: function() {
  32. // 在组件实例被从页面节点树移除时执行
  33. this.stopPolling();
  34. },
  35. },
  36. pageLifetimes:{
  37. // 组件所在页面的生命周期函数
  38. show: function () {
  39. },
  40. hide: function() {
  41. // 页面被隐藏
  42. this.stopPolling();
  43. },
  44. },
  45. /**
  46. * 组件的初始数据
  47. */
  48. data: {
  49. showPopup: false,
  50. occupyFeeOrderList:[],
  51. pollingInterval: null, // 定时器,
  52. costCycleFee:0
  53. },
  54. /**
  55. * 组件的方法列表
  56. */
  57. methods: {
  58. // 第一版
  59. // startPolling: function() {
  60. // let that = this
  61. // // Set up interval for subsequent loads
  62. // this.data.pollingInterval = setInterval(() => {
  63. // let occupyFeeOrder = this.data.occupyFeeOrderList[0];
  64. // // 如果 occupyFeeOrder 不存在,直接返回
  65. // if (!occupyFeeOrder) {
  66. // console.warn("occupyFeeOrder 不存在,跳过本次轮询");
  67. // return;
  68. // }
  69. // wx.request({
  70. // url: 'https://jqcs.pjnes.com/cloud/occupyfee/evcs/occupyfee/template',
  71. // data: occupyFeeOrder.chargpileids,
  72. // method: 'POST',
  73. // success(res) {
  74. // that.calculate(occupyFeeOrder,res.data)
  75. // }})
  76. // }, 30000);
  77. // },
  78. startPolling: function() {
  79. let that = this;
  80. function fetchData() {
  81. let occupyFeeOrder = that.data.occupyFeeOrderList[0];
  82. console.log("occupyFeeOrder",occupyFeeOrder);
  83. // 如果 occupyFeeOrder 不存在,直接返回
  84. if (!occupyFeeOrder) {
  85. console.warn("occupyFeeOrder 不存在,跳过本次轮询");
  86. return;
  87. }
  88. wx.request({
  89. url: 'https://jqcs.pjnes.com/cloud/occupyfee/evcs/occupyfee/template',
  90. data: occupyFeeOrder.orderId,
  91. method: 'POST',
  92. success(res) {
  93. that.calculate(occupyFeeOrder, res.data);
  94. }
  95. });
  96. }
  97. // **立即执行一次**
  98. fetchData();
  99. // **设置轮询**
  100. this.data.pollingInterval = setInterval(fetchData, 30000);
  101. },
  102. calculate(occupyFeeOrder,data) {
  103. let { occupyBeginTime ="" ,occupyTime =""} = occupyFeeOrder;
  104. // 将时间字符串转换为 Date 对象
  105. occupyBeginTime = new Date(occupyBeginTime.replace(/-/g, "/")); // 替换为兼容 iOS 的格式
  106. let currentTime = new Date();
  107. // 计算时间差(分)
  108. // let minutesDifference = Math.floor((currentTime - occupyBeginTime) / (1000 * 60));
  109. let minutesDifference = data.params.occupyTime;
  110. let freeTime = Number(data.freeTime) || 0;
  111. console.log("免费时长:",freeTime);
  112. console.log("最大占位时长:", data.maxFeeTime);
  113. console.log("data:", data);
  114. console.log("分钟:", minutesDifference);
  115. if (minutesDifference >= freeTime +1) {
  116. if (minutesDifference <= data.maxFeeTime) {
  117. console.log("未达到最大时长");
  118. let Nocycle = Math.floor(minutesDifference / data.costCycle)
  119. occupyFeeOrder.occupyFee = (Nocycle * data.costCycleFee).toFixed(2);
  120. occupyFeeOrder.occupyTime = minutesDifference;
  121. } else {
  122. console.log("达到最大时长");
  123. let cycle = Math.floor(data.maxFeeTime / data.costCycle)
  124. occupyFeeOrder.occupyFee = (cycle * data.costCycleFee).toFixed(2);
  125. occupyFeeOrder.occupyTime =minutesDifference;
  126. }
  127. } else {
  128. console.log("处于免费时长");
  129. occupyFeeOrder.occupyFee = 0.00;
  130. occupyFeeOrder.occupyTime = minutesDifference;
  131. }
  132. console.log("触发定时器",occupyFeeOrder);
  133. // 根据返回的结果去调用微服务查询模版信息接口,根据模版信息 重新动态计算占位费模版
  134. this.setData({
  135. "occupyFeeOrderList[0]": occupyFeeOrder
  136. });
  137. },
  138. // Stop polling
  139. stopPolling: function() {
  140. if (this.data.pollingInterval) {
  141. console.log('清除占位中定时器');
  142. clearInterval(this.data.pollingInterval);
  143. this.data.pollingInterval = null;
  144. }
  145. },
  146. loadOccupyOrder() {
  147. let that = this;
  148. return new Promise((resolve, reject) => {
  149. wx.request({
  150. url: getApp().globalData.postHeadAgreement + '/restapi/wechat/queryOccupyFeeOrder',
  151. data: {
  152. userId: wx.getStorageSync('userInfo').userId,
  153. searchIndex: 2,
  154. pagenum: this.data.pagenum++,
  155. pagesize: 10,
  156. },
  157. method: 'POST',
  158. success(res) {
  159. console.log("占位费", res);
  160. if (res.data.result) {
  161. let { rows: occupyFeeOrderList } = res.data.result;
  162. that.setData({
  163. occupyFeeOrderList: that.data.occupyFeeOrderList.concat(occupyFeeOrderList),
  164. });
  165. resolve(res.data.result);
  166. }
  167. // else {
  168. // reject(new Error("No result data"));
  169. // }
  170. },
  171. fail(err) {
  172. reject(err);
  173. }
  174. });
  175. });
  176. },
  177. handleConfirm() {
  178. // 点击确认按钮
  179. this.setData({
  180. showPopup: false
  181. });
  182. },
  183. handleContact() {
  184. // 联系客服
  185. // wx.makePhoneCall({
  186. // phoneNumber: '4009608068' // 替换为实际的客服电话
  187. // });
  188. wx.showModal({
  189. title: '提示',
  190. confirmColor: '#00AADD',
  191. content: '若对当前占位费订单有疑问,请拨打客服电话4009608068,工作时间:08:00-18:00',
  192. complete: (res) => {
  193. // 可以在这里添加回调逻辑
  194. if (res.confirm) {
  195. console.log('用户点击了确认按钮');
  196. }else{
  197. console.log('用户点击了取消按钮');
  198. }
  199. }
  200. });
  201. },
  202. }
  203. })