fix:修复bug
This commit is contained in:
+31
-28
@@ -8,7 +8,7 @@ class OrderMonitor {
|
||||
constructor() {
|
||||
this.activeOrders = new Map() // 存储活跃订单 Map<orderId, {orderData, pageName}>
|
||||
this.timer = null
|
||||
this.checkInterval = 10000 // 10秒检查一次
|
||||
this.checkInterval = 5000 // 5秒检查一次,更及时地发现订单完成
|
||||
this.isRunning = false
|
||||
this.currentPage = null // 当前活跃页面
|
||||
}
|
||||
@@ -16,9 +16,9 @@ class OrderMonitor {
|
||||
/**
|
||||
* 添加订单到监控队列
|
||||
* @param {Object} orderData 订单数据对象,必须包含orderId字段
|
||||
* @param {String} pageName 关联的页面名称,默认为'return'
|
||||
* @param {String} pageName 关联的页面名称,默认为'detail'(订单详情页)
|
||||
*/
|
||||
addOrder(orderData, pageName = 'return') {
|
||||
addOrder(orderData, pageName = 'detail') {
|
||||
if (!orderData || !orderData.orderId) {
|
||||
console.error('添加订单监控失败:无效的订单数据')
|
||||
return
|
||||
@@ -111,19 +111,15 @@ class OrderMonitor {
|
||||
|
||||
console.log(`检查 ${this.activeOrders.size} 个活跃订单状态, 当前页面: ${this.currentPage}`)
|
||||
|
||||
// 只检查当前活跃页面关联的订单,或无页面关联的订单
|
||||
// 检查所有活跃订单,不限制页面
|
||||
// 主要监控订单详情页(detail)和其他相关页面
|
||||
for (const [orderId, data] of this.activeOrders.entries()) {
|
||||
try {
|
||||
// 只在归还页面(或页面未指定时)才执行轮询
|
||||
if (!data.pageName || data.pageName === 'return') {
|
||||
if (this.currentPage === 'return' || this.currentPage === null) {
|
||||
await this.checkOrderStatus(orderId)
|
||||
} else {
|
||||
// console.log(`跳过订单状态检查: ${orderId}, 当前不在归还页面`)
|
||||
}
|
||||
}
|
||||
// 检查所有使用中的订单状态
|
||||
// 不限制页面,因为用户可能在任何页面时订单完成归还
|
||||
await this.checkOrderStatus(orderId)
|
||||
} catch (error) {
|
||||
// console.error(`检查订单状态失败: ${orderId}`, error)
|
||||
console.error(`检查订单状态失败: ${orderId}`, error)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -168,7 +164,7 @@ class OrderMonitor {
|
||||
}
|
||||
|
||||
if (shouldNotify) {
|
||||
// 触发全局事件
|
||||
// 触发全局事件(订单详情页会监听此事件自动刷新)
|
||||
uni.$emit('orderCompleted', orderData)
|
||||
|
||||
// 显示全局通知
|
||||
@@ -183,21 +179,27 @@ class OrderMonitor {
|
||||
// innerAudioContext.src = '/static/audio/return_success.mp3'
|
||||
// innerAudioContext.play()
|
||||
|
||||
// 如果用户不在归还页面,则显示归还成功弹窗
|
||||
// 如果用户不在订单详情页,则显示归还成功弹窗
|
||||
// 如果在订单详情页,页面自己会处理状态变化
|
||||
setTimeout(() => {
|
||||
uni.showModal({
|
||||
title: '归还成功',
|
||||
content: '风扇已归还成功,剩余押金将退还到您的账户',
|
||||
confirmText: '查看详情',
|
||||
success: (res) => {
|
||||
if (res.confirm) {
|
||||
// 跳转到统一订单详情页面查看详情
|
||||
uni.redirectTo({
|
||||
url: `/pages/order/detail?orderId=${orderId}`
|
||||
})
|
||||
if (this.currentPage !== 'detail') {
|
||||
uni.showModal({
|
||||
title: '归还成功',
|
||||
content: '风扇已归还成功,剩余押金将退还到您的账户',
|
||||
confirmText: '查看详情',
|
||||
cancelText: '我知道了',
|
||||
success: (res) => {
|
||||
if (res.confirm) {
|
||||
// 跳转到订单详情页面查看详情
|
||||
uni.navigateTo({
|
||||
url: `/pages/order/detail?orderId=${orderId}`
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
} else {
|
||||
console.log('当前在订单详情页,不显示弹窗,页面会自动刷新')
|
||||
}
|
||||
}, 500)
|
||||
}
|
||||
|
||||
@@ -231,7 +233,8 @@ const initOrderMonitor = () => {
|
||||
const lastActiveOrderId = uni.getStorageSync('activeOrderId')
|
||||
if (lastActiveOrderId) {
|
||||
const lastOrderData = { orderId: lastActiveOrderId }
|
||||
orderMonitor.addOrder(lastOrderData, 'return')
|
||||
orderMonitor.addOrder(lastOrderData, 'detail')
|
||||
console.log('恢复监控上次活跃订单:', lastActiveOrderId)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user