274 行
6.9 KiB
JavaScript
274 行
6.9 KiB
JavaScript
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();
|
|
}
|
|
});
|
|
}
|