| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- const { app, BrowserWindow, Tray, Menu, ipcMain, dialog, shell } = require('electron');
- const path = require('path');
- const { wpsHelper } = require('./winaxHelper.js');
- const Logger = require('./logger.js');
- const isDev = process.env.NODE_ENV === 'development';
- const logger = new Logger();
- let mainWindow;
- let loginWindow;
- let settingsWindow;
- let tray;
- let appConfig = {
- showFloatingWindow: true,
- autoConnectWPS: true,
- logLevel: 'info'
- };
- let isLoggedIn = false;
- function createMainWindow() {
- const { width, height } = require('electron').screen.getPrimaryDisplay().workAreaSize;
- mainWindow = new BrowserWindow({
- width: 1000,
- height: 700,
- x: width - 1100,
- y: 50,
- show: false,
- frame: false,
- webPreferences: {
- nodeIntegration: false,
- contextIsolation: true,
- preload: path.join(__dirname, 'preload.js')
- },
- title: 'AI Electron Client'
- });
- if (isDev) {
- mainWindow.loadURL('http://localhost:5173/#/main');
- } else {
- mainWindow.loadFile('dist/index.html', { hash: 'main' });
- }
- mainWindow.on('close', (event) => {
- if (!app.isQuitting) {
- event.preventDefault();
- mainWindow.hide();
- }
- });
- logger.info('Main window created');
- }
- function createLoginWindow() {
- loginWindow = new BrowserWindow({
- width: 350,
- height: 450,
- show: false,
- modal: true,
- frame: false,
- resizable: false,
- webPreferences: {
- nodeIntegration: false,
- contextIsolation: true,
- preload: path.join(__dirname, 'preload.js')
- }
- });
- if (isDev) {
- loginWindow.loadURL('http://localhost:5173/#/login');
- } else {
- loginWindow.loadFile('dist/index.html', { hash: 'login' });
- }
- loginWindow.on('closed', () => {
- loginWindow = null;
- });
- }
- function createTray() {
- const iconPath = path.join(__dirname, '../assets/tray.png');
- tray = new Tray(iconPath || require('electron').nativeImage.createEmpty());
- const contextMenu = Menu.buildFromTemplate([
- {
- label: '打开主界面',
- click: () => {
- if (mainWindow) {
- mainWindow.show();
- mainWindow.focus();
- }
- }
- },
- {
- label: '退出',
- click: () => {
- app.isQuitting = true;
- app.quit();
- }
- }
- ]);
- tray.setContextMenu(contextMenu);
- tray.setToolTip('AI Electron Client');
- }
- // IPC 处理
- ipcMain.handle('login', async (event, credentials) => {
- try {
- logger.info('Login attempt for user:', credentials.username);
- await new Promise(resolve => setTimeout(resolve, 1000));
- if (credentials.username && credentials.password) {
- isLoggedIn = true;
- wpsHelper.connectToWPS();
- if (loginWindow) loginWindow.hide();
- if (mainWindow) {
- mainWindow.show();
- mainWindow.focus();
- }
- return { success: true, user: { name: credentials.username } };
- } else {
- return { success: false, message: '用户名和密码不能为空' };
- }
- } catch (error) {
- logger.error('Login error:', error);
- return { success: false, message: '登录失败' };
- }
- });
- ipcMain.handle('logout', () => {
- isLoggedIn = false;
- if (mainWindow) mainWindow.hide();
- wpsHelper.cleanup();
- showLoginWindow();
- });
- ipcMain.handle('window-minimize', () => {
- if (mainWindow) mainWindow.minimize();
- });
- ipcMain.handle('window-close', () => {
- if (mainWindow) mainWindow.hide();
- });
- ipcMain.handle('show-settings', () => {
- if (!settingsWindow) {
- settingsWindow = new BrowserWindow({
- width: 600,
- height: 500,
- modal: true,
- parent: mainWindow,
- webPreferences: {
- nodeIntegration: false,
- contextIsolation: true,
- preload: path.join(__dirname, 'preload.js')
- }
- });
- if (isDev) {
- settingsWindow.loadURL('http://localhost:5173/#/settings');
- } else {
- settingsWindow.loadFile('dist/index.html', { hash: 'settings' });
- }
- }
- settingsWindow.show();
- });
- ipcMain.handle('open-file', async (event, filePath) => {
- try {
- if (!filePath) {
- const result = await dialog.showOpenDialog(mainWindow, {
- properties: ['openFile'],
- filters: [
- { name: 'Word Documents', extensions: ['doc', 'docx'] }
- ]
- });
- if (result.canceled) return { success: false };
- filePath = result.filePaths[0];
- }
- const success = wpsHelper.openDocument(filePath);
- return { success, message: success ? '文件已打开' : '打开失败' };
- } catch (error) {
- logger.error('Error opening file:', error);
- return { success: false, error: error.message };
- }
- });
- ipcMain.handle('get-wps-status', () => {
- return wpsHelper.getStatus();
- });
- ipcMain.handle('navigate-paragraph', (event, direction) => {
- return wpsHelper.navigateParagraph(direction);
- });
- ipcMain.handle('get-full-paragraph-content', () => {
- return wpsHelper.getFullParagraphContent();
- });
- ipcMain.handle('update-paragraph-with-revisions', (event, content) => {
- return wpsHelper.updateParagraphWithRevisions(content);
- });
- ipcMain.handle('handle-revision', (event, action, revisionIndex) => {
- return wpsHelper.handleRevision(action, revisionIndex);
- });
- ipcMain.handle('add-comment', (event, commentText) => {
- return wpsHelper.addComment(commentText);
- });
- ipcMain.handle('set-track-revisions', (event, track) => {
- return wpsHelper.setTrackRevisions(track);
- });
- ipcMain.handle('switch-document', (event, filePath) => {
- return wpsHelper.switchToDocument(filePath);
- });
- function showLoginWindow() {
- if (!loginWindow) {
- createLoginWindow();
- }
- loginWindow.show();
- loginWindow.focus();
- }
- app.whenReady().then(() => {
- logger.info('App is ready');
- createLoginWindow();
- createMainWindow();
- createTray();
- setTimeout(() => {
- if (loginWindow) {
- loginWindow.show();
- loginWindow.focus();
- }
- }, 100);
- });
- app.on('window-all-closed', () => {
- if (process.platform !== 'darwin') {
- app.quit();
- }
- });
- app.on('before-quit', () => {
- app.isQuitting = true;
- wpsHelper.cleanup();
- });
- const gotTheLock = app.requestSingleInstanceLock();
- if (!gotTheLock) {
- app.quit();
- } else {
- app.on('second-instance', () => {
- if (mainWindow) {
- if (mainWindow.isMinimized()) mainWindow.restore();
- mainWindow.show();
- mainWindow.focus();
- }
- });
- }
|