From a9a9da4b7ba8578439aefb97373fcb09e7713289 Mon Sep 17 00:00:00 2001 From: shanhexi Date: Sun, 22 Oct 2023 15:19:31 +0800 Subject: [PATCH] electron: stopJavaService --- chat2db-client/.eslintrc.js | 1 + chat2db-client/.umirc.ts | 2 +- .../src/blocks/Setting/About/index.tsx | 18 ++++--- .../src/components/Iconfont/index.tsx | 6 +-- chat2db-client/src/layouts/index.tsx | 14 +++++ chat2db-client/src/main/index.js | 54 ++++--------------- chat2db-client/src/main/preload.js | 12 ++++- chat2db-client/src/service/config.ts | 8 ++- 8 files changed, 58 insertions(+), 57 deletions(-) diff --git a/chat2db-client/.eslintrc.js b/chat2db-client/.eslintrc.js index 4b0e38a6..14dabe6c 100644 --- a/chat2db-client/.eslintrc.js +++ b/chat2db-client/.eslintrc.js @@ -28,6 +28,7 @@ module.exports = { ecmaVersion: 'latest', sourceType: 'module', }, + ignorePatterns: ['src/main'], rules: { 'func-names': 0, // 函数表达式必须有名字 'one-var': [1, 'never'], // 连续声明 diff --git a/chat2db-client/.umirc.ts b/chat2db-client/.umirc.ts index ebfa9a91..0ed67cc9 100644 --- a/chat2db-client/.umirc.ts +++ b/chat2db-client/.umirc.ts @@ -62,7 +62,7 @@ export default defineConfig({ localStorage.clear(); localStorage.setItem('app-local-storage-versions', 'v3'); }`, - `if (window.myAPI) { window.myAPI.startServerForSpawn() }`, + `if (window.electronApi) { window.electronApi.startServerForSpawn() }`, // `if ("serviceWorker" in navigator) { // window.addEventListener("load", function () { // navigator.serviceWorker diff --git a/chat2db-client/src/blocks/Setting/About/index.tsx b/chat2db-client/src/blocks/Setting/About/index.tsx index 18ae5ce0..780356bb 100644 --- a/chat2db-client/src/blocks/Setting/About/index.tsx +++ b/chat2db-client/src/blocks/Setting/About/index.tsx @@ -8,6 +8,7 @@ import configService from '@/service/config'; import { DownloadOutlined } from '@ant-design/icons'; import { IUpdateDetectionData } from '../index'; import { IUpdateDetectionRef, UpdatedStatusEnum } from '../UpdateDetection'; +import Iconfont from '@/components/Iconfont'; interface IProps { updateDetectionData: IUpdateDetectionData | null; @@ -33,6 +34,11 @@ export default function AboutUs(props: IProps) { window.open(WEBSITE_DOC, '_blank'); }; + const restartApp = () => { + console.log(window.electronApi) + window.electronApi?.quitApp(); + } + const updateButton = useMemo(() => { if (!updateDetectionData?.needUpdate) { return false; @@ -68,12 +74,12 @@ export default function AboutUs(props: IProps) { {i18n('setting.button.redownload')} ); - // case UpdatedStatusEnum.UPDATED: - // return ( - // - // ); + case UpdatedStatusEnum.UPDATED: + return ( + + ); default: return false; } diff --git a/chat2db-client/src/components/Iconfont/index.tsx b/chat2db-client/src/components/Iconfont/index.tsx index 57921af9..cd241c12 100644 --- a/chat2db-client/src/components/Iconfont/index.tsx +++ b/chat2db-client/src/components/Iconfont/index.tsx @@ -9,9 +9,9 @@ if (__ENV__ === 'local') { /* 在线链接服务仅供平台体验和调试使用,平台不承诺服务的稳定性,企业客户需下载字体包自行发布使用并做好备份。 */ @font-face { font-family: 'iconfont'; /* Project id 3633546 */ - src: url('//at.alicdn.com/t/c/font_3633546_c0oi4jl53yr.woff2?t=1697950460220') format('woff2'), - url('//at.alicdn.com/t/c/font_3633546_c0oi4jl53yr.woff?t=1697950460220') format('woff'), - url('//at.alicdn.com/t/c/font_3633546_c0oi4jl53yr.ttf?t=1697950460220') format('truetype'); + src: url('//at.alicdn.com/t/c/font_3633546_ios3b6gmgpb.woff2?t=1697956195651') format('woff2'), + url('//at.alicdn.com/t/c/font_3633546_ios3b6gmgpb.woff?t=1697956195651') format('woff'), + url('//at.alicdn.com/t/c/font_3633546_ios3b6gmgpb.ttf?t=1697956195651') format('truetype'); } `; const style = document.createElement('style'); diff --git a/chat2db-client/src/layouts/index.tsx b/chat2db-client/src/layouts/index.tsx index 258e2f60..221342d0 100644 --- a/chat2db-client/src/layouts/index.tsx +++ b/chat2db-client/src/layouts/index.tsx @@ -6,6 +6,7 @@ import { v4 as uuidv4 } from 'uuid'; import { getAntdThemeConfig, injectThemeVar } from '@/theme'; import { IVersionResponse } from '@/typings'; import miscService from '@/service/misc'; +import configService from '@/service/config'; import antdEnUS from 'antd/locale/en_US'; import antdZhCN from 'antd/locale/zh_CN'; import { useTheme } from '@/hooks'; @@ -30,6 +31,11 @@ declare global { _appGatewayParams: IVersionResponse; _notificationApi: any; _indexedDB: any; + electronApi?: { + startServerForSpawn: () => void; + quitApp: () => void; + beforeQuitApp: (fn: () => void) => void; + }; } const __APP_VERSION__: string; const __BUILD_TIME__: string; @@ -92,12 +98,20 @@ function AppContainer() { // 初始化app function collectInitApp() { + registerElectronApi(); monitorOsTheme(); initLang(); initIndexedDB(); setInitEnd(true); } + // 注册Electron关闭时,关闭服务 + function registerElectronApi() { + window.electronApi?.beforeQuitApp(()=>{ + configService.stopJavaService(); + }) + } + // 初始化indexedDB function initIndexedDB() { indexedDB.createDB('chat2db', 1).then((db) => { diff --git a/chat2db-client/src/main/index.js b/chat2db-client/src/main/index.js index ad5ba65e..cded6a0b 100644 --- a/chat2db-client/src/main/index.js +++ b/chat2db-client/src/main/index.js @@ -69,51 +69,7 @@ app.on('window-all-closed', () => { }); app.on('before-quit', (event) => { - // const isWindows = os.platform() === 'win32'; - // let ports = [10821, 10822, 10824]; // 日常端口、测试包端口、线上包端口 - // for (let port of ports) { - // let command = ''; - // if (isWindows) { - // command = `netstat -ano | findstr:${port}`; - // } else { - // command = `lsof -i :${port} | awk '{print $2}'`; - // } - - // exec(command, (err, stdout) => { - // if (err) { - // console.error(`exec error: ${err}`); - // return; - // } - - // let pidArr = []; - // if (isWindows) { - // const lines = stdout.trim().split('\n'); - // pidArr = lines.map((line) => line.trim().split(/\s+/)[4]).filter((pid) => !isNaN(pid)); - // } else { - // pidArr = stdout.trim().split('\n'); - // } - - // if (pidArr.length) { - // try { - // (pidArr || []).forEach((pid) => { - // !!pid && !isNaN(pid) && process.kill(pid); - // }); - // } catch (error) { - // console.error(`Error killing process: ${error}`); - // } - // } - // }); - // } - try { - const request = net.request({ - headers: { - 'Content-Type': 'application/json', - }, - method: 'POST', - url: 'http://127.0.0.1:10824/api/system/stop', - }); - request.end(); - } catch (error) {} + mainWindow.webContents.send('before-quit-app'); }); ipcMain.handle('get-product-name', (event) => { @@ -121,3 +77,11 @@ ipcMain.handle('get-product-name', (event) => { const { name } = path.parse(exePath); return name; }); + +// 注册退出应用事件 +ipcMain.on('quit-app', (event) => { + app.quit(); +}); + + + diff --git a/chat2db-client/src/main/preload.js b/chat2db-client/src/main/preload.js index 8e1e1b9b..64c34454 100644 --- a/chat2db-client/src/main/preload.js +++ b/chat2db-client/src/main/preload.js @@ -4,7 +4,7 @@ const { JAVA_APP_NAME, JAVA_PATH } = require('./constants'); const path = require('path'); const { readVersion } = require('./utils'); -contextBridge.exposeInMainWorld('myAPI', { +contextBridge.exposeInMainWorld('electronApi', { startServerForSpawn: async () => { const javaPath = path.join(__dirname, '../..', `./versions/${readVersion()}`, `./static/${JAVA_APP_NAME}`); const libPath = path.join(__dirname, '../..', `./versions/${readVersion()}`, './static/lib'); @@ -40,4 +40,14 @@ contextBridge.exposeInMainWorld('myAPI', { console.log(`child process exited with code ${code}`); }); }, + quitApp: () => { + ipcRenderer.send('quit-app'); + }, + + beforeQuitAPP: (callback)=>{ + ipcRenderer.on('before-quit-app', () => { + callback(); + }); + } }); + diff --git a/chat2db-client/src/service/config.ts b/chat2db-client/src/service/config.ts index d78c99d2..2ddc6b68 100644 --- a/chat2db-client/src/service/config.ts +++ b/chat2db-client/src/service/config.ts @@ -78,6 +78,11 @@ const setAppUpdateType = createRequest('/api/sy method: 'post', }); +// 退出electron时关闭后端服务 +const stopJavaService = createRequest('/api/system/stop', { + method: 'post', +}); + export default { getSystemConfig, setSystemConfig, @@ -87,5 +92,6 @@ export default { getLatestVersion, isUpdateSuccess, updateDesktopVersion, - setAppUpdateType + setAppUpdateType, + stopJavaService };