Skip to content

Commit fdcca3c

Browse files
committed
fix: 修复软件关闭前数据无法正常保存的BUG
1 parent b22cb83 commit fdcca3c

File tree

4 files changed

+74
-153
lines changed

4 files changed

+74
-153
lines changed

packages/web/src/pages/index.vue

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ const state = reactive({
150150
});
151151
152152
// 监听软件关闭
153-
onUnmounted(() => closeAllBrowser(false));
153+
onUnmounted(() => closeAllBrowser());
154154
155155
onMounted(async () => {
156156
try {
@@ -270,8 +270,22 @@ onMounted(async () => {
270270
}
271271
});
272272
273-
ipcRenderer.on('close', () => {
274-
saveStoreToLocal(store);
273+
/**
274+
* 全局唯一关闭处理地方
275+
*/
276+
ipcRenderer.on('close', async () => {
277+
console.log('关闭浏览器中...');
278+
const res = await closeAllBrowser();
279+
if (res === false) {
280+
console.log('有浏览器拒绝关闭,取消退出');
281+
return;
282+
}
283+
console.log('保存数据中...');
284+
const m = Modal.info({ content: '正在保存数据...', closable: false, maskClosable: false, footer: false });
285+
await saveStoreToLocal(store);
286+
m.close();
287+
console.log('数据已保存');
288+
remote.app.call('exit', 0);
275289
});
276290
277291
function clickMenu(route: RouteRecordRaw & { meta: { title: string } }) {

packages/web/src/utils/browser.ts

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -180,42 +180,45 @@ export async function forceClearBrowserCache(title: string, userDataDirsFolder:
180180
modal.close();
181181
}
182182

183-
export async function closeAllBrowser(quit: boolean) {
183+
/**
184+
* @returns 是否同意关闭软件
185+
*/
186+
export async function closeAllBrowser() {
184187
if (processes.length) {
185-
Modal.warning({
186-
content: '还有浏览器正在运行,您确定关闭软件吗?',
187-
title: '警告',
188-
maskClosable: true,
189-
closable: true,
190-
alignCenter: true,
191-
hideCancel: false,
192-
onOk: async () => {
193-
const m = Modal.info({ content: '正在关闭所有浏览器...', closable: false, maskClosable: false, footer: false });
194-
195-
const close = () => {
196-
if (quit) {
197-
remote.app.call('exit');
188+
return new Promise<boolean>((resolve, reject) => {
189+
Modal.warning({
190+
content: '还有浏览器正在运行,您确定关闭软件吗?',
191+
title: '警告',
192+
maskClosable: true,
193+
closable: true,
194+
alignCenter: true,
195+
hideCancel: false,
196+
onOk: async () => {
197+
const m = Modal.info({
198+
content: '正在关闭所有浏览器...',
199+
closable: false,
200+
maskClosable: false,
201+
footer: false
202+
});
203+
204+
// 最久5秒后关闭
205+
const timeout = setTimeout(close, 5000);
206+
try {
207+
for (const process of processes) {
208+
await process.close();
209+
await sleep(100);
210+
}
211+
} catch (err) {
212+
Message.error(String(err));
198213
}
214+
clearTimeout(timeout);
199215
m.close();
200-
};
201-
202-
// 最久5秒后关闭
203-
const timeout = setTimeout(close, 5000);
204-
try {
205-
for (const process of processes) {
206-
await process.close();
207-
await sleep(100);
208-
}
209-
} catch (err) {
210-
Message.error(String(err));
216+
resolve(true);
217+
},
218+
onCancel() {
219+
resolve(false);
211220
}
212-
clearTimeout(timeout);
213-
close();
214-
}
221+
});
215222
});
216-
} else {
217-
if (quit) {
218-
remote.app.call('exit');
219-
}
220223
}
221224
}

packages/web/src/utils/ipc.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import { UpdateInformationResource } from '@ocs-desktop/common';
22
import { store } from '../store';
33
import { electron } from '../utils/node';
4-
import { closeAllBrowser } from './browser';
54
import { notify } from './notify';
65
import { Modal } from '@arco-design/web-vue';
76
import { h } from 'vue';
87
import { remote } from './remote';
98
const { ipcRenderer } = electron;
109

1110
export function activeIpcRenderListener() {
12-
ipcRenderer.on('close', () => closeAllBrowser(true));
13-
1411
/** 如果正在更新的话,获取更新进度 */
1512
ipcRenderer.on('update-download', (e, rate, totalLength, chunkLength) => {
1613
notify(

0 commit comments

Comments
 (0)