78 lines
4.1 KiB
JavaScript
78 lines
4.1 KiB
JavaScript
import { request } from '../api'
|
|||
|
|
|
||
|
|
/**
|
||
|
|
* 自动化规则与任务接口封装(任务 005 后端实际契约,见 docs/接口设计.md 第 10、11 章)。
|
||
|
|
* 只依赖既有 request 封装与统一响应结构,不引入第三方 HTTP 库;失败时抛出可被 describeError 识别的错误。
|
||
|
|
*/
|
||
|
|
function buildQuery(params = {}) {
|
||
|
|
const search = new URLSearchParams()
|
||
|
|
for (const [key, value] of Object.entries(params || {})) {
|
||
|
|
if (value === undefined || value === null || value === '' || value === 'all') continue
|
||
|
|
search.set(key, String(value))
|
||
|
|
}
|
||
|
|
const text = search.toString()
|
||
|
|
return text ? `?${text}` : ''
|
||
|
|
}
|
||
|
|
|
||
|
|
function jsonBody(payload) {
|
||
|
|
return { method: 'POST', body: JSON.stringify(payload) }
|
||
|
|
}
|
||
|
|
|
||
|
|
function post() {
|
||
|
|
return { method: 'POST' }
|
||
|
|
}
|
||
|
|
|
||
|
|
export const automationApi = {
|
||
|
|
emergencyState: () => request('/api/v1/automation/emergency-stop'),
|
||
|
|
emergencyStop: (reason) => request('/api/v1/automation/emergency-stop', jsonBody({ reason })),
|
||
|
|
resume: () => request('/api/v1/automation/resume', post()),
|
||
|
|
|
||
|
|
rules: (params) => request(`/api/v1/automation/rules${buildQuery(params)}`),
|
||
|
|
createRule: (payload) => request('/api/v1/automation/rules', jsonBody(payload)),
|
||
|
|
// 写操作可带 qqAccount:后端据此校验规则归属账号,跨账号编辑会被拒绝并提示切换账号。
|
||
|
|
updateRule: (ruleUid, payload, qqAccount) =>
|
||
|
|
request(`/api/v1/automation/rules/${encodeURIComponent(ruleUid)}${buildQuery({ qqAccount })}`,
|
||
|
|
{ method: 'PATCH', body: JSON.stringify(payload) }),
|
||
|
|
rule: (ruleUid) => request(`/api/v1/automation/rules/${encodeURIComponent(ruleUid)}`),
|
||
|
|
versions: (ruleUid) => request(`/api/v1/automation/rules/${encodeURIComponent(ruleUid)}/versions`),
|
||
|
|
createVersion: (ruleUid, payload, qqAccount) =>
|
||
|
|
request(`/api/v1/automation/rules/${encodeURIComponent(ruleUid)}/versions${buildQuery({ qqAccount })}`,
|
||
|
|
jsonBody(payload)),
|
||
|
|
validateVersion: (ruleUid, versionUid) =>
|
||
|
|
request(`/api/v1/automation/rules/${encodeURIComponent(ruleUid)}/versions/${encodeURIComponent(versionUid)}/validate`,
|
||
|
|
post()),
|
||
|
|
publish: (ruleUid, versionUid, reason, qqAccount) =>
|
||
|
|
request(`/api/v1/automation/rules/${encodeURIComponent(ruleUid)}/versions/${encodeURIComponent(versionUid)}/publish${buildQuery({ qqAccount })}`,
|
||
|
|
jsonBody({ reason })),
|
||
|
|
rollback: (ruleUid, versionNumber, reason, qqAccount) =>
|
||
|
|
request(`/api/v1/automation/rules/${encodeURIComponent(ruleUid)}/versions/${versionNumber}/rollback${buildQuery({ qqAccount })}`,
|
||
|
|
jsonBody({ reason })),
|
||
|
|
safetyLimits: () => request('/api/v1/automation/safety-limits'),
|
||
|
|
batchRules: (action, ruleUids, reason) =>
|
||
|
|
request('/api/v1/automation/rules/batch', jsonBody({ action, ruleUids, reason })),
|
||
|
|
hits: (params) => request(`/api/v1/automation/hits${buildQuery(params)}`),
|
||
|
|
disable: (ruleUid, reason) =>
|
||
|
|
request(`/api/v1/automation/rules/${encodeURIComponent(ruleUid)}/disable`, jsonBody({ reason })),
|
||
|
|
archive: (ruleUid, reason) =>
|
||
|
|
request(`/api/v1/automation/rules/${encodeURIComponent(ruleUid)}/archive`, jsonBody({ reason })),
|
||
|
|
execute: (ruleUid, payload) =>
|
||
|
|
request(`/api/v1/automation/rules/${encodeURIComponent(ruleUid)}/execute`, jsonBody(payload)),
|
||
|
|
testRun: (ruleUid, payload) =>
|
||
|
|
request(`/api/v1/automation/rules/${encodeURIComponent(ruleUid)}/test-run`, jsonBody(payload)),
|
||
|
|
schedule: (ruleUid) => request(`/api/v1/automation/rules/${encodeURIComponent(ruleUid)}/schedule`),
|
||
|
|
setScheduleEnabled: (ruleUid, enabled, reason) =>
|
||
|
|
request(`/api/v1/automation/rules/${encodeURIComponent(ruleUid)}/schedule`,
|
||
|
|
jsonBody({ enabled, reason })),
|
||
|
|
webhook: (ruleUid) => request(`/api/v1/automation/rules/${encodeURIComponent(ruleUid)}/webhook`),
|
||
|
|
|
||
|
|
tasks: (params) => request(`/api/v1/tasks${buildQuery(params)}`),
|
||
|
|
task: (taskUid) => request(`/api/v1/tasks/${encodeURIComponent(taskUid)}`),
|
||
|
|
taskStatus: (taskUid) => request(`/api/v1/tasks/${encodeURIComponent(taskUid)}/status`),
|
||
|
|
retryTask: (taskUid, reason) =>
|
||
|
|
request(`/api/v1/tasks/${encodeURIComponent(taskUid)}/retry`, jsonBody({ reason })),
|
||
|
|
cancelTask: (taskUid, reason) =>
|
||
|
|
request(`/api/v1/tasks/${encodeURIComponent(taskUid)}/cancel`, jsonBody({ reason }))
|
||
|
|
}
|
||
|
|
|
||
|
|
export default automationApi
|