// 与壳子后端交互(同源,统一账户会话 cookie 自动携带)+ WebSocket 实时流 export const PLUGIN_ID = 'xq0031_xq0032' async function j(url, opts) { const res = await fetch(url, opts) if (res.status === 401) { throw Object.assign(new Error('未登录:请先在壳子登录统一账户再打开本界面'), { code: 401 }) } 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 function loadConfig() { return j(`/api/plugin-config/${PLUGIN_ID}`) } export function saveConfig(data) { return j(`/api/plugin-config/${PLUGIN_ID}`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data), }) } export function startRun(fields) { return j('/api/runs', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ plugin_id: PLUGIN_ID, fields }), }) } export function getRun(runId) { return j(`/api/runs/${runId}`) } export function stopRun(runId) { return j(`/api/runs/${runId}/stop`, { method: 'POST' }) } export function connectWS(onEvent) { const ws = new WebSocket((location.protocol === 'https:' ? 'wss://' : 'ws://') + location.host + '/ws') ws.onmessage = (e) => { try { onEvent(JSON.parse(e.data)) } catch (_) { /* 忽略脏帧 */ } } return ws }