feat: 飞猪自动化调试完成

This commit is contained in:
duanshuwen
2026-03-10 21:26:17 +08:00
parent 15a7d115cb
commit 8a6544ede2
7 changed files with 124 additions and 130 deletions

View File

@@ -3,6 +3,7 @@ import { ipcMain, app } from 'electron';
import { IPC_EVENTS } from '@common/constants';
import { launchLocalChrome } from '@main/utils/chrome/launchLocalChrome'
import { executeScriptService } from '@main/service/execute-script-service';
import fs from 'fs'
import path from 'path'
import log from 'electron-log';
@@ -11,81 +12,48 @@ export function runTaskOperationService() {
ipcMain.handle(IPC_EVENTS.EXECUTE_SCRIPT, async (_event, options: any) => {
try {
/**
* options参数包括房型、日期范围
* 房型:亲子房、雅致套房
* 日期范围2023-10-01至2023-10-07
* 备注:操作的渠道有:飞猪、美团、抖音来客
* 这里需要一个排队任务,用队列来处理各渠道的操作路径自动化,先将任务加入队列,然后按顺序执行脚本
*/
await launchLocalChrome(options)
// 脚本路径处理
let scriptPath = ''
if (app.isPackaged) {
scriptPath = path.join(process.resourcesPath, 'scripts/fg_trace.js')
} else {
scriptPath = path.join(process.cwd(), 'src/main/scripts/fg_trace.js')
const channels = ['feizhu', 'meituan', 'douyin', 'xiaocheng']
const scriptMap: Record<string, string> = {
feizhu: 'fg_trace.mjs',
meituan: 'mt_trace.mjs',
douyin: 'dy_trace.mjs',
xiaocheng: 'xc_trace.mjs',
}
log.info(`Launching script with path: ${scriptPath}`);
const result = await executeScriptServiceInstance.executeScript(scriptPath, options)
const scriptsDir = app.isPackaged
? path.join(process.resourcesPath, 'scripts')
: path.join(process.cwd(), 'src/main/scripts')
return { success: true, result };
const scriptPaths = channels.map((channel: string) => {
const fileName = scriptMap[channel]
if (!fileName) {
throw new Error(`Unknown channel: ${channel}`)
}
const p = path.join(scriptsDir, fileName)
if (!fs.existsSync(p)) {
throw new Error(`Script not found for channel ${channel}: ${p}`)
}
return { channel, scriptPath: p }
})
const results: any[] = []
for (const item of scriptPaths) {
log.info(`Launching script for channel ${item.channel}: ${item.scriptPath}`)
const result = await executeScriptServiceInstance.executeScript(item.scriptPath, options)
results.push({
channel: item.channel,
scriptPath: item.scriptPath,
...result,
})
}
return { success: true, result: results };
} 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);
// });
// });
// });
// }