"use strict";const e=require("../common/vendor.js"),t=require("../config/user.js");const r=new class{constructor(){this.activeOrders=new Map,this.timer=null,this.checkInterval=1e4,this.isRunning=!1,this.currentPage=null}addOrder(e,t="return"){e&&e.orderId?(console.log(`添加订单到监控队列: ${e.orderId}, 页面: ${t}`),this.activeOrders.set(e.orderId,{...e,pageName:t}),this.isRunning||this.start()):console.error("添加订单监控失败：无效的订单数据")}removeOrder(e){if(e){if(e.orderId&&this.activeOrders.has(e.orderId))console.log("从监控队列移除订单:",e.orderId),this.activeOrders.delete(e.orderId);else if(e.pageName){console.log("从监控队列移除页面相关订单:",e.pageName);for(const[t,r]of this.activeOrders.entries())r.pageName===e.pageName&&this.activeOrders.delete(t)}0===this.activeOrders.size&&this.stop()}}setActivePage(e){this.currentPage=e,console.log("设置当前活跃页面:",e)}start(){this.isRunning||(console.log("启动订单监控服务"),this.isRunning=!0,this.checkOrders(),this.timer=setInterval((()=>{this.checkOrders()}),this.checkInterval))}stop(){this.isRunning&&(console.log("停止订单监控服务"),this.isRunning=!1,this.timer&&(clearInterval(this.timer),this.timer=null))}async checkOrders(){if(0!==this.activeOrders.size){console.log(`检查 ${this.activeOrders.size} 个活跃订单状态, 当前页面: ${this.currentPage}`);for(const[t,r]of this.activeOrders.entries())try{r.pageName&&"return"!==r.pageName||"return"!==this.currentPage&&null!==this.currentPage||await this.checkOrderStatus(t)}catch(e){}}}async checkOrderStatus(r){try{const s=new Date;console.log(`检查订单 ${r} 的状态`);const i=await t.queryById(r);if(200===i.code&&i.data){const t=i.data,o=this.activeOrders.get(r),n=o?o.pageName:null;if(this.activeOrders.set(r,{...t,pageName:n}),"used_done"===t.orderStatus||"used_down"===t.orderStatus){console.log(`订单 ${r} 已完成!`);let i=!0;if(t.endTime){const e=new Date(t.endTime),o=Math.abs(s-e)/6e4;o>3&&(console.log(`订单 ${r} 完成时间与当前时间相差${o.toFixed(2)}分钟，超过3分钟，不进行提醒`),i=!1)}i&&(e.index.$emit("orderCompleted",t),e.index.showToast({title:"风扇归还成功",icon:"success",duration:2e3}),setTimeout((()=>{e.index.showModal({title:"归还成功",content:"风扇已归还成功，剩余押金将退还到您的账户",confirmText:"查看详情",success:t=>{t.confirm&&e.index.redirectTo({url:`/pages/order/return-success?orderId=${r}`})}})}),500)),this.removeOrder({orderId:r})}}}catch(s){console.error(`检查订单 ${r} 状态出错:`,s)}}};e.index.onAppRoute((e=>{const t=e.path||"",s=t.split("/"),i=s[s.length-1];r.setActivePage(i||null),console.log("页面切换:",t,"当前活跃页面:",i)}));(()=>{const t=e.index.getStorageSync("activeOrderId");if(t){const e={orderId:t};r.addOrder(e,"return")}})(),exports.orderMonitor=r;
