Skip to content

Commit a9a9da4

Browse files
committed
electron: stopJavaService
1 parent a57708a commit a9a9da4

8 files changed

Lines changed: 58 additions & 57 deletions

File tree

chat2db-client/.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ module.exports = {
2828
ecmaVersion: 'latest',
2929
sourceType: 'module',
3030
},
31+
ignorePatterns: ['src/main'],
3132
rules: {
3233
'func-names': 0, // 函数表达式必须有名字
3334
'one-var': [1, 'never'], // 连续声明

chat2db-client/.umirc.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export default defineConfig({
6262
localStorage.clear();
6363
localStorage.setItem('app-local-storage-versions', 'v3');
6464
}`,
65-
`if (window.myAPI) { window.myAPI.startServerForSpawn() }`,
65+
`if (window.electronApi) { window.electronApi.startServerForSpawn() }`,
6666
// `if ("serviceWorker" in navigator) {
6767
// window.addEventListener("load", function () {
6868
// navigator.serviceWorker

chat2db-client/src/blocks/Setting/About/index.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import configService from '@/service/config';
88
import { DownloadOutlined } from '@ant-design/icons';
99
import { IUpdateDetectionData } from '../index';
1010
import { IUpdateDetectionRef, UpdatedStatusEnum } from '../UpdateDetection';
11+
import Iconfont from '@/components/Iconfont';
1112

1213
interface IProps {
1314
updateDetectionData: IUpdateDetectionData | null;
@@ -33,6 +34,11 @@ export default function AboutUs(props: IProps) {
3334
window.open(WEBSITE_DOC, '_blank');
3435
};
3536

37+
const restartApp = () => {
38+
console.log(window.electronApi)
39+
window.electronApi?.quitApp();
40+
}
41+
3642
const updateButton = useMemo(() => {
3743
if (!updateDetectionData?.needUpdate) {
3844
return false;
@@ -68,12 +74,12 @@ export default function AboutUs(props: IProps) {
6874
{i18n('setting.button.redownload')}
6975
</Button>
7076
);
71-
// case UpdatedStatusEnum.UPDATED:
72-
// return (
73-
// <Button icon={<RedoOutlined />} type="primary">
74-
// {i18n('setting.button.restart')}
75-
// </Button>
76-
// );
77+
case UpdatedStatusEnum.UPDATED:
78+
return (
79+
<Button icon={<Iconfont code="&#xe662;" />} type="primary" onClick={restartApp}>
80+
{i18n('setting.button.restart')}
81+
</Button>
82+
);
7783
default:
7884
return false;
7985
}

chat2db-client/src/components/Iconfont/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ if (__ENV__ === 'local') {
99
/* 在线链接服务仅供平台体验和调试使用,平台不承诺服务的稳定性,企业客户需下载字体包自行发布使用并做好备份。 */
1010
@font-face {
1111
font-family: 'iconfont'; /* Project id 3633546 */
12-
src: url('//at.alicdn.com/t/c/font_3633546_c0oi4jl53yr.woff2?t=1697950460220') format('woff2'),
13-
url('//at.alicdn.com/t/c/font_3633546_c0oi4jl53yr.woff?t=1697950460220') format('woff'),
14-
url('//at.alicdn.com/t/c/font_3633546_c0oi4jl53yr.ttf?t=1697950460220') format('truetype');
12+
src: url('//at.alicdn.com/t/c/font_3633546_ios3b6gmgpb.woff2?t=1697956195651') format('woff2'),
13+
url('//at.alicdn.com/t/c/font_3633546_ios3b6gmgpb.woff?t=1697956195651') format('woff'),
14+
url('//at.alicdn.com/t/c/font_3633546_ios3b6gmgpb.ttf?t=1697956195651') format('truetype');
1515
}
1616
`;
1717
const style = document.createElement('style');

chat2db-client/src/layouts/index.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { v4 as uuidv4 } from 'uuid';
66
import { getAntdThemeConfig, injectThemeVar } from '@/theme';
77
import { IVersionResponse } from '@/typings';
88
import miscService from '@/service/misc';
9+
import configService from '@/service/config';
910
import antdEnUS from 'antd/locale/en_US';
1011
import antdZhCN from 'antd/locale/zh_CN';
1112
import { useTheme } from '@/hooks';
@@ -30,6 +31,11 @@ declare global {
3031
_appGatewayParams: IVersionResponse;
3132
_notificationApi: any;
3233
_indexedDB: any;
34+
electronApi?: {
35+
startServerForSpawn: () => void;
36+
quitApp: () => void;
37+
beforeQuitApp: (fn: () => void) => void;
38+
};
3339
}
3440
const __APP_VERSION__: string;
3541
const __BUILD_TIME__: string;
@@ -92,12 +98,20 @@ function AppContainer() {
9298

9399
// 初始化app
94100
function collectInitApp() {
101+
registerElectronApi();
95102
monitorOsTheme();
96103
initLang();
97104
initIndexedDB();
98105
setInitEnd(true);
99106
}
100107

108+
// 注册Electron关闭时,关闭服务
109+
function registerElectronApi() {
110+
window.electronApi?.beforeQuitApp(()=>{
111+
configService.stopJavaService();
112+
})
113+
}
114+
101115
// 初始化indexedDB
102116
function initIndexedDB() {
103117
indexedDB.createDB('chat2db', 1).then((db) => {

chat2db-client/src/main/index.js

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -69,55 +69,19 @@ app.on('window-all-closed', () => {
6969
});
7070

7171
app.on('before-quit', (event) => {
72-
// const isWindows = os.platform() === 'win32';
73-
// let ports = [10821, 10822, 10824]; // 日常端口、测试包端口、线上包端口
74-
// for (let port of ports) {
75-
// let command = '';
76-
// if (isWindows) {
77-
// command = `netstat -ano | findstr:${port}`;
78-
// } else {
79-
// command = `lsof -i :${port} | awk '{print $2}'`;
80-
// }
81-
82-
// exec(command, (err, stdout) => {
83-
// if (err) {
84-
// console.error(`exec error: ${err}`);
85-
// return;
86-
// }
87-
88-
// let pidArr = [];
89-
// if (isWindows) {
90-
// const lines = stdout.trim().split('\n');
91-
// pidArr = lines.map((line) => line.trim().split(/\s+/)[4]).filter((pid) => !isNaN(pid));
92-
// } else {
93-
// pidArr = stdout.trim().split('\n');
94-
// }
95-
96-
// if (pidArr.length) {
97-
// try {
98-
// (pidArr || []).forEach((pid) => {
99-
// !!pid && !isNaN(pid) && process.kill(pid);
100-
// });
101-
// } catch (error) {
102-
// console.error(`Error killing process: ${error}`);
103-
// }
104-
// }
105-
// });
106-
// }
107-
try {
108-
const request = net.request({
109-
headers: {
110-
'Content-Type': 'application/json',
111-
},
112-
method: 'POST',
113-
url: 'http://127.0.0.1:10824/api/system/stop',
114-
});
115-
request.end();
116-
} catch (error) {}
72+
mainWindow.webContents.send('before-quit-app');
11773
});
11874

11975
ipcMain.handle('get-product-name', (event) => {
12076
const exePath = app.getPath('exe');
12177
const { name } = path.parse(exePath);
12278
return name;
12379
});
80+
81+
// 注册退出应用事件
82+
ipcMain.on('quit-app', (event) => {
83+
app.quit();
84+
});
85+
86+
87+

chat2db-client/src/main/preload.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { JAVA_APP_NAME, JAVA_PATH } = require('./constants');
44
const path = require('path');
55
const { readVersion } = require('./utils');
66

7-
contextBridge.exposeInMainWorld('myAPI', {
7+
contextBridge.exposeInMainWorld('electronApi', {
88
startServerForSpawn: async () => {
99
const javaPath = path.join(__dirname, '../..', `./versions/${readVersion()}`, `./static/${JAVA_APP_NAME}`);
1010
const libPath = path.join(__dirname, '../..', `./versions/${readVersion()}`, './static/lib');
@@ -40,4 +40,14 @@ contextBridge.exposeInMainWorld('myAPI', {
4040
console.log(`child process exited with code ${code}`);
4141
});
4242
},
43+
quitApp: () => {
44+
ipcRenderer.send('quit-app');
45+
},
46+
47+
beforeQuitAPP: (callback)=>{
48+
ipcRenderer.on('before-quit-app', () => {
49+
callback();
50+
});
51+
}
4352
});
53+

chat2db-client/src/service/config.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ const setAppUpdateType = createRequest<ILatestVersion['type'], boolean>('/api/sy
7878
method: 'post',
7979
});
8080

81+
// 退出electron时关闭后端服务
82+
const stopJavaService = createRequest<void, void>('/api/system/stop', {
83+
method: 'post',
84+
});
85+
8186
export default {
8287
getSystemConfig,
8388
setSystemConfig,
@@ -87,5 +92,6 @@ export default {
8792
getLatestVersion,
8893
isUpdateSuccess,
8994
updateDesktopVersion,
90-
setAppUpdateType
95+
setAppUpdateType,
96+
stopJavaService
9197
};

0 commit comments

Comments
 (0)