update default global proxy
This commit is contained in:
		| @ -60,6 +60,10 @@ class WindowController { | ||||
|   async getGlobalProxyInfo(args, event) { | ||||
|     return await windowService.getGlobalProxyInfo(args, event); | ||||
|   } | ||||
|   //验证全局代理修复效果 | ||||
|   async verifyGlobalProxyFix(args, event) { | ||||
|     return await windowService.verifyGlobalProxyFix(args, event); | ||||
|   } | ||||
|   //修改窗口代理配置信息 | ||||
|   async editProxyInfo(args, event) { | ||||
|     return await windowService.editProxyInfo(args, event); | ||||
|  | ||||
| @ -206,6 +206,15 @@ const initializeDatabase = async () => { | ||||
|         password: "TEXT", | ||||
|       }, | ||||
|     }, | ||||
|     user_auth_cache: { | ||||
|       columns: { | ||||
|         id: "INTEGER PRIMARY KEY AUTOINCREMENT", | ||||
|         authData: "TEXT", | ||||
|         createTime: "TEXT", | ||||
|         updateTime: "TEXT", | ||||
|       }, | ||||
|       constraints: [], | ||||
|     }, | ||||
|   }; | ||||
|   // 同步每个表的结构 | ||||
|   for (const [tableName, { columns, constraints }] of Object.entries(tables)) { | ||||
| @ -232,7 +241,7 @@ const initializeTableData = async () => { | ||||
|   const globalProxyConfig = await app.sdb.selectOne("global_proxy_config"); | ||||
|   if (!globalProxyConfig) { | ||||
|     await app.sdb.insert("global_proxy_config", { | ||||
|       proxyStatus: "false",  // 默认关闭全局代理 | ||||
|       proxyStatus: "true",   // 默认开启全局代理 | ||||
|       proxyType: "http", | ||||
|       proxyIp: "", | ||||
|       proxyPort: "", | ||||
| @ -357,6 +366,53 @@ const initializeTableData = async () => { | ||||
|   //1.0.51单独设置数据 | ||||
|   //  await app.sdb.update("translate_config",{translateRoute:"youDao"},{}); | ||||
| }; | ||||
| // 恢复用户认证信息 | ||||
| const restoreAuthInfo = async () => { | ||||
|   try { | ||||
|     // 尝试从数据库恢复用户认证信息 | ||||
|     const authRecord = await app.sdb.selectOne('user_auth_cache'); | ||||
|     if (authRecord && authRecord.authData) { | ||||
|       try { | ||||
|         const authData = JSON.parse(authRecord.authData); | ||||
|         // 验证认证信息的完整性 | ||||
|         if (authData.authKey && authData.userName && authData.machineCode) { | ||||
|           app.authInfo = authData; | ||||
|           logger.info('✅ 已从数据库恢复用户认证信息:', authData.userName); | ||||
|           return true; | ||||
|         } | ||||
|       } catch (parseError) { | ||||
|         logger.error('❌ 解析认证信息失败:', parseError); | ||||
|       } | ||||
|     } | ||||
|  | ||||
|     logger.info('ℹ️ 未找到有效的认证信息缓存'); | ||||
|     return false; | ||||
|   } catch (error) { | ||||
|     logger.error('❌ 恢复认证信息失败:', error); | ||||
|     return false; | ||||
|   } | ||||
| }; | ||||
|  | ||||
| // 保存用户认证信息到数据库 | ||||
| const saveAuthInfo = async (authData) => { | ||||
|   try { | ||||
|     if (!authData) return; | ||||
|  | ||||
|     const authRecord = await app.sdb.selectOne('user_auth_cache'); | ||||
|     const authDataStr = JSON.stringify(authData); | ||||
|  | ||||
|     if (authRecord) { | ||||
|       await app.sdb.update('user_auth_cache', { authData: authDataStr }, { id: authRecord.id }); | ||||
|     } else { | ||||
|       await app.sdb.insert('user_auth_cache', { authData: authDataStr }); | ||||
|     } | ||||
|  | ||||
|     logger.info('✅ 已保存用户认证信息到数据库'); | ||||
|   } catch (error) { | ||||
|     logger.error('❌ 保存认证信息失败:', error); | ||||
|   } | ||||
| }; | ||||
|  | ||||
| class Lifecycle { | ||||
|   /** | ||||
|    * core app have been loaded | ||||
| @ -382,8 +438,15 @@ class Lifecycle { | ||||
|     await initializeDatabase(); | ||||
|     await initializePlatform(); | ||||
|     await initializeTableData(); | ||||
|  | ||||
|     // 恢复用户认证信息 | ||||
|     await restoreAuthInfo(); | ||||
|  | ||||
|     app.viewsMap = new Map(); | ||||
|  | ||||
|     // 将保存认证信息的函数暴露给其他模块使用 | ||||
|     app.saveAuthInfo = saveAuthInfo; | ||||
|  | ||||
|   } | ||||
|   ready; | ||||
|   /** | ||||
|  | ||||
| @ -217,10 +217,15 @@ const checkInterceptLanguage = async (text) => { | ||||
|  | ||||
| // 错误消息检测:拦截把错误当消息发送 | ||||
| const isErrorText = (s) => { | ||||
|   if (!s) return false; | ||||
|   const text = String(s); | ||||
|   const patterns = [ | ||||
|     '翻译失败', '请求失败', 'Client Error', 'Unauthorized', | ||||
|     'HTTPConnectionPool', 'timeout', '超时', '错误', 'Error', | ||||
|     'Connection Error', 'Network Error', 'Request failed', | ||||
|     'Translation failed', 'API Error', '服务器错误' | ||||
|   ]; | ||||
|   return patterns.some((p) => s.includes(p)); | ||||
|   return patterns.some((p) => text.includes(p)); | ||||
| }; | ||||
| const sendMsg = async ()=> { | ||||
|     // 最终文本验证(含错误/空文本/语言拦截),仅当开启拦截开关时检查语言 | ||||
| @ -397,10 +402,14 @@ const addTranslateListener = () => { | ||||
|         const flag = trcConfig.translatePreview; | ||||
|         const textContent = editableDiv.textContent.trim(); | ||||
|         const sendStatus = trcConfig.sendTranslateStatus; | ||||
|  | ||||
|         if(!textContent) { | ||||
|             styledTextarea.setContent('...') | ||||
|             return; | ||||
|         } | ||||
|         if (flag && flag === 'true' && sendStatus === 'true') { | ||||
|  | ||||
|         // 只有在开启翻译预览且开启发送翻译时才进行实时翻译 | ||||
|         if (flag === 'true' && sendStatus === 'true') { | ||||
|             const to = trcConfig.sendTargetLanguage; | ||||
|             styledTextarea.setTranslateStatus(false) | ||||
|             await sendTranslate(textContent,to); | ||||
| @ -421,52 +430,60 @@ const addTranslateListener = () => { | ||||
|             const sendTranslateStatus = trcConfig.sendTranslateStatus; | ||||
|             const sendPreview = trcConfig.translatePreview; | ||||
|             const textContent = editableDiv.textContent.trim(); | ||||
|             const from = trcConfig.sendSourceLanguage; | ||||
|             const to = trcConfig.sendTargetLanguage; | ||||
|             const route = trcConfig.translateRoute; | ||||
|             const mode = trcConfig.mode; | ||||
|             const args = {text:textContent,from:from,to: to,route:route,mode:mode}; | ||||
|  | ||||
|             if (sendPreview === 'true'&& sendTranslateStatus === 'true') { | ||||
|                 const status = styledTextarea.getTranslateStatus() | ||||
|                 const isProcess = styledTextarea.getIsProcessing() | ||||
|                 if (status === true && isProcess === false) { | ||||
|                     const translateText = styledTextarea.getContent(); | ||||
|                     await inputMsg(translateText) | ||||
|                     await sendMsg() | ||||
|                     styledTextarea.setTranslateStatus(false) | ||||
|                     styledTextarea.setContent('...') | ||||
|                     return; | ||||
|                 }else { | ||||
|                     console.log('正在处理翻译中:') | ||||
|                     return; | ||||
|                 } | ||||
|             }else if (textContent && sendTranslateStatus === 'true') { | ||||
|                 isProcessing = true; | ||||
|                 styledTextarea.setContent('翻译中...') | ||||
|                 const res = await ipc.translateText(args) | ||||
|                 if (res.status) { | ||||
|                     const translateText = res.data; | ||||
|                     await inputMsg(translateText) | ||||
|                     styledTextarea.setContent(translateText); | ||||
|                     setTimeout(async ()=>{ | ||||
|                         isProcessing = false; | ||||
|             // 如果开启了发送翻译功能,则进行翻译处理 | ||||
|             if (sendTranslateStatus === 'true') { | ||||
|                 const from = trcConfig.sendSourceLanguage; | ||||
|                 const to = trcConfig.sendTargetLanguage; | ||||
|                 const route = trcConfig.translateRoute; | ||||
|                 const mode = trcConfig.mode; | ||||
|                 const args = {text:textContent,from:from,to: to,route:route,mode:mode}; | ||||
|  | ||||
|                 if (sendPreview === 'true') { | ||||
|                     const status = styledTextarea.getTranslateStatus() | ||||
|                     const isProcess = styledTextarea.getIsProcessing() | ||||
|                     if (status === true && isProcess === false) { | ||||
|                         const translateText = styledTextarea.getContent(); | ||||
|                         await inputMsg(translateText) | ||||
|                         await sendMsg() | ||||
|                         styledTextarea.setTranslateStatus(false) | ||||
|                         styledTextarea.setContent('...') | ||||
|                         return; | ||||
|                     }else { | ||||
|                         console.log('正在处理翻译中:') | ||||
|                         return; | ||||
|                     } | ||||
|                 }else if (textContent) { | ||||
|                     isProcessing = true; | ||||
|                     styledTextarea.setContent('翻译中...') | ||||
|                     const res = await ipc.translateText(args) | ||||
|                     if (res.status) { | ||||
|                         const translateText = res.data; | ||||
|                         await inputMsg(translateText) | ||||
|                         styledTextarea.setContent(translateText); | ||||
|                         setTimeout(async ()=>{ | ||||
|                             isProcessing = false; | ||||
|                             await sendMsg() | ||||
|                             styledTextarea.setContent('...'); | ||||
|                         },500) | ||||
|                     }else { | ||||
|                         alert('翻译失败,未发送。请稍后重试或更换翻译通道。'); | ||||
|                         styledTextarea.setContent('...'); | ||||
|                     },500) | ||||
|                 }else { | ||||
|                     alert('翻译失败,未发送。请稍后重试或更换翻译通道。'); | ||||
|                     styledTextarea.setContent('...'); | ||||
|                     isProcessing = false; | ||||
|                     return; | ||||
|                         isProcessing = false; | ||||
|                         return; | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
|             const chineseDetectionStatus = trcConfig.chineseDetectionStatus; | ||||
|             if (chineseDetectionStatus === 'true' && sendTranslateStatus === 'true') { | ||||
|                 const textContent = editableDiv.textContent.trim(); | ||||
|                 if (containsChinese(textContent)) { | ||||
|                     return; | ||||
|  | ||||
|                 // 中文检测逻辑:只有在开启发送翻译时才检测中文 | ||||
|                 const chineseDetectionStatus = trcConfig.chineseDetectionStatus; | ||||
|                 if (chineseDetectionStatus === 'true') { | ||||
|                     if (containsChinese(textContent)) { | ||||
|                         return; | ||||
|                     } | ||||
|                 } | ||||
|  | ||||
|                 // 如果开启了翻译功能,这里就不再继续发送原文了 | ||||
|                 return; | ||||
|             } | ||||
|             await sendMsg(); | ||||
|         } | ||||
| @ -485,33 +502,55 @@ const addTranslateListener = () => { | ||||
|                 styIsProcess:isProcess, | ||||
|                 sendTranslateStatus:sendTranslateStatus, | ||||
|             }) | ||||
|  | ||||
|             // 如果正在处理翻译,阻止发送 | ||||
|             if (isProcess === true) { | ||||
|                 event.preventDefault(); | ||||
|                 event.stopPropagation(); | ||||
|                 event.stopImmediatePropagation(); | ||||
|             }else { | ||||
|                 if (event.isSimulated === true) { | ||||
|                     //不做处理 | ||||
|                 }else if (sendTranslateStatus === 'true' && status === true) { | ||||
|                     const translateText = styledTextarea.getContent(); | ||||
|                     inputMsg(translateText).then(()=>{}) | ||||
|                 } | ||||
|                 return; | ||||
|             } | ||||
|  | ||||
|             // 如果是模拟点击事件,不做处理 | ||||
|             if (event.isSimulated === true) { | ||||
|                 return; | ||||
|             } | ||||
|  | ||||
|             // 如果开启了翻译功能且有翻译结果,使用翻译文本 | ||||
|             if (sendTranslateStatus === 'true' && status === true) { | ||||
|                 const translateText = styledTextarea.getContent(); | ||||
|                 inputMsg(translateText).then(()=>{}) | ||||
|             } | ||||
|         } | ||||
|     }); | ||||
| }; | ||||
|  | ||||
| const updateConfigInfo = async () => { | ||||
|     const currentUserId = getCurrentUserId(); | ||||
|     const args = {platform: 'Telegram', userId: currentUserId || ''}; | ||||
|     const res = await ipc.getTranslateConfig(args) | ||||
|     if (res.status) { | ||||
|         styledTextarea.setTitle(res.data.zhName); | ||||
|         trcConfig = res.data; | ||||
|         console.log('获取配置信息:args:',args) | ||||
|     }else { | ||||
|     try { | ||||
|         const currentUserId = getCurrentUserId(); | ||||
|         const args = {platform: 'Telegram', userId: currentUserId || ''}; | ||||
|  | ||||
|         // 检查必要的依赖是否存在 | ||||
|         if (!ipc || !ipc.getTranslateConfig) { | ||||
|             console.warn('ipc 对象未准备就绪,跳过配置更新'); | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|         const res = await ipc.getTranslateConfig(args) | ||||
|         if (res.status) { | ||||
|             // 检查 styledTextarea 是否存在 | ||||
|             if (styledTextarea && styledTextarea.setTitle) { | ||||
|                 styledTextarea.setTitle(res.data.zhName); | ||||
|             } | ||||
|             trcConfig = res.data; | ||||
|             console.log('获取配置信息:args:',args) | ||||
|         }else { | ||||
|             trcConfig = {}; | ||||
|             console.error('获取配置信息失败:',res); | ||||
|         } | ||||
|     } catch (error) { | ||||
|         console.error('updateConfigInfo 执行出错:', error); | ||||
|         trcConfig = {}; | ||||
|         console.error('获取配置信息失败:',res); | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -731,7 +770,7 @@ const monitorMainNode = ()=> { | ||||
|         await checkAndDisplayExistingTranslation(text, leftDiv, rightDiv); | ||||
|  | ||||
|         const receiveTranslateStatus = trcConfig.receiveTranslateStatus; | ||||
|         logger.info('发送消息 当前配置:',trcConfig) | ||||
|         console.log('发送消息 当前配置:',trcConfig) | ||||
|         if (receiveTranslateStatus === 'true') { | ||||
|             let text = getMsgText(msgSpan.parentNode); | ||||
|             text = text.replace(/\n/g, '<br>'); | ||||
|  | ||||
| @ -21,6 +21,13 @@ window.addEventListener("languageChanged", function (event) { | ||||
|   window.updateLanguage(language); | ||||
| }); | ||||
|  | ||||
| // 监听翻译配置更新事件 | ||||
| window.addEventListener("translateConfigChanged", function (event) { | ||||
|   console.log("WhatsApp: 收到翻译配置更新事件", event.detail); | ||||
|   // 立即更新配置 | ||||
|   updateConfigInfo(); | ||||
| }); | ||||
|  | ||||
| // 更新翻译相关文本 | ||||
| function updateTranslateTexts() { | ||||
|   // 更新输入框placeholder | ||||
| @ -425,6 +432,9 @@ const isErrorText = (text) => { | ||||
|   const patterns = [ | ||||
|     '翻译失败', '请求失败', 'Client Error', 'Unauthorized', | ||||
|     'HTTPConnectionPool', 'timeout', '超时', '错误', 'Error', | ||||
|     '用户信息不存在', '账户不存在', '认证失败', '登录失败', | ||||
|     'Authentication failed', 'User not found', 'Invalid credentials', | ||||
|     'Network Error', 'Connection failed', '连接失败', '网络错误' | ||||
|   ]; | ||||
|   return patterns.some((p) => s.includes(p)); | ||||
| }; | ||||
| @ -807,6 +817,16 @@ const addTranslateListener = () => { | ||||
|     const translateStatus = styledTextarea.getTranslateStatus(); | ||||
|     const textContent = (await whatsappContent())?.trim(); | ||||
|  | ||||
|     // 调试日志:显示当前配置状态 | ||||
|     console.log('📤 WhatsApp: 发送消息检查配置', { | ||||
|       sendTranslateStatus: sendTranslateStatus, | ||||
|       sendPreview: sendPreview, | ||||
|       translateStatus: translateStatus, | ||||
|       textLength: textContent?.length || 0, | ||||
|       configRaw: trcConfig.sendTranslateStatus, | ||||
|       timestamp: new Date().toISOString() | ||||
|     }); | ||||
|  | ||||
|     // 跳过空消息 | ||||
|     if (!textContent) return; | ||||
|  | ||||
| @ -847,7 +867,8 @@ const addTranslateListener = () => { | ||||
|           await inputMsg(res.data); | ||||
|           sendMsg(); | ||||
|         } else { | ||||
|           styledTextarea.setContent(res.message); | ||||
|           alert('翻译失败,未发送。请稍后重试或更换翻译通道。'); | ||||
|           styledTextarea.setContent('...'); | ||||
|           // sendMsg(); | ||||
|         } | ||||
|         styledTextarea.setTranslateStatus(false); | ||||
| @ -933,15 +954,49 @@ const addTranslateListener = () => { | ||||
|  | ||||
| // addTranslateListener 结束 | ||||
| const updateConfigInfo = async () => { | ||||
|   const currentUserId = getCurrentUserId(); | ||||
|   const args = { platform: "WhatsApp", userId: currentUserId || "" }; | ||||
|   const res = await ipc.getTranslateConfig(args); | ||||
|   if (res.status) { | ||||
|     styledTextarea.setTitle(res.data.zhName); | ||||
|     trcConfig = res.data; | ||||
|   } else { | ||||
|   try { | ||||
|     const currentUserId = getCurrentUserId(); | ||||
|     const args = { platform: "WhatsApp", userId: currentUserId || "" }; | ||||
|  | ||||
|     // 检查必要的依赖是否存在 | ||||
|     if (!ipc || !ipc.getTranslateConfig) { | ||||
|       console.warn('ipc 对象未准备就绪,跳过配置更新'); | ||||
|       return; | ||||
|     } | ||||
|  | ||||
|     console.log('🔄 WhatsApp: 正在更新翻译配置...', args); | ||||
|     const res = await ipc.getTranslateConfig(args); | ||||
|     if (res.status) { | ||||
|       const oldSendStatus = trcConfig.sendTranslateStatus; | ||||
|       const newSendStatus = res.data.sendTranslateStatus; | ||||
|  | ||||
|       // 检查 styledTextarea 是否存在 | ||||
|       if (styledTextarea && styledTextarea.setTitle) { | ||||
|         styledTextarea.setTitle(res.data.zhName); | ||||
|       } | ||||
|       trcConfig = res.data; | ||||
|  | ||||
|       // 记录关键配置变化 | ||||
|       if (oldSendStatus !== newSendStatus) { | ||||
|         console.log('📝 WhatsApp: 发送翻译状态变化:', { | ||||
|           from: oldSendStatus, | ||||
|           to: newSendStatus, | ||||
|           timestamp: new Date().toISOString() | ||||
|         }); | ||||
|       } | ||||
|  | ||||
|       console.log('✅ WhatsApp: 翻译配置更新成功', { | ||||
|         sendTranslateStatus: trcConfig.sendTranslateStatus, | ||||
|         translateRoute: trcConfig.translateRoute, | ||||
|         zhName: trcConfig.zhName | ||||
|       }); | ||||
|     } else { | ||||
|       trcConfig = {}; | ||||
|       console.error("获取配置信息失败:", res); | ||||
|     } | ||||
|   } catch (error) { | ||||
|     console.error('updateConfigInfo 执行出错:', error); | ||||
|     trcConfig = {}; | ||||
|     console.error("获取配置信息失败:", res); | ||||
|   } | ||||
| }; | ||||
|  | ||||
|  | ||||
| @ -97,6 +97,12 @@ class SystemService { | ||||
|           } | ||||
|  | ||||
|           app.authInfo = data; | ||||
|  | ||||
|           // 保存认证信息到数据库 | ||||
|           if (app.saveAuthInfo) { | ||||
|             await app.saveAuthInfo(data); | ||||
|           } | ||||
|  | ||||
|           return { status: true, message: 'login.success', data: data }; | ||||
|  | ||||
|         case 401: // 参数缺失 - 请求缺少必要参数或参数格式错误 | ||||
| @ -182,6 +188,12 @@ class SystemService { | ||||
|             userId: user_id//用户id | ||||
|           } | ||||
|           app.authInfo = data; | ||||
|  | ||||
|           // 保存认证信息到数据库 | ||||
|           if (app.saveAuthInfo) { | ||||
|             await app.saveAuthInfo(data); | ||||
|           } | ||||
|  | ||||
|           return { status: true, message: 'login.success', data: data }; | ||||
|  | ||||
|         case 401: // 参数缺失 - 请求缺少必要参数或参数格式错误 | ||||
|  | ||||
| @ -350,7 +350,26 @@ class TranslateService { | ||||
|       ); | ||||
|       // 返回最新配置(可选) | ||||
|       const updatedConfig = await app.sdb.selectOne('translate_config', { id: id }); | ||||
|       if (partitionId) await this._routeUpdateNotify(partitionId) | ||||
|  | ||||
|       // 异步执行通知,不阻塞主流程 | ||||
|       if (partitionId) { | ||||
|         // 检查是否为全局配置更新 | ||||
|         const isGlobalConfig = updatedConfig && updatedConfig.userId === "" && updatedConfig.platform; | ||||
|  | ||||
|         if (isGlobalConfig) { | ||||
|           // 全局配置更新:通知所有相关平台的会话(包括当前会话) | ||||
|           console.log(`🌐 检测到全局配置更新,平台: ${updatedConfig.platform}`); | ||||
|           this._broadcastGlobalConfigUpdate(updatedConfig.platform).catch(notifyError => { | ||||
|             console.error('全局配置广播通知失败,但不影响主流程:', notifyError); | ||||
|           }); | ||||
|         } else { | ||||
|           // 个人配置更新:只通知当前会话 | ||||
|           this._routeUpdateNotify(partitionId).catch(notifyError => { | ||||
|             console.error('配置更新通知失败,但不影响主流程:', notifyError); | ||||
|           }); | ||||
|         } | ||||
|       } | ||||
|  | ||||
|       return { | ||||
|         success: true, | ||||
|         data: updatedConfig | ||||
| @ -379,7 +398,14 @@ class TranslateService { | ||||
|           //更新数据 | ||||
|           const count = await app.sdb.update('translate_config', { friendTranslateStatus: status }, { id: configById.id }) | ||||
|           logger.info('状态修改成功:', args) | ||||
|           if (partitionId) await this._routeUpdateNotify(partitionId) | ||||
|  | ||||
|           // 异步执行通知,不阻塞主流程 | ||||
|           if (partitionId) { | ||||
|             this._routeUpdateNotify(partitionId).catch(notifyError => { | ||||
|               console.error('状态更新通知失败,但不影响主流程:', notifyError); | ||||
|             }); | ||||
|           } | ||||
|  | ||||
|           return { status: true, message: getI18nText('dataUpdateSuccess') } | ||||
|         } | ||||
|       } | ||||
| @ -730,16 +756,111 @@ class TranslateService { | ||||
|   //翻译线路更改通知 | ||||
|   async _routeUpdateNotify(partitionId) { | ||||
|     if (!partitionId) return; | ||||
|     const view = app.viewsMap.get(partitionId); | ||||
|     if (view && !view.webContents.isDestroyed()) { | ||||
|  | ||||
|     try { | ||||
|       const view = app.viewsMap.get(partitionId); | ||||
|       if (!view || view.webContents.isDestroyed()) { | ||||
|         console.warn('视图不存在或已销毁,跳过配置更新通知'); | ||||
|         return; | ||||
|       } | ||||
|  | ||||
|       // 检查页面是否已加载完成 | ||||
|       const isReady = await view.webContents.executeJavaScript('document.readyState === "complete"'); | ||||
|       if (!isReady) { | ||||
|         console.warn('页面未完全加载,延迟执行配置更新'); | ||||
|         setTimeout(() => this._routeUpdateNotify(partitionId), 1000); | ||||
|         return; | ||||
|       } | ||||
|  | ||||
|       // 设置标志位,避免翻译历史消息 | ||||
|       await view.webContents.executeJavaScript(` | ||||
|         window.isConfigUpdating = true; | ||||
|         updateConfigInfo(); | ||||
|         setTimeout(() => { | ||||
|           window.isConfigUpdating = false; | ||||
|         }, 1000); | ||||
|       `) | ||||
|         (function() { | ||||
|           try { | ||||
|             window.isConfigUpdating = true; | ||||
|  | ||||
|             // 触发配置更新事件 | ||||
|             try { | ||||
|               const configUpdateEvent = new CustomEvent('translateConfigChanged', { | ||||
|                 detail: { timestamp: Date.now(), source: 'backend' } | ||||
|               }); | ||||
|               window.dispatchEvent(configUpdateEvent); | ||||
|               console.log('已触发翻译配置更新事件'); | ||||
|             } catch (eventError) { | ||||
|               console.error('触发配置更新事件失败:', eventError); | ||||
|             } | ||||
|  | ||||
|             // 检查必要的对象是否存在 | ||||
|             if (typeof updateConfigInfo === 'function') { | ||||
|               // 延迟执行,确保页面完全准备好 | ||||
|               setTimeout(() => { | ||||
|                 try { | ||||
|                   updateConfigInfo(); | ||||
|                 } catch (updateError) { | ||||
|                   console.error('updateConfigInfo 执行出错:', updateError); | ||||
|                 } | ||||
|               }, 100); | ||||
|             } else { | ||||
|               console.warn('updateConfigInfo 函数不存在,可能页面还未完全加载'); | ||||
|             } | ||||
|  | ||||
|             setTimeout(() => { | ||||
|               window.isConfigUpdating = false; | ||||
|             }, 2000); | ||||
|  | ||||
|           } catch (error) { | ||||
|             console.error('配置更新脚本执行出错:', error); | ||||
|             window.isConfigUpdating = false; | ||||
|           } | ||||
|         })(); | ||||
|       `); | ||||
|  | ||||
|     } catch (error) { | ||||
|       console.error('执行配置更新脚本失败:', error); | ||||
|       // 不重新抛出错误,避免影响主流程 | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   // 广播全局配置更新到所有相关平台的会话 | ||||
|   async _broadcastGlobalConfigUpdate(platform) { | ||||
|     if (!platform) return; | ||||
|  | ||||
|     try { | ||||
|       console.log(`🌐 开始广播全局配置更新,平台: ${platform}`); | ||||
|       let notifiedCount = 0; | ||||
|       let totalViews = 0; | ||||
|  | ||||
|       // 遍历所有视图,找到匹配平台的会话 | ||||
|       for (const [partitionId, view] of app.viewsMap) { | ||||
|         totalViews++; | ||||
|  | ||||
|         // 检查视图是否有效 | ||||
|         if (!view || view.webContents.isDestroyed()) { | ||||
|           console.log(`⚠️ 视图无效或已销毁: ${partitionId}`); | ||||
|           continue; | ||||
|         } | ||||
|  | ||||
|         try { | ||||
|           // 检查会话是否属于目标平台 | ||||
|           const session = await app.sdb.selectOne("session_list", { | ||||
|             partitionId: partitionId, | ||||
|           }); | ||||
|  | ||||
|           if (session && session.platform === platform) { | ||||
|             console.log(`📡 通知 ${platform} 会话: ${partitionId} (用户: ${session.userId || '未知'})`); | ||||
|  | ||||
|             // 通知该会话更新配置 | ||||
|             await this._routeUpdateNotify(partitionId); | ||||
|             notifiedCount++; | ||||
|           } | ||||
|         } catch (sessionError) { | ||||
|           console.warn(`查询会话信息失败 ${partitionId}:`, sessionError.message); | ||||
|         } | ||||
|       } | ||||
|  | ||||
|       console.log(`✅ 全局配置广播完成,平台: ${platform}, 总视图: ${totalViews}, 已通知: ${notifiedCount}`); | ||||
|  | ||||
|     } catch (error) { | ||||
|       console.error('广播全局配置更新失败:', error); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|  | ||||
| @ -565,6 +565,83 @@ class WindowService { | ||||
|     if (config) return { status: true, message: "查询成功", data: config }; | ||||
|     return { status: false, message: "查询失败" }; | ||||
|   } | ||||
|  | ||||
|   //验证全局代理修复效果 | ||||
|   async verifyGlobalProxyFix(args, event) { | ||||
|     try { | ||||
|       console.log('🧪 验证全局代理修复效果...'); | ||||
|  | ||||
|       // 获取当前配置 | ||||
|       const config = await app.sdb.selectOne("global_proxy_config"); | ||||
|       if (!config) { | ||||
|         return { | ||||
|           status: false, | ||||
|           message: "未找到全局代理配置", | ||||
|           data: { error: "no_config" } | ||||
|         }; | ||||
|       } | ||||
|  | ||||
|       // 检查配置完整性 | ||||
|       const isEnabled = config.proxyStatus === "true"; | ||||
|       const hasValidConfig = config.proxyIp && config.proxyPort && config.proxyType; | ||||
|       const hasAuth = config.userVerifyStatus === "true"; | ||||
|       const hasAuthCredentials = config.username && config.password; | ||||
|  | ||||
|       // 检查是否有重复配置 | ||||
|       const allConfigs = await app.sdb.select("global_proxy_config"); | ||||
|       const hasDuplicates = allConfigs.length > 1; | ||||
|  | ||||
|       const result = { | ||||
|         config: { | ||||
|           id: config.id, | ||||
|           proxyStatus: config.proxyStatus, | ||||
|           proxyType: config.proxyType, | ||||
|           proxyIp: config.proxyIp, | ||||
|           proxyPort: config.proxyPort, | ||||
|           userVerifyStatus: config.userVerifyStatus, | ||||
|           username: config.username, | ||||
|           hasPassword: !!config.password | ||||
|         }, | ||||
|         validation: { | ||||
|           isEnabled, | ||||
|           hasValidConfig, | ||||
|           hasAuth, | ||||
|           hasAuthCredentials: hasAuth ? hasAuthCredentials : true, // 如果不需要认证,则认为凭据有效 | ||||
|           hasDuplicates, | ||||
|           configCount: allConfigs.length | ||||
|         }, | ||||
|         assessment: { | ||||
|           overall: isEnabled ? (hasValidConfig ? 'configured' : 'enabled_awaiting_config') : 'disabled', | ||||
|           issues: [] | ||||
|         } | ||||
|       }; | ||||
|  | ||||
|       // 收集问题(注意:开启但配置为空是正常的初始状态) | ||||
|       if (isEnabled && hasValidConfig && hasAuth && !hasAuthCredentials) { | ||||
|         result.assessment.issues.push('启用了认证但缺少用户名或密码'); | ||||
|       } | ||||
|       if (hasDuplicates) { | ||||
|         result.assessment.issues.push(`发现${allConfigs.length}条重复配置`); | ||||
|       } | ||||
|       // 开启但配置为空是正常的初始状态,不算问题 | ||||
|  | ||||
|       console.log('验证结果:', result); | ||||
|  | ||||
|       return { | ||||
|         status: true, | ||||
|         message: "验证完成", | ||||
|         data: result | ||||
|       }; | ||||
|  | ||||
|     } catch (error) { | ||||
|       console.error('验证全局代理修复效果失败:', error); | ||||
|       return { | ||||
|         status: false, | ||||
|         message: `验证失败: ${error.message}`, | ||||
|         data: { error: error.message } | ||||
|       }; | ||||
|     } | ||||
|   } | ||||
|   //获取代理配置信息 | ||||
|   async getProxyInfo(args, event) { | ||||
|     const { partitionId } = args; | ||||
|  | ||||
| @ -95,6 +95,9 @@ const ipcApiRoute = { | ||||
|   //翻译缓存相关 | ||||
|   clearTranslateCache: 'controller/translate/clearTranslateCache', | ||||
|   refreshSessionTranslateButtons: 'controller/translate/refreshSessionTranslateButtons', | ||||
|  | ||||
|   //验证修复效果 | ||||
|   verifyGlobalProxyFix: 'controller/window/verifyGlobalProxyFix', | ||||
| } | ||||
|  | ||||
| export { | ||||
|  | ||||
| @ -118,6 +118,10 @@ const localProxyStatus = ref('false'); | ||||
|  | ||||
| const syncProxyStatus = () => { | ||||
|   localProxyStatus.value = proxyInfo.value.proxyStatus; | ||||
|   console.log('同步代理状态:', { | ||||
|     from: proxyInfo.value.proxyStatus, | ||||
|     to: localProxyStatus.value | ||||
|   }); | ||||
| }; | ||||
|  | ||||
| const onProxySwitchChange = async (val) => { | ||||
| @ -216,10 +220,23 @@ const getConfigInfo = async () => { | ||||
|     if (res.status) { | ||||
|       Object.assign(proxyInfo.value, res.data); | ||||
|       syncProxyStatus(); | ||||
|  | ||||
|       // 确保状态同步,特别是在初始化时 | ||||
|       console.log('全局代理配置加载:', { | ||||
|         proxyStatus: proxyInfo.value.proxyStatus, | ||||
|         localProxyStatus: localProxyStatus.value | ||||
|       }); | ||||
|  | ||||
|       // 强制同步状态 | ||||
|       if (proxyInfo.value.proxyStatus !== localProxyStatus.value) { | ||||
|         console.log('检测到状态不一致,强制同步'); | ||||
|         localProxyStatus.value = proxyInfo.value.proxyStatus; | ||||
|       } | ||||
|     } else { | ||||
|       console.log(res); | ||||
|       console.log('获取全局代理配置失败:', res); | ||||
|     } | ||||
|   } catch (err) { | ||||
|     console.error('获取全局代理配置异常:', err); | ||||
|   } finally { | ||||
|     addWatchers(); | ||||
|   } | ||||
| @ -246,8 +263,174 @@ const testGlobalProxy = async () => { | ||||
|   } | ||||
| } | ||||
|  | ||||
| onMounted(() => { | ||||
|   getConfigInfo(); | ||||
| // 验证修复效果的函数 | ||||
| const verifyFixEffect = () => { | ||||
|   console.log('🧪 验证全局代理修复效果...\n'); | ||||
|  | ||||
|   console.log('📋 当前前端状态:'); | ||||
|   console.log('   proxyInfo.proxyStatus:', proxyInfo.value.proxyStatus); | ||||
|   console.log('   localProxyStatus:', localProxyStatus.value); | ||||
|   console.log('   proxyIp:', proxyInfo.value.proxyIp); | ||||
|   console.log('   proxyPort:', proxyInfo.value.proxyPort); | ||||
|   console.log('   proxyType:', proxyInfo.value.proxyType); | ||||
|   console.log('   userVerifyStatus:', proxyInfo.value.userVerifyStatus); | ||||
|   console.log('   username:', proxyInfo.value.username); | ||||
|   console.log('   password:', proxyInfo.value.password ? '已设置' : '未设置'); | ||||
|  | ||||
|   console.log('\n🔍 状态验证:'); | ||||
|   const isEnabled = localProxyStatus.value === 'true'; | ||||
|   const hasValidConfig = proxyInfo.value.proxyIp && proxyInfo.value.proxyPort; | ||||
|  | ||||
|   if (isEnabled) { | ||||
|     console.log('✅ 全局代理开关状态: 开启'); | ||||
|     if (hasValidConfig) { | ||||
|       console.log('✅ 代理配置: 完整'); | ||||
|       console.log('🎯 验证结果: 修复成功 - 状态正确显示为开启且配置完整'); | ||||
|     } else { | ||||
|       console.log('ℹ️  代理配置: 待配置(IP和端口为空是正常的初始状态)'); | ||||
|       console.log('🎯 验证结果: 修复成功 - 状态正确显示为开启,等待用户配置'); | ||||
|     } | ||||
|   } else { | ||||
|     console.log('ℹ️  全局代理开关状态: 关闭'); | ||||
|     console.log('🎯 验证结果: 代理处于关闭状态'); | ||||
|   } | ||||
|  | ||||
|   console.log('\n📊 修复效果评估:'); | ||||
|   console.log('   1. 状态读取: ' + (proxyInfo.value.proxyStatus ? '✅ 正常' : '❌ 异常')); | ||||
|   console.log('   2. 界面同步: ' + (proxyInfo.value.proxyStatus === localProxyStatus.value ? '✅ 一致' : '❌ 不一致')); | ||||
|   console.log('   3. 配置状态: ' + (hasValidConfig ? '✅ 已配置' : 'ℹ️  待配置(正常)')); | ||||
|  | ||||
|   return { | ||||
|     isEnabled, | ||||
|     hasValidConfig, | ||||
|     isConsistent: proxyInfo.value.proxyStatus === localProxyStatus.value | ||||
|   }; | ||||
| } | ||||
|  | ||||
| // 完整的验证函数(调用后端接口) | ||||
| const verifyGlobalProxyFixComplete = async () => { | ||||
|   console.log('🧪 开始完整验证全局代理修复效果...\n'); | ||||
|  | ||||
|   try { | ||||
|     // 1. 前端状态验证 | ||||
|     console.log('📋 第一步:前端状态验证'); | ||||
|     const frontendResult = verifyFixEffect(); | ||||
|  | ||||
|     // 2. 后端状态验证 | ||||
|     console.log('\n📋 第二步:后端状态验证'); | ||||
|     const backendRes = await ipc.invoke(ipcApiRoute.verifyGlobalProxyFix, {}); | ||||
|  | ||||
|     if (backendRes.status) { | ||||
|       const data = backendRes.data; | ||||
|  | ||||
|       console.log('📊 后端验证结果:'); | ||||
|       console.log('   配置状态:', data.config.proxyStatus === 'true' ? '🟢 启用' : '🔴 禁用'); | ||||
|       console.log('   代理地址:', `${data.config.proxyIp}:${data.config.proxyPort}`); | ||||
|       console.log('   代理类型:', data.config.proxyType); | ||||
|       console.log('   认证状态:', data.config.userVerifyStatus === 'true' ? '🟢 启用' : '🔴 禁用'); | ||||
|       console.log('   用户名:', data.config.username); | ||||
|       console.log('   密码状态:', data.config.hasPassword ? '🟢 已设置' : '🔴 未设置'); | ||||
|  | ||||
|       console.log('\n🔍 配置验证:'); | ||||
|       console.log('   是否启用:', data.validation.isEnabled ? '✅ 是' : '❌ 否'); | ||||
|       console.log('   配置完整:', data.validation.hasValidConfig ? '✅ 是' : '❌ 否'); | ||||
|       console.log('   认证配置:', data.validation.hasAuthCredentials ? '✅ 完整' : '❌ 不完整'); | ||||
|       console.log('   重复配置:', data.validation.hasDuplicates ? '⚠️  有' : '✅ 无'); | ||||
|       console.log('   配置数量:', data.validation.configCount); | ||||
|  | ||||
|       console.log('\n🎯 整体评估:', data.assessment.overall); | ||||
|       if (data.assessment.issues.length > 0) { | ||||
|         console.log('⚠️  发现问题:'); | ||||
|         data.assessment.issues.forEach((issue, index) => { | ||||
|           console.log(`   ${index + 1}. ${issue}`); | ||||
|         }); | ||||
|       } else { | ||||
|         console.log('✅ 无问题发现'); | ||||
|       } | ||||
|  | ||||
|       // 3. 前后端一致性检查 | ||||
|       console.log('\n📋 第三步:前后端一致性检查'); | ||||
|       const frontendStatus = localProxyStatus.value; | ||||
|       const backendStatus = data.config.proxyStatus; | ||||
|  | ||||
|       if (frontendStatus === backendStatus) { | ||||
|         console.log('✅ 前后端状态一致'); | ||||
|       } else { | ||||
|         console.log('❌ 前后端状态不一致:'); | ||||
|         console.log(`   前端状态: ${frontendStatus}`); | ||||
|         console.log(`   后端状态: ${backendStatus}`); | ||||
|       } | ||||
|  | ||||
|       // 4. 修复效果总结 | ||||
|       console.log('\n🎯 修复效果总结:'); | ||||
|       const isSuccess = frontendStatus === backendStatus && | ||||
|                        (data.assessment.overall === 'configured' || | ||||
|                         data.assessment.overall === 'enabled_awaiting_config' || | ||||
|                         data.assessment.overall === 'disabled') && | ||||
|                        data.assessment.issues.length === 0; | ||||
|  | ||||
|       if (isSuccess) { | ||||
|         if (data.assessment.overall === 'enabled_awaiting_config') { | ||||
|           console.log('🎉 修复成功!全局代理已开启,等待用户配置'); | ||||
|         } else if (data.assessment.overall === 'configured') { | ||||
|           console.log('🎉 修复成功!全局代理功能完全正常'); | ||||
|         } else { | ||||
|           console.log('🎉 修复成功!全局代理功能正常'); | ||||
|         } | ||||
|       } else { | ||||
|         console.log('⚠️  修复需要进一步调整'); | ||||
|       } | ||||
|  | ||||
|       return { | ||||
|         success: isSuccess, | ||||
|         frontend: frontendResult, | ||||
|         backend: data, | ||||
|         consistent: frontendStatus === backendStatus | ||||
|       }; | ||||
|  | ||||
|     } else { | ||||
|       console.error('❌ 后端验证失败:', backendRes.message); | ||||
|       return { success: false, error: backendRes.message }; | ||||
|     } | ||||
|  | ||||
|   } catch (error) { | ||||
|     console.error('❌ 验证过程出错:', error); | ||||
|     return { success: false, error: error.message }; | ||||
|   } | ||||
| }; | ||||
|  | ||||
| // 暴露到全局,方便在控制台调用 | ||||
| window.verifyGlobalProxyFix = verifyFixEffect; | ||||
| window.verifyGlobalProxyFixComplete = verifyGlobalProxyFixComplete; | ||||
|  | ||||
| onMounted(async () => { | ||||
|   console.log('🔍 全局代理页面初始化...'); | ||||
|  | ||||
|   // 先获取数据库中的实际配置状态 | ||||
|   console.log('📡 正在从数据库获取全局代理配置...'); | ||||
|   await getConfigInfo(); | ||||
|  | ||||
|   // 验证状态一致性 | ||||
|   console.log('🔍 验证状态一致性:', { | ||||
|     数据库状态: proxyInfo.value.proxyStatus, | ||||
|     界面状态: localProxyStatus.value, | ||||
|     代理IP: proxyInfo.value.proxyIp, | ||||
|     代理端口: proxyInfo.value.proxyPort | ||||
|   }); | ||||
|  | ||||
|   // 状态验证和提示 | ||||
|   setTimeout(() => { | ||||
|     if (localProxyStatus.value === 'true') { | ||||
|       if (!proxyInfo.value.proxyIp || !proxyInfo.value.proxyPort) { | ||||
|         console.log('ℹ️  全局代理已开启,等待用户配置IP和端口'); | ||||
|         console.log('💡 提示:这是正常的初始状态,请在界面中配置代理信息'); | ||||
|       } else { | ||||
|         console.log('✅ 全局代理配置完整,状态正常'); | ||||
|       } | ||||
|     } else { | ||||
|       console.log('ℹ️  全局代理处于关闭状态'); | ||||
|     } | ||||
|   }, 500); | ||||
| }) | ||||
| </script> | ||||
|  | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 unknown
					unknown