Skip to content

Commit

Permalink
fix : 修复安装打开后报错问题
Browse files Browse the repository at this point in the history
  • Loading branch information
ByLiangCheng committed Oct 19, 2023
1 parent c5eb06f commit 271b459
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 40 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "time-translate",
"version": "0.9.1",
"version": "0.9.2",
"description": "一款简洁、高效、高颜值的输入、截图、划词翻译软件",
"main": "./out/main/index.js",
"author": "byliangcheng",
Expand Down
11 changes: 8 additions & 3 deletions src/main/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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() 会引起窗口闪烁问题
Expand All @@ -29,6 +28,9 @@ if (!SystemTypeEnum.isMac()) {
app.disableHardwareAcceleration()
}

StoreService.init()
StoreService.initConfig()

const mainWinInfo = {
width: StoreService.configGet('mainWinWidth'),
height: 339
Expand All @@ -53,6 +55,9 @@ if (gotTheLock) {
app.quit()
}

// 初始化服务
initServer()

function createWindow(): void {
mainWin = new BrowserWindow({
width: mainWinInfo.width,
Expand Down
2 changes: 0 additions & 2 deletions src/main/service/StoreService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,4 @@ ipcMain.handle('cache-delete', (_event, storeTypeEnum, key) => {
StoreService[storeTypeEnum + 'DeleteByKey'](key)
})

StoreService.init()
StoreService.initConfig()
export default StoreService
70 changes: 36 additions & 34 deletions src/main/service/WebServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
'<div style=" display: flex;align-items: center;justify-content: center;width: 100%;height: 100%;">' +
res.writeHead(200, { 'Content-Type': 'text/html;charset=UTF8' })
res.end(
'<div style=" display: flex;align-items: center;justify-content: center;width: 100%;height: 100%;">' +
'<div style="display: flex;align-items: center;flex-direction: column;">' +
'<h1>成功</h1>' +
'<h2>您可以关闭此页面</h2>' +
'</div>' +
'</div>'
)
})
)
})
}

0 comments on commit 271b459

Please sign in to comment.