feat: 打开渠道与脚本自动化分离

This commit is contained in:
DEV_DSW
2026-03-12 17:05:16 +08:00
parent f084e47c58
commit 236f4c7dc1
12 changed files with 201 additions and 132 deletions

View File

@@ -3,17 +3,74 @@ 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 { chromium } from 'playwright';
import { addStealthInit, connectCdpContext, safeDisconnectBrowser } from '@main/utils/chrome/cdp'
import fs from 'fs'
import path from 'path'
import log from 'electron-log';
const openedTabIndexByChannelName = new Map<string, number>()
export function runTaskOperationService() {
const executeScriptServiceInstance = new executeScriptService();
// 打开渠道
ipcMain.handle(IPC_EVENTS.OPEN_CHANNEL, async (_event, channels: any) => {
try {
await launchLocalChrome()
const arr = Array.isArray(channels) ? channels : []
const keys = arr
.map((it: any) => typeof it === 'string' ? it : it.channelName)
.filter(Boolean)
if (keys.length === 0) return { success: true }
const urlMap: Record<string, string> = {}
for (const it of arr) {
const name = typeof it === 'string' ? it : it.channelName
const url = typeof it === 'string' ? undefined : it?.channelUrl
if (name && url) urlMap[name] = url
}
const { browser, context } = await connectCdpContext(chromium)
await addStealthInit(context)
for (let i = 0; i < keys.length; i++) {
const key = keys[i]
const targetUrl = urlMap[key]
let pages = await context.pages()
while (pages.length <= i) {
await context.newPage()
pages = await context.pages()
}
const page = pages[i]
await page.bringToFront()
if (targetUrl) {
const current = page.url()
if (!current || !current.startsWith(targetUrl)) {
await page.goto(targetUrl, { waitUntil: 'domcontentloaded' as any })
}
}
openedTabIndexByChannelName.set(key, i)
}
await safeDisconnectBrowser(browser)
return { success: true }
} catch (error) {
return { success: false, error: (error as any)?.message || 'open channel failed' }
}
})
// 执行脚本
ipcMain.handle(IPC_EVENTS.EXECUTE_SCRIPT, async (_event, options: any) => {
try {
await launchLocalChrome(options)
// 从options.roomList列表中找到对应的名称
const roomType = options.roomList.find((item: any) => item.id === options.roomType);
@@ -44,13 +101,21 @@ export function runTaskOperationService() {
const results: any[] = []
for (let i = 0; i < scriptPaths.length; i++) {
const item = scriptPaths[i]
const channelNameMap: Record<string, string> = {
fzName: 'fliggy',
mtName: 'meituan',
dyHotelName: 'douyin',
dyHotSpringName: 'douyin',
}
const mappedName = channelNameMap[item.channel]
const tabIndex = mappedName ? (openedTabIndexByChannelName.get(mappedName) ?? i) : i
log.info(`Launching script for channel ${item.channel}: ${item.scriptPath}`)
const result = await executeScriptServiceInstance.executeScript(item.scriptPath, {
roomType: roomType[item.channel],
startTime: options.startTime,
endTime: options.endTime,
operation: options.operation,
tabIndex: i,
tabIndex,
})
results.push({
channel: item.channel,