90 lines
2.0 KiB
JavaScript
90 lines
2.0 KiB
JavaScript
/**
|
|
* 订单状态轮询Mixin
|
|
* 用于自动刷新订单状态
|
|
* 2026-01-24
|
|
*/
|
|
|
|
export default {
|
|
data() {
|
|
return {
|
|
pollingTimer: null,
|
|
pollingInterval: 10000, // 默认10秒
|
|
isPolling: false
|
|
}
|
|
},
|
|
|
|
methods: {
|
|
/**
|
|
* 开始轮询
|
|
* @param {Number} interval - 轮询间隔(毫秒)
|
|
* @param {Function} callback - 轮询回调函数
|
|
*/
|
|
startPolling(interval, callback) {
|
|
if (this.isPolling) {
|
|
console.log('[轮询] 已在轮询中,跳过')
|
|
return
|
|
}
|
|
|
|
this.pollingInterval = interval || this.pollingInterval
|
|
this.isPolling = true
|
|
|
|
console.log(`[轮询] 开始轮询,间隔${this.pollingInterval}ms`)
|
|
|
|
this.pollingTimer = setInterval(() => {
|
|
// 检查页面是否可见(仅在H5环境中检查)
|
|
// #ifdef H5
|
|
if (typeof document !== 'undefined' && document.hidden) {
|
|
console.log('[轮询] 页面不可见,跳过本次轮询')
|
|
return
|
|
}
|
|
// #endif
|
|
|
|
console.log('[轮询] 执行轮询回调')
|
|
if (typeof callback === 'function') {
|
|
callback()
|
|
}
|
|
}, this.pollingInterval)
|
|
},
|
|
|
|
/**
|
|
* 停止轮询
|
|
*/
|
|
stopPolling() {
|
|
if (this.pollingTimer) {
|
|
clearInterval(this.pollingTimer)
|
|
this.pollingTimer = null
|
|
this.isPolling = false
|
|
console.log('[轮询] 已停止轮询')
|
|
}
|
|
},
|
|
|
|
/**
|
|
* 调整轮询频率
|
|
* @param {Number} interval - 新的轮询间隔(毫秒)
|
|
*/
|
|
adjustPollingInterval(interval) {
|
|
if (this.isPolling) {
|
|
this.stopPolling()
|
|
this.startPolling(interval, this.pollingCallback)
|
|
}
|
|
}
|
|
},
|
|
|
|
// 页面隐藏时停止轮询
|
|
onHide() {
|
|
this.stopPolling()
|
|
},
|
|
|
|
// 页面卸载时停止轮询
|
|
onUnload() {
|
|
this.stopPolling()
|
|
},
|
|
|
|
// 页面显示时恢复轮询
|
|
onShow() {
|
|
if (this.pollingCallback && !this.isPolling) {
|
|
this.startPolling(this.pollingInterval, this.pollingCallback)
|
|
}
|
|
}
|
|
}
|