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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { app, BrowserWindow, ipcMain, screen, desktopCapturer, globalShortcut } from 'electron';
|
| 2 |
+
import * as path from 'path';
|
| 3 |
+
import { AutomationManager } from './automation/manager';
|
| 4 |
+
import { OCRService } from './services/ocr';
|
| 5 |
+
import { createLogger } from './utils/logger';
|
| 6 |
+
|
| 7 |
+
const logger = createLogger('main');
|
| 8 |
+
|
| 9 |
+
// 初始化自动化管理器
|
| 10 |
+
const automationManager = new AutomationManager();
|
| 11 |
+
const ocrService = new OCRService();
|
| 12 |
+
|
| 13 |
+
let mainWindow: BrowserWindow | null = null;
|
| 14 |
+
|
| 15 |
+
function createWindow() {
|
| 16 |
+
const { width, height } = screen.getPrimaryDisplay().workAreaSize;
|
| 17 |
+
|
| 18 |
+
mainWindow = new BrowserWindow({
|
| 19 |
+
width: Math.min(1400, width - 100),
|
| 20 |
+
height: Math.min(900, height - 100),
|
| 21 |
+
minWidth: 1000,
|
| 22 |
+
minHeight: 700,
|
| 23 |
+
webPreferences: {
|
| 24 |
+
nodeIntegration: false,
|
| 25 |
+
contextIsolation: true,
|
| 26 |
+
preload: path.join(__dirname, 'preload.js'),
|
| 27 |
+
},
|
| 28 |
+
titleBarStyle: 'hiddenInset',
|
| 29 |
+
trafficLightPosition: { x: 20, y: 20 },
|
| 30 |
+
icon: path.join(__dirname, '../assets/icon.png'),
|
| 31 |
+
});
|
| 32 |
+
|
| 33 |
+
// 开发环境加载本地服务
|
| 34 |
+
if (process.env.NODE_ENV === 'development') {
|
| 35 |
+
mainWindow.loadURL('http://localhost:3000');
|
| 36 |
+
mainWindow.webContents.openDevTools();
|
| 37 |
+
} else {
|
| 38 |
+
mainWindow.loadFile(path.join(__dirname, '../frontend/index.html'));
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
mainWindow.on('closed', () => {
|
| 42 |
+
mainWindow = null;
|
| 43 |
+
});
|
| 44 |
+
|
| 45 |
+
logger.info('Main window created');
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
// IPC 处理器
|
| 49 |
+
function setupIPC() {
|
| 50 |
+
// 截屏
|
| 51 |
+
ipcMain.handle('capture-screen', async (_, region) => {
|
| 52 |
+
try {
|
| 53 |
+
const image = await ocrService.captureScreen(region);
|
| 54 |
+
return image.toString('base64');
|
| 55 |
+
} catch (error) {
|
| 56 |
+
logger.error('Capture screen failed:', error);
|
| 57 |
+
throw error;
|
| 58 |
+
}
|
| 59 |
+
});
|
| 60 |
+
|
| 61 |
+
// OCR识别
|
| 62 |
+
ipcMain.handle('ocr-recognize', async (_, imageBase64) => {
|
| 63 |
+
try {
|
| 64 |
+
const result = await ocrService.recognize(Buffer.from(imageBase64, 'base64'));
|
| 65 |
+
return result;
|
| 66 |
+
} catch (error) {
|
| 67 |
+
logger.error('OCR recognize failed:', error);
|
| 68 |
+
throw error;
|
| 69 |
+
}
|
| 70 |
+
});
|
| 71 |
+
|
| 72 |
+
// 模拟点击
|
| 73 |
+
ipcMain.handle('automation-click', async (_, x, y, button = 'left') => {
|
| 74 |
+
try {
|
| 75 |
+
await automationManager.click(x, y, button);
|
| 76 |
+
return { success: true };
|
| 77 |
+
} catch (error) {
|
| 78 |
+
logger.error('Click failed:', error);
|
| 79 |
+
throw error;
|
| 80 |
+
}
|
| 81 |
+
});
|
| 82 |
+
|
| 83 |
+
// 模拟输入
|
| 84 |
+
ipcMain.handle('automation-type', async (_, text) => {
|
| 85 |
+
try {
|
| 86 |
+
await automationManager.type(text);
|
| 87 |
+
return { success: true };
|
| 88 |
+
} catch (error) {
|
| 89 |
+
logger.error('Type failed:', error);
|
| 90 |
+
throw error;
|
| 91 |
+
}
|
| 92 |
+
});
|
| 93 |
+
|
| 94 |
+
// 移动鼠标
|
| 95 |
+
ipcMain.handle('automation-move', async (_, x, y) => {
|
| 96 |
+
try {
|
| 97 |
+
await automationManager.moveTo(x, y);
|
| 98 |
+
return { success: true };
|
| 99 |
+
} catch (error) {
|
| 100 |
+
logger.error('Move failed:', error);
|
| 101 |
+
throw error;
|
| 102 |
+
}
|
| 103 |
+
});
|
| 104 |
+
|
| 105 |
+
// 快捷键
|
| 106 |
+
ipcMain.handle('automation-hotkey', async (_, keys) => {
|
| 107 |
+
try {
|
| 108 |
+
await automationManager.hotKey(keys);
|
| 109 |
+
return { success: true };
|
| 110 |
+
} catch (error) {
|
| 111 |
+
logger.error('Hotkey failed:', error);
|
| 112 |
+
throw error;
|
| 113 |
+
}
|
| 114 |
+
});
|
| 115 |
+
|
| 116 |
+
// 查找图片
|
| 117 |
+
ipcMain.handle('automation-find-image', async (_, templateBase64) => {
|
| 118 |
+
try {
|
| 119 |
+
const position = await automationManager.findImage(
|
| 120 |
+
Buffer.from(templateBase64, 'base64')
|
| 121 |
+
);
|
| 122 |
+
return position;
|
| 123 |
+
} catch (error) {
|
| 124 |
+
logger.error('Find image failed:', error);
|
| 125 |
+
throw error;
|
| 126 |
+
}
|
| 127 |
+
});
|
| 128 |
+
|
| 129 |
+
// 平台自动化 - 千牛
|
| 130 |
+
ipcMain.handle('platform-taobao-init', async (_, config) => {
|
| 131 |
+
try {
|
| 132 |
+
await automationManager.initPlatform('taobao', config);
|
| 133 |
+
return { success: true };
|
| 134 |
+
} catch (error) {
|
| 135 |
+
logger.error('Init taobao platform failed:', error);
|
| 136 |
+
throw error;
|
| 137 |
+
}
|
| 138 |
+
});
|
| 139 |
+
|
| 140 |
+
ipcMain.handle('platform-taobao-poll', async () => {
|
| 141 |
+
try {
|
| 142 |
+
const result = await automationManager.pollNewMessages('taobao');
|
| 143 |
+
return result;
|
| 144 |
+
} catch (error) {
|
| 145 |
+
logger.error('Poll taobao failed:', error);
|
| 146 |
+
throw error;
|
| 147 |
+
}
|
| 148 |
+
});
|
| 149 |
+
|
| 150 |
+
ipcMain.handle('platform-taobao-send', async (_, customerId, message) => {
|
| 151 |
+
try {
|
| 152 |
+
await automationManager.sendMessage('taobao', customerId, message);
|
| 153 |
+
return { success: true };
|
| 154 |
+
} catch (error) {
|
| 155 |
+
logger.error('Send message failed:', error);
|
| 156 |
+
throw error;
|
| 157 |
+
}
|
| 158 |
+
});
|
| 159 |
+
|
| 160 |
+
logger.info('IPC handlers setup complete');
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
app.whenReady().then(() => {
|
| 164 |
+
createWindow();
|
| 165 |
+
setupIPC();
|
| 166 |
+
|
| 167 |
+
// 注册全局快捷键
|
| 168 |
+
globalShortcut.register('CommandOrControl+Shift+O', () => {
|
| 169 |
+
mainWindow?.show();
|
| 170 |
+
});
|
| 171 |
+
|
| 172 |
+
app.on('activate', () => {
|
| 173 |
+
if (BrowserWindow.getAllWindows().length === 0) {
|
| 174 |
+
createWindow();
|
| 175 |
+
}
|
| 176 |
+
});
|
| 177 |
+
});
|
| 178 |
+
|
| 179 |
+
app.on('window-all-closed', () => {
|
| 180 |
+
if (process.platform !== 'darwin') {
|
| 181 |
+
app.quit();
|
| 182 |
+
}
|
| 183 |
+
});
|
| 184 |
+
|
| 185 |
+
app.on('will-quit', () => {
|
| 186 |
+
globalShortcut.unregisterAll();
|
| 187 |
+
automationManager.stop();
|
| 188 |
+
});
|