1.m2pool断网重连 60秒内重连机制 所有页面添加
2.优化响应错误提示 3秒内同一种错误只提示一次
This commit is contained in:
@@ -2,6 +2,8 @@ import axios from 'axios'
|
||||
import errorCode from './errorCode'
|
||||
import { Notification, MessageBox, Message } from 'element-ui'
|
||||
import loadingManager from './loadingManager';
|
||||
import errorNotificationManager from './errorNotificationManager';
|
||||
|
||||
// 创建axios实例
|
||||
const service = axios.create({
|
||||
// axios中请求配置有baseURL选项,表示请求URL公共部分
|
||||
@@ -19,10 +21,51 @@ let lastNetworkErrorTime = 0; // 上次网络错误提示时间
|
||||
let pendingRequests = new Map();
|
||||
|
||||
|
||||
// 网络状态监听器
|
||||
// 网络状态最后提示时间
|
||||
let lastNetworkStatusTime = {
|
||||
online: 0,
|
||||
offline: 0
|
||||
};
|
||||
|
||||
// 创建一个全局标志,确保每次网络恢复只显示一次提示
|
||||
let networkRecoveryInProgress = false;
|
||||
|
||||
// 网络状态监听器
|
||||
window.addEventListener('online', () => {
|
||||
// 网络恢复时,重试所有待处理的请求
|
||||
const now = Date.now();
|
||||
|
||||
// 避免短时间内多次触发
|
||||
if (networkRecoveryInProgress) {
|
||||
console.log('[网络] 网络恢复处理已在进行中,忽略重复事件');
|
||||
return;
|
||||
}
|
||||
|
||||
networkRecoveryInProgress = true;
|
||||
|
||||
// 严格检查是否应该显示提示
|
||||
if (now - lastNetworkStatusTime.online > 30000) { // 30秒内不重复提示
|
||||
lastNetworkStatusTime.online = now;
|
||||
|
||||
try {
|
||||
if (window.vm && window.vm.$message) {
|
||||
// 确保消息只显示一次
|
||||
window.vm.$message({
|
||||
message: window.vm.$i18n.t('home.networkReconnected') || '网络已重新连接,正在恢复数据...',
|
||||
type: 'success',
|
||||
duration: 5000,
|
||||
showClose: true,
|
||||
});
|
||||
console.log('[网络] 显示网络恢复提示, 时间:', new Date().toLocaleTimeString());
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('[网络] 显示网络恢复提示失败:', e);
|
||||
}
|
||||
} else {
|
||||
console.log('[网络] 抑制重复的网络恢复提示, 间隔过短:', now - lastNetworkStatusTime.online + 'ms');
|
||||
}
|
||||
|
||||
// 网络恢复时,重试所有待处理的请求
|
||||
const pendingPromises = [];
|
||||
|
||||
pendingRequests.forEach(async (request, key) => {
|
||||
@@ -71,27 +114,46 @@ window.addEventListener('online', () => {
|
||||
// 等待所有请求完成
|
||||
Promise.allSettled(pendingPromises).then(() => {
|
||||
// 重置所有 loading 状态
|
||||
loadingManager.resetAllLoadingStates();
|
||||
if (loadingManager) {
|
||||
loadingManager.resetAllLoadingStates();
|
||||
}
|
||||
|
||||
// 手动重置一些关键的 loading 状态
|
||||
if (window.vm) {
|
||||
// 常见的加载状态
|
||||
const commonLoadingProps = [
|
||||
'minerChartLoading', 'reportBlockLoading', 'apiPageLoading',
|
||||
'MiningLoading', 'miniLoading'
|
||||
];
|
||||
|
||||
commonLoadingProps.forEach(prop => {
|
||||
if (typeof window.vm[prop] !== 'undefined') {
|
||||
window.vm[prop] = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 触发网络重试完成事件
|
||||
window.dispatchEvent(new CustomEvent('network-retry-complete'));
|
||||
|
||||
// 重置网络恢复标志
|
||||
setTimeout(() => {
|
||||
networkRecoveryInProgress = false;
|
||||
}, 5000); // 5秒后允许再次处理网络恢复
|
||||
});
|
||||
|
||||
// 显示网络恢复提示
|
||||
if (window.vm && window.vm.$message) {
|
||||
window.vm.$message({
|
||||
message: window.vm.$i18n.t('home.networkReconnected') || '网络已重新连接,正在恢复数据...',
|
||||
type: 'success',
|
||||
duration: 3000
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
// 使用错误提示管理器控制网络断开提示
|
||||
window.addEventListener('offline', () => {
|
||||
if (window.vm && window.vm.$message) {
|
||||
if (window.vm && window.vm.$message && errorNotificationManager.canShowError('networkOffline')) {
|
||||
window.vm.$message({
|
||||
message: window.vm.$i18n.t('home.networkOffline') || '网络连接已断开,系统将在恢复连接后自动重试',
|
||||
type: 'warning',
|
||||
duration: 3000
|
||||
type: 'error',
|
||||
duration: 5000,
|
||||
showClose: true,
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -273,19 +335,12 @@ service.interceptors.response.use(res => {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (!superReportError) {
|
||||
superReportError = "error"
|
||||
localStorage.setItem('superReportError', superReportError)
|
||||
let { message } = error;
|
||||
if (message == "Network Error") {
|
||||
// message = "后端接口网络连接异常,请刷新重试";
|
||||
const now = Date.now();
|
||||
if (now - lastNetworkErrorTime > NETWORK_ERROR_THROTTLE_TIME) {
|
||||
lastNetworkErrorTime = now; // 更新最后提示时间
|
||||
//使用错误提示管理器errorNotificationManager
|
||||
if (errorNotificationManager.canShowError(message)) {
|
||||
if (message == "Network Error") {
|
||||
Message({
|
||||
message: window.vm.$i18n.t(`home.NetworkError`),
|
||||
type: 'error',
|
||||
@@ -293,36 +348,84 @@ service.interceptors.response.use(res => {
|
||||
showClose: true
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
else if (message.includes("timeout")) {
|
||||
// message = "系统接口请求超时,请刷新重试";
|
||||
Message({
|
||||
message: window.vm.$i18n.t(`home.requestTimeout`),
|
||||
type: 'error',
|
||||
duration: 5 * 1000,
|
||||
showClose: true
|
||||
})
|
||||
|
||||
}
|
||||
else if (message.includes("Request failed with status code")) {
|
||||
// message = "系统接口" + message.substr(message.length - 3) + "异常";
|
||||
Message({
|
||||
message: "系统接口" + message.substr(message.length - 3) + "异常",
|
||||
type: 'error',
|
||||
duration: 5 * 1000,
|
||||
showClose: true
|
||||
})
|
||||
else if (message.includes("timeout")) {
|
||||
Message({
|
||||
message: window.vm.$i18n.t(`home.requestTimeout`),
|
||||
type: 'error',
|
||||
duration: 5 * 1000,
|
||||
showClose: true
|
||||
});
|
||||
}
|
||||
else if (message.includes("Request failed with status code")) {
|
||||
Message({
|
||||
message: "系统接口" + message.substr(message.length - 3) + "异常",
|
||||
type: 'error',
|
||||
duration: 5 * 1000,
|
||||
showClose: true
|
||||
});
|
||||
} else {
|
||||
Message({
|
||||
message: message,
|
||||
type: 'error',
|
||||
duration: 5 * 1000,
|
||||
showClose: true
|
||||
});
|
||||
}
|
||||
} else {
|
||||
|
||||
Message({
|
||||
message: message,
|
||||
type: 'error',
|
||||
duration: 5 * 1000,
|
||||
showClose: true
|
||||
})
|
||||
|
||||
// 避免完全不提示,可以在控制台记录被抑制的错误
|
||||
console.log('[错误提示] 已抑制重复错误:', message);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// let { message } = error;
|
||||
// if (message == "Network Error") {
|
||||
// // message = "后端接口网络连接异常,请刷新重试";
|
||||
// const now = Date.now();
|
||||
// if (now - lastNetworkErrorTime > NETWORK_ERROR_THROTTLE_TIME) {
|
||||
// lastNetworkErrorTime = now; // 更新最后提示时间
|
||||
// Message({
|
||||
// message: window.vm.$i18n.t(`home.NetworkError`),
|
||||
// type: 'error',
|
||||
// duration: 4 * 1000,
|
||||
// showClose: true
|
||||
// });
|
||||
// }
|
||||
|
||||
// }
|
||||
// else if (message.includes("timeout")) {
|
||||
// // message = "系统接口请求超时,请刷新重试";
|
||||
// Message({
|
||||
// message: window.vm.$i18n.t(`home.requestTimeout`),
|
||||
// type: 'error',
|
||||
// duration: 5 * 1000,
|
||||
// showClose: true
|
||||
// })
|
||||
|
||||
// }
|
||||
// else if (message.includes("Request failed with status code")) {
|
||||
// // message = "系统接口" + message.substr(message.length - 3) + "异常";
|
||||
// Message({
|
||||
// message: "系统接口" + message.substr(message.length - 3) + "异常",
|
||||
// type: 'error',
|
||||
// duration: 5 * 1000,
|
||||
// showClose: true
|
||||
// })
|
||||
// } else {
|
||||
|
||||
// Message({
|
||||
// message: message,
|
||||
// type: 'error',
|
||||
// duration: 5 * 1000,
|
||||
// showClose: true
|
||||
// })
|
||||
|
||||
// }
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user