2026-09-11 22:46:31 +08:00
|
|
|
|
import path from 'node:path';
|
|
|
|
|
|
import { BrowserWindow, app, shell } from 'electron';
|
|
|
|
|
|
import { registerIpcHandlers } from './ipc.js';
|
2026-09-12 08:41:08 +08:00
|
|
|
|
import { island, registerIslandIpc } from './island.js';
|
|
|
|
|
|
import { logger } from './logger.js';
|
2026-09-11 22:46:31 +08:00
|
|
|
|
import { runSmokeTest } from './smoke.js';
|
2026-09-12 15:19:13 +08:00
|
|
|
|
import { createTray, destroyTray, isTrayReady } from './tray.js';
|
2026-09-11 22:46:31 +08:00
|
|
|
|
|
|
|
|
|
|
// 主进程由 esbuild 打包为 CommonJS,因此这里可以直接使用 __dirname
|
|
|
|
|
|
// (dist/main/index.js -> dist/preload/index.js 与 dist/renderer/index.html)
|
|
|
|
|
|
const currentDir = __dirname;
|
|
|
|
|
|
|
|
|
|
|
|
const devServerUrl = process.env.VITE_DEV_SERVER_URL;
|
|
|
|
|
|
const isSmokeTest = process.env.ELECTRON_SMOKE_TEST === '1';
|
|
|
|
|
|
|
2026-09-12 09:39:33 +08:00
|
|
|
|
// 自动化验证使用独立的 userData:否则用户正在运行的客户端会占着单实例锁,
|
|
|
|
|
|
// 冒烟进程会"静默退出(退出码 0、无输出)",看起来像构建坏了。
|
|
|
|
|
|
const smokeProfile = process.env.ELECTRON_SMOKE_PROFILE;
|
|
|
|
|
|
if (smokeProfile) app.setPath('userData', smokeProfile);
|
|
|
|
|
|
|
2026-09-11 22:46:31 +08:00
|
|
|
|
let mainWindow: BrowserWindow | null = null;
|
|
|
|
|
|
|
2026-09-12 15:19:13 +08:00
|
|
|
|
/** 是否正在退出:用于区分「关闭窗口 = 隐藏到托盘」与「真正退出」 */
|
|
|
|
|
|
let isQuitting = false;
|
|
|
|
|
|
|
2026-09-11 22:46:31 +08:00
|
|
|
|
function createWindow(): BrowserWindow {
|
|
|
|
|
|
const win = new BrowserWindow({
|
|
|
|
|
|
width: 1200,
|
|
|
|
|
|
height: 800,
|
|
|
|
|
|
minWidth: 980,
|
|
|
|
|
|
minHeight: 640,
|
|
|
|
|
|
title: '班级小助手',
|
|
|
|
|
|
// 冒烟验证时不弹窗,避免打扰使用者
|
|
|
|
|
|
show: !isSmokeTest,
|
|
|
|
|
|
autoHideMenuBar: true,
|
|
|
|
|
|
backgroundColor: '#f5f7fa',
|
|
|
|
|
|
webPreferences: {
|
|
|
|
|
|
preload: path.join(currentDir, '../preload/index.js'),
|
|
|
|
|
|
contextIsolation: true,
|
|
|
|
|
|
nodeIntegration: false,
|
|
|
|
|
|
sandbox: true,
|
|
|
|
|
|
webSecurity: true,
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 外部链接交给系统浏览器,禁止在应用内新开窗口
|
|
|
|
|
|
win.webContents.setWindowOpenHandler(({ url }) => {
|
|
|
|
|
|
if (/^https?:\/\//i.test(url)) void shell.openExternal(url);
|
|
|
|
|
|
return { action: 'deny' };
|
|
|
|
|
|
});
|
|
|
|
|
|
win.webContents.on('will-navigate', (event, url) => {
|
|
|
|
|
|
const allowed = devServerUrl ? url.startsWith(devServerUrl) : url.startsWith('file://');
|
|
|
|
|
|
if (!allowed) {
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
if (/^https?:\/\//i.test(url)) void shell.openExternal(url);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
win.on('closed', () => {
|
|
|
|
|
|
mainWindow = null;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return win;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function loadRenderer(win: BrowserWindow): Promise<void> {
|
|
|
|
|
|
if (devServerUrl) {
|
|
|
|
|
|
await win.loadURL(devServerUrl);
|
|
|
|
|
|
win.webContents.openDevTools({ mode: 'detach' });
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
// 生产:加载打包后的本地 HTML(vite base 为 './',使用 file:// 相对路径)
|
|
|
|
|
|
await win.loadFile(path.join(currentDir, '../renderer/index.html'));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 单实例:重复启动时聚焦已有窗口
|
|
|
|
|
|
const gotLock = app.requestSingleInstanceLock();
|
|
|
|
|
|
if (!gotLock) {
|
|
|
|
|
|
app.quit();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
app.on('second-instance', () => {
|
|
|
|
|
|
if (!mainWindow) return;
|
|
|
|
|
|
if (mainWindow.isMinimized()) mainWindow.restore();
|
|
|
|
|
|
mainWindow.focus();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
app.whenReady().then(async () => {
|
|
|
|
|
|
registerIpcHandlers();
|
2026-09-12 08:41:08 +08:00
|
|
|
|
registerIslandIpc();
|
2026-09-11 22:46:31 +08:00
|
|
|
|
|
|
|
|
|
|
mainWindow = createWindow();
|
|
|
|
|
|
await loadRenderer(mainWindow);
|
|
|
|
|
|
|
2026-09-12 08:41:08 +08:00
|
|
|
|
// 灵动岛:独立的置顶透明小窗口(桌面中上方)
|
|
|
|
|
|
try {
|
|
|
|
|
|
await island.init(devServerUrl ? `${devServerUrl.replace(/\/+$/, '')}/island.html` : null);
|
|
|
|
|
|
// 点击灵动岛里的"打开应用"时把主窗口拉到前台
|
|
|
|
|
|
island.setOpenAppHandler(() => {
|
|
|
|
|
|
if (!mainWindow || mainWindow.isDestroyed()) return;
|
|
|
|
|
|
if (mainWindow.isMinimized()) mainWindow.restore();
|
|
|
|
|
|
mainWindow.show();
|
|
|
|
|
|
mainWindow.focus();
|
|
|
|
|
|
});
|
2026-09-12 13:57:46 +08:00
|
|
|
|
// 灵动岛点"标为已读":转交主窗口渲染进程调用接口并刷新通知中心
|
|
|
|
|
|
island.setMarkReadHandler((id) => {
|
|
|
|
|
|
if (!mainWindow || mainWindow.isDestroyed()) return;
|
|
|
|
|
|
mainWindow.webContents.send('island:mark-read', id);
|
|
|
|
|
|
});
|
2026-09-12 08:41:08 +08:00
|
|
|
|
} catch (error) {
|
|
|
|
|
|
logger.error('灵动岛初始化失败(不影响主功能)', error);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-12 15:19:13 +08:00
|
|
|
|
// 关闭主窗口 → 隐藏到系统托盘(需求 5),只有托盘"退出"才真正结束进程
|
|
|
|
|
|
// 注意:必须在冒烟验证之前挂载,否则 close() 会真的销毁窗口
|
|
|
|
|
|
mainWindow?.on('close', (event) => {
|
|
|
|
|
|
if (isQuitting) return;
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
mainWindow?.hide();
|
|
|
|
|
|
logger.info('主窗口已隐藏到系统托盘(后台继续运行,左键托盘图标可恢复)');
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
createTray({
|
|
|
|
|
|
onShow: () => showMainWindow(),
|
|
|
|
|
|
onHide: () => mainWindow?.hide(),
|
|
|
|
|
|
onQuit: () => quitApp(),
|
|
|
|
|
|
});
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
logger.warn(`系统托盘初始化失败(不影响主功能):${error instanceof Error ? error.message : error}`);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-11 22:46:31 +08:00
|
|
|
|
if (isSmokeTest) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
await runSmokeTest(mainWindow);
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('[SMOKE] 冒烟验证异常', error);
|
|
|
|
|
|
app.exit(1);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-09-12 15:19:13 +08:00
|
|
|
|
/** 显示主窗口(托盘左键 / 托盘菜单 / 二次启动) */
|
|
|
|
|
|
function showMainWindow(): void {
|
|
|
|
|
|
if (!mainWindow || mainWindow.isDestroyed()) {
|
|
|
|
|
|
mainWindow = createWindow();
|
|
|
|
|
|
void loadRenderer(mainWindow);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (mainWindow.isMinimized()) mainWindow.restore();
|
|
|
|
|
|
mainWindow.show();
|
|
|
|
|
|
mainWindow.focus();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 退出前的统一资源清理(需求 5:托盘 / 定时器 / 网络连接 / 窗口都不留残留)。
|
|
|
|
|
|
* 幂等:托盘退出、before-quit、will-quit 都可能触发。
|
|
|
|
|
|
*/
|
|
|
|
|
|
function shutdownResources(): string {
|
|
|
|
|
|
const done: string[] = [];
|
|
|
|
|
|
if (isTrayReady()) {
|
|
|
|
|
|
destroyTray();
|
|
|
|
|
|
done.push('托盘已销毁');
|
|
|
|
|
|
}
|
|
|
|
|
|
if (island.isReady()) {
|
|
|
|
|
|
island.destroy();
|
|
|
|
|
|
done.push('灵动岛已销毁');
|
|
|
|
|
|
}
|
|
|
|
|
|
for (const win of BrowserWindow.getAllWindows()) {
|
|
|
|
|
|
if (!win.isDestroyed()) {
|
|
|
|
|
|
// 渲染进程随之结束:其中的 Socket.IO 连接、定时器、IndexedDB 句柄一并释放
|
|
|
|
|
|
win.destroy();
|
|
|
|
|
|
done.push('窗口已销毁');
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return done.length > 0 ? done.join('、') : '无需要清理的资源';
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** 真正退出:先清理资源,再 app.quit,并用 app.exit 兜底(确保不留进程) */
|
|
|
|
|
|
function quitApp(): void {
|
|
|
|
|
|
if (isQuitting) return;
|
|
|
|
|
|
isQuitting = true;
|
|
|
|
|
|
logger.info(`正在退出:${shutdownResources()}`);
|
|
|
|
|
|
app.quit();
|
|
|
|
|
|
setTimeout(() => app.exit(0), 1500);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-11 22:46:31 +08:00
|
|
|
|
app.on('window-all-closed', () => {
|
2026-09-12 15:19:13 +08:00
|
|
|
|
// 有托盘常驻:窗口全关也不退出,继续后台接收通知
|
|
|
|
|
|
logger.info('所有窗口已关闭,程序继续在系统托盘后台运行');
|
2026-09-11 22:46:31 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-09-12 08:41:08 +08:00
|
|
|
|
app.on('before-quit', () => {
|
2026-09-12 15:19:13 +08:00
|
|
|
|
isQuitting = true;
|
|
|
|
|
|
shutdownResources();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
app.on('will-quit', () => {
|
|
|
|
|
|
const summary = shutdownResources();
|
|
|
|
|
|
logger.info(`应用退出清理:${summary}`);
|
2026-09-12 08:41:08 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-09-11 22:46:31 +08:00
|
|
|
|
app.on('activate', async () => {
|
|
|
|
|
|
if (BrowserWindow.getAllWindows().length === 0) {
|
|
|
|
|
|
mainWindow = createWindow();
|
|
|
|
|
|
await loadRenderer(mainWindow);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|