"use strict";const e=require("../common/vendor.js"),r=require("../config/user.js");const t=new class{constructor(){this.activeOrders=new Map,this.timer=null,this.checkInterval=1e4,this.isRunning=!1,this.currentPage=null}addOrder(e,r="return"){e&&e.orderId?(console.log(`添加订单到监控队列: ${e.orderId}, 页面: ${r}`),this.activeOrders.set(e.orderId,{...e,pageName:r}),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[r,t]of this.activeOrders.entries())t.pageName===e.pageName&&this.activeOrders.delete(r)}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[r,t]of this.activeOrders.entries())try{t.pageName&&"return"!==t.pageName||"return"!==this.currentPage&&null!==this.currentPage||await this.checkOrderStatus(r)}catch(e){}}}async checkOrderStatus(t){try{console.log(`检查订单 ${t} 的状态`);const s=await r.queryById(t);if(200===s.code&&s.data){const r=s.data,i=this.activeOrders.get(t),o=i?i.pageName:null;if(this.activeOrders.set(t,{...r,pageName:o}),"used_done"===r.orderStatus||"used_down"===r.orderStatus){console.log(`订单 ${t} 已完成!`),e.index.$emit("orderCompleted",r),e.index.showToast({title:"风扇归还成功",icon:"success",duration:2e3});const s=e.index.createInnerAudioContext();s.src="/static/audio/return_success.mp3",s.play(),this.removeOrder({orderId:t}),setTimeout((()=>{e.index.showModal({title:"归还成功",content:"风扇已归还成功，剩余押金将退还到您的账户",confirmText:"查看详情",success:r=>{r.confirm&&e.index.redirectTo({url:`/pages/order/return-success?orderId=${t}`})}})}),500)}}}catch(s){console.error(`检查订单 ${t} 状态出错:`,s)}}};e.index.onAppRoute((e=>{const r=e.path||"",s=r.split("/"),i=s[s.length-1];t.setActivePage(i||null),console.log("页面切换:",r,"当前活跃页面:",i)}));(()=>{const r=e.index.getStorageSync("activeOrderId");if(r){const e={orderId:r};t.addOrder(e,"return")}})(),exports.orderMonitor=t;
