// 与 FastAPI 后端交互 + WebSocket 实时流 const WS_BASE = (location.protocol === 'https:' ? 'wss://' : 'ws://') + location.host async function j(url, opts) { const res = await fetch(url, opts) if (!res.ok) { let msg = `HTTP ${res.status}` try { msg = (await res.json()).detail || msg } catch (e) {} throw new Error(msg) } return res.json() } export async function fetchMeta() { return j('/api/meta') } // 启动状态(启动页展示当前步骤,无需登录) export async function getBootStatus() { return j('/api/boot/status') } export async function syncPlugins() { return j('/api/plugins/sync', { method: 'POST' }) } // ===== 插件回滚(升级前自动备份上一版,坏包可回退) ===== export async function listRollbacks() { return j('/api/plugins/rollback') } export async function rollbackPlugin(id) { return j(`/api/plugins/rollback/${encodeURIComponent(id)}`, { method: 'POST' }) } // ===== 场景配置方案(多配置,SQLite 存储) ===== export async function listPluginConfigs(pluginId) { return j(`/api/plugin-configs?plugin_id=${encodeURIComponent(pluginId)}`) } export async function savePluginConfig(pluginId, name, fields) { return j('/api/plugin-configs', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ plugin_id: pluginId, name, fields }), }) } export async function deletePluginConfig(pluginId, cfgId) { return j(`/api/plugin-configs/${encodeURIComponent(pluginId)}/${encodeURIComponent(cfgId)}`, { method: 'DELETE' }) } export async function getShellStatus() { return j('/api/shell/update-status') } export async function checkShellUpdate() { return j('/api/shell/check', { method: 'POST' }) } export async function downloadShellUpdate() { return j('/api/shell/download', { method: 'POST' }) } export async function applyShellUpdate() { return j('/api/shell/apply', { method: 'POST' }) } // ===== 浏览器(Chrome)安装 ===== export async function getChromeStatus() { return j('/api/chrome/status') } export async function checkChrome() { return j('/api/chrome/check', { method: 'POST' }) } export async function downloadChrome() { return j('/api/chrome/download', { method: 'POST' }) } export async function installChrome() { return j('/api/chrome/install', { method: 'POST' }) } // ===== 环境包(便携 Python 运行时) ===== export async function getEnvStatus() { return j('/api/env/status') } export async function syncEnv() { return j('/api/env/sync', { method: 'POST' }) } export async function rollbackEnv() { return j('/api/env/rollback', { method: 'POST' }) } export async function loginPlatform(id) { return j(`/api/platforms/${id}/login`, { method: 'POST' }) } export async function logoutPlatform(id) { return j(`/api/platforms/${id}/logout`, { method: 'POST' }) } // 当前账号的平台状态(个人中心):在线态 + 是否已配置个人账号密码 + 已存用户名 export async function getPlatformStatus() { return j('/api/platforms/status') } // 保存当前账号的 HR/OA 账号密码(本机加密);pass 留空表示保留原密码 export async function savePlatformCreds(payload) { return j('/api/platforms/creds', { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload), }) } export async function startRun(pluginId, fields) { return j('/api/runs', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ plugin_id: pluginId, fields }), }) } export async function getRun(runId) { return j(`/api/runs/${runId}`) } // 停止一次运行:排队中 = 出队取消,执行中 = 整树强杀 export async function stopRun(runId) { return j(`/api/runs/${runId}/stop`, { method: 'POST' }) } // 执行队列快照(WS 断线/首屏时兜底) export async function fetchQueue() { return j('/api/queue') } // 运行历史列表(当前账号,新的在前):执行台「历史运行」数据源 export async function fetchRuns(limit = 30) { return j(`/api/runs?limit=${limit}`) } // ===== 插件备注(账号 × 插件 唯一;本机缓存 + 回传中心机)===== export async function fetchPluginNotes() { return j('/api/plugin-notes') } export async function savePluginNote(pluginId, note) { return j('/api/plugin-notes', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ plugin_id: pluginId, note }), }) } // ===== 流程编排(DAG) ===== export async function listFlows() { return j('/api/flows') } export async function createFlow(body) { return j('/api/flows', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) }) } export async function updateFlow(id, body) { return j(`/api/flows/${encodeURIComponent(id)}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) }) } export async function deleteFlow(id) { return j(`/api/flows/${encodeURIComponent(id)}`, { method: 'DELETE' }) } export async function runFlow(id) { return j(`/api/flows/${encodeURIComponent(id)}/run`, { method: 'POST' }) } export async function listFlowRuns() { return j('/api/flows/runs') } // 流程版本历史 / 回滚 export async function listFlowVersions(id) { return j(`/api/flows/${encodeURIComponent(id)}/versions`) } export async function rollbackFlow(id, vid) { return j(`/api/flows/${encodeURIComponent(id)}/rollback`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ vid }), }) } // 人工确认节点:ok=true 继续,false 终止后续 export async function confirmFlowNode(flowRunId, nodeId, ok) { return j(`/api/flows/runs/${encodeURIComponent(flowRunId)}/confirm`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ node_id: nodeId, ok }), }) } // ===== 定时任务(壳子内置定时,配置自动备份中心机) ===== export async function listSchedules() { return j('/api/schedules') } export async function createSchedule(body) { return j('/api/schedules', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), }) } export async function updateSchedule(sid, body) { return j(`/api/schedules/${sid}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), }) } export async function deleteSchedule(sid) { return j(`/api/schedules/${sid}`, { method: 'DELETE' }) } export async function toggleSchedule(sid, enabled) { return j(`/api/schedules/${sid}/toggle`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ enabled }), }) } export function connectWS(onEvent) { const ws = new WebSocket(WS_BASE + '/ws') ws.onmessage = (e) => { try { onEvent(JSON.parse(e.data)) } catch (_) {} } return ws } // ===== 统一账户登录/会话(底座仅保留认证与个人密码,管理走中心机控制面) ===== export async function adminMe() { return j('/api/admin/me') } export async function adminLogin(account, password) { return j('/api/admin/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ account, password }) }) } // 离线登录:凭本机 7 天离线授权票据直接进入(不依赖网络/服务端) export async function adminOfflineLogin() { return j('/api/admin/offline-login', { method: 'POST' }) } // v3 钉钉直连接登录:生成钉钉授权 URL(redirect_uri=本机 /auth/done),前端当前窗口跳转到该 URL export async function authDingtalk() { return j('/api/auth/dingtalk') } export async function adminLogout() { return j('/api/admin/logout', { method: 'POST' }) } export async function adminPassword(body) { return j('/api/admin/password', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) }) } export async function adminAvatarUpload(file) { const fd = new FormData() fd.append('file', file) return j('/api/avatar', { method: 'POST', body: fd }) }