Files
tk-electron-ai/main/services/iosai.js
2025-10-28 19:40:13 +08:00

79 lines
2.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// main/services/iosai.js
const { app } = require('electron')
const path = require('path')
const fs = require('fs')
const { exec } = require('child_process')
const axios = require('axios')
let iosAIProcess = null
let userData = { tenantId: null, userId: null, tokenValue: null } // 如果需要共享,请提供 setter
function resolveIOSAIPath() {
const candidates = [
path.join(path.dirname(process.execPath), 'iOSAI', 'IOSAI.exe'),
path.join(process.resourcesPath || '', 'iOSAI', 'IOSAI.exe'),
path.join(__dirname, '..', 'iOSAI', 'IOSAI.exe'),
path.join(process.cwd(), 'iOSAI', 'IOSAI.exe'),
]
for (const p of candidates) {
try { if (fs.existsSync(p)) return p } catch { }
}
return null
}
function startIOSAIExecutable() {
if (process.platform !== 'win32') {
console.warn('[IOSAI] 非 Windows跳过')
return
}
const exePath = resolveIOSAIPath()
if (!exePath) return console.error('[IOSAI] 未找到 IOSAI.exe')
const exeDir = path.dirname(exePath)
const exeFile = path.basename(exePath)
const cmd = `start "" /D "${exeDir}" "${exeFile}"`
try {
exec(cmd, { cwd: exeDir, windowsHide: false }, (err) => {
if (err) console.error('[IOSAI] 启动失败:', err)
else console.log('[IOSAI] 启动命令已执行')
})
} catch (e) { console.error('[IOSAI] 启动异常:', e) }
}
async function killIOSAI() {
try {
if (userData.userId && userData.tenantId) {
await axios.post('https://crawlclient.api.yolozs.com/api/user/aiChat-logout',
{ userId: userData.userId, tenantId: userData.tenantId },
{ headers: { 'Content-Type': 'application/json', 'vvtoken': userData.tokenValue } }
).catch(e => console.log('[IOSAI] 登出失败', e?.message))
}
if (iosAIProcess?.pid) {
exec(`taskkill /PID ${iosAIProcess.pid} /T /F`, (err) => {
if (err) {
exec('taskkill /IM IOSAI.exe /F')
}
})
} else {
exec('taskkill /IM IOSAI.exe /F')
exec('taskkill /IM iproxy.exe /F')
exec('taskkill /IM tidevice.exe /F')
}
} catch { }
iosAIProcess = null
}
function isProcessRunningWin(exeName) {
return new Promise((resolve) => {
if (process.platform !== 'win32') return resolve(true)
const cmd = `tasklist /FI "IMAGENAME eq ${exeName}" /FO CSV /NH`
exec(cmd, { windowsHide: true }, (err, stdout) => {
if (err || !stdout) return resolve(false)
resolve(stdout.toLowerCase().includes(`"${exeName.toLowerCase()}"`))
})
})
}
module.exports = { startIOSAIExecutable, killIOSAI, isProcessRunningWin }