From 271b459e1c641802145d7477d697bc0a2ddccee9 Mon Sep 17 00:00:00 2001 From: byliangcheng Date: Thu, 19 Oct 2023 11:02:52 +0800 Subject: [PATCH] =?UTF-8?q?fix=20:=20=E4=BF=AE=E5=A4=8D=E5=AE=89=E8=A3=85?= =?UTF-8?q?=E6=89=93=E5=BC=80=E5=90=8E=E6=8A=A5=E9=94=99=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/main/index.ts | 11 +++-- src/main/service/StoreService.ts | 2 - src/main/service/WebServer.ts | 70 ++++++++++++++++---------------- 4 files changed, 45 insertions(+), 40 deletions(-) diff --git a/package.json b/package.json index d0a987b..c32627f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "time-translate", - "version": "0.9.1", + "version": "0.9.2", "description": "一款简洁、高效、高颜值的输入、截图、划词翻译软件", "main": "./out/main/index.js", "author": "byliangcheng", diff --git a/src/main/index.ts b/src/main/index.ts index f4e11f7..0ae0bca 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -1,7 +1,6 @@ import { app, BrowserWindow } from 'electron' import * as path from 'path' import { electronApp, is, optimizer } from '@electron-toolkit/utils' -import StoreService from './service/StoreService' import { GlobalShortcutEvent } from './service/GlobalShortcutEvent' import { WinEvent } from './service/Win' import { TrayEvent } from './service/TrayEvent' @@ -15,10 +14,10 @@ import './service/HoverBall' import './service/Ocr' import './service/OcrSilence' import './service/ClipboardListenerService' -import './service/WebServer' +import { initServer } from './service/WebServer' import './service/IpcMainHandle' -import './service/channel/interfaces/EcDictRequest' import { injectWinAgent } from './utils/RequestUtil' +import StoreService from './service/StoreService' import { YesNoEnum } from '../common/enums/YesNoEnum' // 解决使用 win.hide() 后再使用 win.show() 会引起窗口闪烁问题 @@ -29,6 +28,9 @@ if (!SystemTypeEnum.isMac()) { app.disableHardwareAcceleration() } +StoreService.init() +StoreService.initConfig() + const mainWinInfo = { width: StoreService.configGet('mainWinWidth'), height: 339 @@ -53,6 +55,9 @@ if (gotTheLock) { app.quit() } +// 初始化服务 +initServer() + function createWindow(): void { mainWin = new BrowserWindow({ width: mainWinInfo.width, diff --git a/src/main/service/StoreService.ts b/src/main/service/StoreService.ts index f340c82..76b9231 100644 --- a/src/main/service/StoreService.ts +++ b/src/main/service/StoreService.ts @@ -461,6 +461,4 @@ ipcMain.handle('cache-delete', (_event, storeTypeEnum, key) => { StoreService[storeTypeEnum + 'DeleteByKey'](key) }) -StoreService.init() -StoreService.initConfig() export default StoreService diff --git a/src/main/service/WebServer.ts b/src/main/service/WebServer.ts index 0faa745..1de5a13 100644 --- a/src/main/service/WebServer.ts +++ b/src/main/service/WebServer.ts @@ -5,46 +5,48 @@ import StoreService from './StoreService' import log from '../utils/log' import http from 'http' -const servicePort = StoreService.configGet('servicePort') -// 创建服务并绑定端口号 -const server = http.createServer().listen(servicePort, '0.0.0.0') -log.info('[服务] - 初始化 , 端口号 : ', servicePort) +export const initServer = (): void => { + const servicePort = StoreService.configGet('servicePort') + // 创建服务并绑定端口号 + const server = http.createServer().listen(servicePort, '0.0.0.0') + log.info('[服务] - 初始化 , 端口号 : ', servicePort) -server.on('error', (e: any) => { - log.error('[服务] - 异常 : ', e) - if (e?.code === 'EADDRINUSE' || e?.code === 'EACCES') { - log.error('[服务] - 重置服务端口号 - 开始 ') - setTimeout(() => { - server.close() - // 随机分配端口号 - server.listen(0, '0.0.0.0', () => { - const port = (server.address() as any).port - log.info('[服务] - 重置服务端口号 : ', port) - StoreService.configSet('servicePort', port) - }) - }, 1000) - } -}) + server.on('error', (e: any) => { + log.error('[服务] - 异常 : ', e) + if (e?.code === 'EADDRINUSE' || e?.code === 'EACCES') { + log.error('[服务] - 重置服务端口号 - 开始 ') + setTimeout(() => { + server.close() + // 随机分配端口号 + server.listen(0, '0.0.0.0', () => { + const port = (server.address() as any).port + log.info('[服务] - 重置服务端口号 : ', port) + StoreService.configSet('servicePort', port) + }) + }, 1000) + } + }) // 绑定请求事件监听 -server.on('request', (req, res) => { - const parseCustomProtocol = parseCustomProtocolUrl(req.url) - if (isNull(parseCustomProtocol)) { - return - } - if (parseCustomProtocol.path === 'login') { - const token = parseCustomProtocol.queryParams.token - if (token) { - TTimeAuth.login(token) + server.on('request', (req, res) => { + const parseCustomProtocol = parseCustomProtocolUrl(req.url) + if (isNull(parseCustomProtocol)) { + return + } + if (parseCustomProtocol.path === 'login') { + const token = parseCustomProtocol.queryParams.token + if (token) { + TTimeAuth.login(token) + } } - } - res.writeHead(200, { 'Content-Type': 'text/html;charset=UTF8' }) - res.end( - '
' + + res.writeHead(200, { 'Content-Type': 'text/html;charset=UTF8' }) + res.end( + '
' + '
' + '

成功

' + '

您可以关闭此页面

' + '
' + '
' - ) -}) + ) + }) +}