mirror of
https://github.com/CodePhiliaX/Chat2DB.git
synced 2026-03-13 09:02:05 +08:00
electron: stopJavaService
This commit is contained in:
@@ -28,6 +28,7 @@ module.exports = {
|
||||
ecmaVersion: 'latest',
|
||||
sourceType: 'module',
|
||||
},
|
||||
ignorePatterns: ['src/main'],
|
||||
rules: {
|
||||
'func-names': 0, // 函数表达式必须有名字
|
||||
'one-var': [1, 'never'], // 连续声明
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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')}
|
||||
</Button>
|
||||
);
|
||||
// case UpdatedStatusEnum.UPDATED:
|
||||
// return (
|
||||
// <Button icon={<RedoOutlined />} type="primary">
|
||||
// {i18n('setting.button.restart')}
|
||||
// </Button>
|
||||
// );
|
||||
case UpdatedStatusEnum.UPDATED:
|
||||
return (
|
||||
<Button icon={<Iconfont code="" />} type="primary" onClick={restartApp}>
|
||||
{i18n('setting.button.restart')}
|
||||
</Button>
|
||||
);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -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');
|
||||
|
||||
@@ -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) => {
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -78,6 +78,11 @@ const setAppUpdateType = createRequest<ILatestVersion['type'], boolean>('/api/sy
|
||||
method: 'post',
|
||||
});
|
||||
|
||||
// 退出electron时关闭后端服务
|
||||
const stopJavaService = createRequest<void, void>('/api/system/stop', {
|
||||
method: 'post',
|
||||
});
|
||||
|
||||
export default {
|
||||
getSystemConfig,
|
||||
setSystemConfig,
|
||||
@@ -87,5 +92,6 @@ export default {
|
||||
getLatestVersion,
|
||||
isUpdateSuccess,
|
||||
updateDesktopVersion,
|
||||
setAppUpdateType
|
||||
setAppUpdateType,
|
||||
stopJavaService
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user