// 与 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 syncPlugins() { return j('/api/plugins/sync', { method: 'POST' }) } 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' }) } 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 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 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 }) }) } // 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 }) }