Files
m2pool_web_frontend/mining-pool/src/mixins/networkRecoveryMixin.js
yaoqin 4d436c725e 1.m2pool断网重连 60秒内重连机制 所有页面添加
2.优化响应错误提示 3秒内同一种错误只提示一次
2025-04-18 14:45:39 +08:00

61 lines
1.9 KiB
JavaScript

//处理断网重连后数据刷新的混入
export default {
data() {
return {
// 注册需要在网络恢复时调用的方法名
recoveryMethods: [],
// 上次调用这些方法时的参数
methodParams: {}
};
},
methods: {
/**
* 注册需要在网络恢复时自动重新调用的方法
* @param {String} methodName - 方法名
* @param {*} params - 调用方法时需要的参数
*/
registerRecoveryMethod(methodName, params) {
if (typeof this[methodName] === 'function' && !this.recoveryMethods.includes(methodName)) {
this.recoveryMethods.push(methodName);
this.methodParams[methodName] = params;
console.log(`[NetworkRecovery] 注册方法: ${methodName}`);
}
},
/**
* 更新方法的参数
* @param {String} methodName - 方法名
* @param {*} params - 新的参数
*/
updateMethodParams(methodName, params) {
if (this.recoveryMethods.includes(methodName)) {
this.methodParams[methodName] = params;
}
},
/**
* 网络恢复后调用所有注册的方法
*/
handleNetworkRecovery() {
console.log('[NetworkRecovery] 网络已恢复,正在刷新数据...');
this.recoveryMethods.forEach(methodName => {
if (typeof this[methodName] === 'function') {
const params = this.methodParams[methodName];
console.log(`[NetworkRecovery] 重新调用方法: ${methodName}`);
this[methodName](params);
}
});
}
},
mounted() {
// 监听网络恢复事件
window.addEventListener('network-retry-complete', this.handleNetworkRecovery);
},
beforeDestroy() {
// 清理事件监听
window.removeEventListener('network-retry-complete', this.handleNetworkRecovery);
}
};