import { ipcMain } from 'electron'; import { IPC_EVENTS } from '@common/constants'; import { launchLocalChrome } from '@main/utils/chrome/launchLocalChrome' import { executeScriptService } from '@main/service/execute-script-service'; import path from 'path' export function runTaskOperationService() { const executeScriptServiceInstance = new executeScriptService(); ipcMain.handle(IPC_EVENTS.EXECUTE_SCRIPT, async (_event, options: any) => { try { /** * options参数包括:房型、日期范围 * 房型:亲子房、雅致套房 * 日期范围:2023-10-01至2023-10-07 * 备注:操作的渠道有:飞猪、美团、抖音来客 * 这里需要一个排队任务,用队列来处理各渠道的操作路径自动化,先将任务加入队列,然后按顺序执行脚本 */ await launchLocalChrome(options) const result = await executeScriptServiceInstance.executeScript(path.join(__dirname, '../../scripts/fg_trace.js'), options) return { success: true, result }; } catch (error: any) { return { success: false, error: error.message }; } }); } // export function runTaskOperationService() { // ipcMain.handle(IPC_EVENTS.EXECUTE_SCRIPT, async (event, params) => { // logManager.info('Received task operation:', params); // return new Promise((resolve, reject) => { // // 脚本路径 // const scriptPath = path.join(__dirname, '../../scripts/fg_trace.js'); // const child = fork(scriptPath, [], { // stdio: ['pipe', 'pipe', 'pipe', 'ipc'], // env: { ...process.env, ...params } // 传递环境变量 // }); // let output = ''; // let errorOutput = ''; // if (child.stdout) { // child.stdout.on('data', (data) => { // const msg = data.toString(); // logManager.info(`[Task Script]: ${msg}`); // output += msg; // }); // } // if (child.stderr) { // child.stderr.on('data', (data) => { // const msg = data.toString(); // logManager.error(`[Task Script Error]: ${msg}`); // errorOutput += msg; // }); // } // child.on('close', (code) => { // logManager.info(`Task script exited with code ${code}`); // if (code === 0) { // resolve({ success: true, output }); // } else { // // 如果是因为模块找不到或语法错误退出,这里会捕获 // reject(new Error(`Script exited with code ${code}. Error: ${errorOutput}`)); // } // }); // child.on('error', (err) => { // logManager.error('Failed to start task script:', err); // reject(err); // }); // }); // }); // }