431ceb4bdb
在项目中新增了多个页面,包括押金页面、设备详情页面、反馈页面和帮助页面。同时,更新了订单支付和归还成功页面的逻辑,确保用户在支付和归还设备时能够获得清晰的反馈。优化了扫码和订单状态处理逻辑,提升了整体用户体验。
171 lines
4.5 KiB
JavaScript
171 lines
4.5 KiB
JavaScript
import { queryById } from '@/config/user.js'
|
|
|
|
/**
|
|
* 订单监控服务
|
|
* 用于在后台监控订单状态变化,特别是归还完成状态
|
|
*/
|
|
class OrderMonitor {
|
|
constructor() {
|
|
this.activeOrders = new Map() // 存储活跃订单 Map<orderId, orderData>
|
|
this.timer = null
|
|
this.checkInterval = 10000 // 10秒检查一次
|
|
this.isRunning = false
|
|
}
|
|
|
|
/**
|
|
* 添加订单到监控队列
|
|
* @param {Object} orderData 订单数据对象,必须包含orderId字段
|
|
*/
|
|
addOrder(orderData) {
|
|
if (!orderData || !orderData.orderId) {
|
|
console.error('添加订单监控失败:无效的订单数据')
|
|
return
|
|
}
|
|
|
|
console.log('添加订单到监控队列:', orderData.orderId)
|
|
this.activeOrders.set(orderData.orderId, orderData)
|
|
|
|
// 如果监控服务尚未启动,则启动
|
|
if (!this.isRunning) {
|
|
this.start()
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 移除订单的监控
|
|
* @param {String} orderId 订单ID
|
|
*/
|
|
removeOrder(orderId) {
|
|
if (this.activeOrders.has(orderId)) {
|
|
console.log('从监控队列移除订单:', orderId)
|
|
this.activeOrders.delete(orderId)
|
|
|
|
// 如果没有订单需要监控了,停止服务
|
|
if (this.activeOrders.size === 0) {
|
|
this.stop()
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 启动监控服务
|
|
*/
|
|
start() {
|
|
if (this.isRunning) return
|
|
|
|
console.log('启动订单监控服务')
|
|
this.isRunning = true
|
|
this.checkOrders()
|
|
|
|
this.timer = setInterval(() => {
|
|
this.checkOrders()
|
|
}, this.checkInterval)
|
|
}
|
|
|
|
/**
|
|
* 停止监控服务
|
|
*/
|
|
stop() {
|
|
if (!this.isRunning) return
|
|
|
|
console.log('停止订单监控服务')
|
|
this.isRunning = false
|
|
|
|
if (this.timer) {
|
|
clearInterval(this.timer)
|
|
this.timer = null
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 检查所有活跃订单的状态
|
|
*/
|
|
async checkOrders() {
|
|
if (this.activeOrders.size === 0) return
|
|
|
|
console.log(`检查 ${this.activeOrders.size} 个活跃订单状态`)
|
|
|
|
for (const [orderId, orderData] of this.activeOrders.entries()) {
|
|
try {
|
|
await this.checkOrderStatus(orderId)
|
|
} catch (error) {
|
|
console.error(`检查订单状态失败: ${orderId}`, error)
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 检查单个订单的状态
|
|
* @param {String} orderId 订单ID
|
|
*/
|
|
async checkOrderStatus(orderId) {
|
|
try {
|
|
console.log(`检查订单 ${orderId} 的状态`)
|
|
const result = await queryById(orderId)
|
|
|
|
if (result.code === 200 && result.data) {
|
|
const orderData = result.data
|
|
|
|
// 更新本地存储的订单数据
|
|
this.activeOrders.set(orderId, orderData)
|
|
|
|
// 检查订单是否已完成
|
|
if (orderData.orderStatus === 'used_done' || orderData.orderStatus === 'used_down') {
|
|
console.log(`订单 ${orderId} 已完成!`)
|
|
|
|
// 触发全局事件
|
|
uni.$emit('orderCompleted', orderData)
|
|
|
|
// 显示全局通知
|
|
uni.showToast({
|
|
title: '充电宝归还成功',
|
|
icon: 'success',
|
|
duration: 2000
|
|
})
|
|
|
|
// 播放通知声音(如果需要)
|
|
const innerAudioContext = uni.createInnerAudioContext()
|
|
innerAudioContext.src = '/static/audio/return_success.mp3'
|
|
innerAudioContext.play()
|
|
|
|
// 完成的订单不再需要监控
|
|
this.removeOrder(orderId)
|
|
|
|
// 如果用户不在归还页面,则显示归还成功弹窗
|
|
setTimeout(() => {
|
|
uni.showModal({
|
|
title: '归还成功',
|
|
content: '充电宝已归还成功,剩余押金将退还到您的账户',
|
|
confirmText: '查看详情',
|
|
success: (res) => {
|
|
if (res.confirm) {
|
|
// 跳转到归还成功页面查看详情
|
|
uni.redirectTo({
|
|
url: `/pages/order/return-success?orderId=${orderId}`
|
|
})
|
|
}
|
|
}
|
|
})
|
|
}, 500)
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error(`检查订单 ${orderId} 状态出错:`, error)
|
|
}
|
|
}
|
|
}
|
|
|
|
// 导出单例实例
|
|
export const orderMonitor = new OrderMonitor()
|
|
|
|
// 页面加载时自动恢复监控上次的活跃订单(如果有)
|
|
const initOrderMonitor = () => {
|
|
const lastActiveOrderId = uni.getStorageSync('activeOrderId')
|
|
if (lastActiveOrderId) {
|
|
const lastOrderData = { orderId: lastActiveOrderId }
|
|
orderMonitor.addOrder(lastOrderData)
|
|
}
|
|
}
|
|
|
|
// 初始化
|
|
initOrderMonitor()
|