diff --git a/src/main/ipc.js b/src/main/ipc.js index 6e64c5d..d0eca56 100644 --- a/src/main/ipc.js +++ b/src/main/ipc.js @@ -199,9 +199,31 @@ export function setupIPC() { newWindow.loadURL(url); - // 可选:添加错误处理 + // 添加重试机制的错误处理 + let failLoadCount = 0; + const maxRetries = 2; + const retryDelay = 1000; + newWindow.webContents.on('did-fail-load', (event, errorCode, errorDescription) => { console.error('页面加载失败:', errorDescription); + failLoadCount++; + + if (failLoadCount <= maxRetries) { + console.log(`页面加载失败,${retryDelay}ms后进行第${failLoadCount}次重试...`); + setTimeout(() => { + newWindow.reload(); + }, retryDelay); + } else { + console.error('页面加载失败次数超过最大重试次数'); + } + }); + + // 页面加载成功时重置失败计数 + newWindow.webContents.on('did-finish-load', () => { + if (failLoadCount > 0) { + console.log('页面加载成功,重置失败计数'); + failLoadCount = 0; + } }); return 'success'; @@ -222,9 +244,31 @@ export function setupIPC() { newWindow.loadURL(url); - // 可选:添加错误处理 + // 添加重试机制的错误处理 + let failLoadCount2 = 0; + const maxRetries2 = 2; + const retryDelay2 = 1000; + newWindow.webContents.on('did-fail-load', (event, errorCode, errorDescription) => { console.error('页面加载失败:', errorDescription); + failLoadCount2++; + + if (failLoadCount2 <= maxRetries2) { + console.log(`页面加载失败,${retryDelay2}ms后进行第${failLoadCount2}次重试...`); + setTimeout(() => { + newWindow.reload(); + }, retryDelay2); + } else { + console.error('页面加载失败次数超过最大重试次数'); + } + }); + + // 页面加载成功时重置失败计数 + newWindow.webContents.on('did-finish-load', () => { + if (failLoadCount2 > 0) { + console.log('页面加载成功,重置失败计数'); + failLoadCount2 = 0; + } }); return 'success'; diff --git a/src/main/window.js b/src/main/window.js index cc983b7..9e603bb 100644 --- a/src/main/window.js +++ b/src/main/window.js @@ -168,18 +168,57 @@ export async function createWindow() { mainWindow.loadURL(h5_client_url) // 监听页面加载失败事件 - mainWindow.webContents.on('did-fail-load', (event, errorCode, errorDescription, validatedURL) => { - logger.error(`主窗口页面加载失败: code=${errorCode}, desc=${errorDescription}, url=${validatedURL}`) - // 跳转到网络错误页面 + let failLoadCount = 0; + const maxRetries = 3; + const retryDelay = 2000; // 2秒后重试 + mainWindow.webContents.on('did-fail-load', async (event, errorCode, errorDescription, validatedURL) => { + logger.error(`主窗口页面加载失败: code=${errorCode}, desc=${errorDescription}, url=${validatedURL}, 失败次数: ${failLoadCount + 1}`) + + // 增加失败计数 + failLoadCount++; + + if (failLoadCount <= maxRetries) { + logger.info(`页面加载失败,${retryDelay}ms后进行第${failLoadCount}次重试...`); + setTimeout(async () => { + try { + const { checkNetworkConnection } = await import('./utils/networkUtils.js'); + const isApiOk = await checkNetworkConnection(); + if (isApiOk) { + logger.info('接口正常,重新加载页面'); + mainWindow.reload(); + } else { + logger.warn('接口异常,跳转到网络错误页面'); + jumpToNetworkErrorPage(); + } + } catch (error) { + logger.error('重试过程中出错:', error); + jumpToNetworkErrorPage(); + } + }, retryDelay); + } else { + // 超过最大重试次数,跳转到网络错误页面 + logger.error(`页面加载失败次数超过${maxRetries}次,跳转到网络错误页面`); + jumpToNetworkErrorPage(); + } + }); + + // 页面加载成功时重置失败计数 + mainWindow.webContents.on('did-finish-load', () => { + if (failLoadCount > 0) { + logger.info('页面加载成功,重置失败计数'); + failLoadCount = 0; + } + }); + + // 跳转到网络错误页面的函数 + function jumpToNetworkErrorPage() { if (is.dev && process.env['ELECTRON_RENDERER_URL']) { mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL'] + '/#/network_error') } else { mainWindow.loadFile(join(__dirname, '../renderer/index.html'), { hash: '/network_error' }) } - - - }) + } // 接口超过30分钟不活动,则退出登录 await tokenExpireTimer() @@ -457,7 +496,50 @@ export async function createNewWindow(url, access_token, refresh_token,sandbox=f difyfullScreenWindow.loadURL(url) + // 添加重试机制的错误处理 + let failLoadCount = 0; + const maxRetries = 3; + const retryDelay = 2000; + difyfullScreenWindow.webContents.on('did-fail-load', async (event, errorCode, errorDescription, validatedURL) => { + logger.error(`全屏窗口页面加载失败: code=${errorCode}, desc=${errorDescription}, url=${validatedURL}, 失败次数: ${failLoadCount + 1}`) + + failLoadCount++; + + if (failLoadCount <= maxRetries) { + logger.info(`全屏窗口页面加载失败,${retryDelay}ms后进行第${failLoadCount}次重试...`); + + setTimeout(async () => { + try { + // 在重试前先检查网络连接 + const { checkDefaultClientConnection } = await import('./utils/networkUtils.js'); + const isNetworkConnected = await checkDefaultClientConnection(3000); + + if (isNetworkConnected) { + logger.info('网络连接正常,重新加载全屏窗口页面'); + difyfullScreenWindow.reload(); + } else { + logger.warn('网络连接异常,关闭全屏窗口'); + difyfullScreenWindow.close(); + } + } catch (error) { + logger.error('全屏窗口重试过程中出错:', error); + difyfullScreenWindow.close(); + } + }, retryDelay); + } else { + logger.error(`全屏窗口页面加载失败次数超过${maxRetries}次,关闭窗口`); + difyfullScreenWindow.close(); + } + }); + + // 页面加载成功时重置失败计数 + difyfullScreenWindow.webContents.on('did-finish-load', () => { + if (failLoadCount > 0) { + logger.info('全屏窗口页面加载成功,重置失败计数'); + failLoadCount = 0; + } + }); } @@ -517,12 +599,55 @@ export function createDrageWindow() { logger.log(h5_client_url) drageWindow.loadURL(h5_client_url) + // 添加重试机制的错误处理 + let drageFailLoadCount = 0; + const drageMaxRetries = 2; + const drageRetryDelay = 1000; + + drageWindow.webContents.on('did-fail-load', async (event, errorCode, errorDescription, validatedURL) => { + logger.error(`悬浮窗口页面加载失败: code=${errorCode}, desc=${errorDescription}, url=${validatedURL}, 失败次数: ${drageFailLoadCount + 1}`) + + drageFailLoadCount++; + + if (drageFailLoadCount <= drageMaxRetries) { + logger.info(`悬浮窗口页面加载失败,${drageRetryDelay}ms后进行第${drageFailLoadCount}次重试...`); + + setTimeout(async () => { + try { + // 在重试前先检查网络连接 + const { checkDefaultClientConnection } = await import('./utils/networkUtils.js'); + const isNetworkConnected = await checkDefaultClientConnection(3000); + + if (isNetworkConnected) { + logger.info('网络连接正常,重新加载悬浮窗口页面'); + drageWindow.reload(); + } else { + logger.warn('网络连接异常,隐藏悬浮窗口'); + drageWindow.hide(); + } + } catch (error) { + logger.error('悬浮窗口重试过程中出错:', error); + drageWindow.hide(); + } + }, drageRetryDelay); + } else { + logger.error(`悬浮窗口页面加载失败次数超过${drageMaxRetries}次,隐藏窗口`); + drageWindow.hide(); + } + }); + + // 页面加载成功时重置失败计数 + drageWindow.webContents.on('did-finish-load', () => { + if (drageFailLoadCount > 0) { + logger.info('悬浮窗口页面加载成功,重置失败计数'); + drageFailLoadCount = 0; + } + }); const { width: screenWidth, height: screenHeight } = screen.getPrimaryDisplay().workAreaSize drageWindow.setPosition(screenWidth - drageWindow.getSize()[0] -100, screenHeight - drageWindow.getSize()[1] - 100) } - export async function tokenExpireTimer(){ let lastLogTime = 0; // 记录上次打印日志的时间 const LOG_INTERVAL = 60000; // 日志打印间隔,60秒打印一次