diff --git a/src/main/modules/window.ts b/src/main/modules/window.ts index d4fef40..8b0241e 100644 --- a/src/main/modules/window.ts +++ b/src/main/modules/window.ts @@ -1,11 +1,43 @@ -import { BrowserWindow, shell, ipcMain, app } from 'electron'; +import { BrowserWindow, shell, ipcMain, app, session } from 'electron'; import { is } from '@electron-toolkit/utils'; import { join } from 'path'; +import Store from 'electron-store'; + +const store = new Store(); + +/** + * 初始化代理设置 + */ +function initializeProxy() { + const defaultConfig = { + enable: false, + protocol: 'http', + host: '127.0.0.1', + port: 7890 + }; + + const proxyConfig = store.get('set.proxyConfig', defaultConfig) as { + enable: boolean; + protocol: string; + host: string; + port: number; + }; + + if (proxyConfig?.enable) { + const proxyRules = `${proxyConfig.protocol}://${proxyConfig.host}:${proxyConfig.port}`; + session.defaultSession.setProxy({ proxyRules }); + } else { + session.defaultSession.setProxy({ proxyRules: '' }); + } +} /** * 初始化窗口管理相关的IPC监听 */ export function initializeWindowManager() { + // 初始化代理设置 + initializeProxy(); + ipcMain.on('minimize-window', (event) => { const win = BrowserWindow.fromWebContents(event.sender); if (win) { @@ -38,6 +70,11 @@ export function initializeWindowManager() { win.hide(); } }); + + // 监听代理设置变化 + store.onDidChange('set.proxyConfig', () => { + initializeProxy(); + }); } /** diff --git a/src/main/set.json b/src/main/set.json index 0e0c6a6..b340616 100644 --- a/src/main/set.json +++ b/src/main/set.json @@ -1,5 +1,13 @@ { "isProxy": false, + "proxyConfig": { + "enable": false, + "protocol": "http", + "host": "127.0.0.1", + "port": 7890 + }, + "enableRealIP":false, + "realIP":"", "noAnimate": false, "animationSpeed": 1, "author": "Alger", diff --git a/src/renderer/components.d.ts b/src/renderer/components.d.ts index ed4582c..15cc638 100644 --- a/src/renderer/components.d.ts +++ b/src/renderer/components.d.ts @@ -17,6 +17,8 @@ declare module 'vue' { NDropdown: typeof import('naive-ui')['NDropdown'] NEllipsis: typeof import('naive-ui')['NEllipsis'] NEmpty: typeof import('naive-ui')['NEmpty'] + NForm: typeof import('naive-ui')['NForm'] + NFormItem: typeof import('naive-ui')['NFormItem'] NImage: typeof import('naive-ui')['NImage'] NInput: typeof import('naive-ui')['NInput'] NInputNumber: typeof import('naive-ui')['NInputNumber'] diff --git a/src/renderer/utils/request.ts b/src/renderer/utils/request.ts index fcc269e..87989dd 100644 --- a/src/renderer/utils/request.ts +++ b/src/renderer/utils/request.ts @@ -1,11 +1,14 @@ import axios, { InternalAxiosRequestConfig } from 'axios'; +import { isElectron } from '.'; let setData: any = null; -if (window.electron) { - setData = window.electron.ipcRenderer.sendSync('get-store-value', 'set'); +const getSetData = ()=>{ + if (window.electron) { + setData = window.electron.ipcRenderer.sendSync('get-store-value', 'set'); + } } - +getSetData() // 扩展请求配置接口 interface CustomAxiosRequestConfig extends InternalAxiosRequestConfig { retryCount?: number; @@ -26,15 +29,18 @@ const RETRY_DELAY = 500; // 请求拦截器 request.interceptors.request.use( (config: CustomAxiosRequestConfig) => { - // 初始化重试次数 - config.retryCount = 0; + getSetData(); + // 只在retryCount未定义时初始化为0 + if (config.retryCount === undefined) { + config.retryCount = 0; + } // 在请求发送之前做一些处理 // 在get请求params中添加timestamp if (config.method === 'get') { config.params = { ...config.params, - timestamp: Date.now() + timestamp: Date.now(), }; const token = localStorage.getItem('token'); if (token) { @@ -42,6 +48,16 @@ request.interceptors.request.use( } } + if(isElectron){ + const proxyConfig = setData?.proxyConfig + if (proxyConfig?.enable && ['http', 'https'].includes(proxyConfig?.protocol)) { + config.params.proxy = `${proxyConfig.protocol}://${proxyConfig.host}:${proxyConfig.port}` + } + if(setData.enableRealIP && setData.realIP){ + config.params.realIP = setData.realIP + } + } + return config; }, (error) => { @@ -58,14 +74,15 @@ request.interceptors.response.use( async (error) => { const config = error.config as CustomAxiosRequestConfig; - // 如果没有配置重试次数,则初始化为0 - if (!config || !config.retryCount) { - config.retryCount = 0; + // 如果没有配置,直接返回错误 + if (!config) { + return Promise.reject(error); } // 检查是否还可以重试 - if (config.retryCount < MAX_RETRIES) { + if (config.retryCount !== undefined && config.retryCount < MAX_RETRIES) { config.retryCount++; + console.log(`请求重试第 ${config.retryCount} 次`); // 延迟重试 await new Promise((resolve) => setTimeout(resolve, RETRY_DELAY)); @@ -74,6 +91,7 @@ request.interceptors.response.use( return request(config); } + console.log(`重试${MAX_RETRIES}次后仍然失败`); return Promise.reject(error); } ); diff --git a/src/renderer/views/set/index.vue b/src/renderer/views/set/index.vue index 417fe52..3d65b2e 100644 --- a/src/renderer/views/set/index.vue +++ b/src/renderer/views/set/index.vue @@ -126,7 +126,7 @@ { label: '最小化到托盘', value: 'minimize' }, { label: '直接退出', value: 'close' } ]" - style="width: 120px" + style="width: 160px" />