本次提交包含多项功能更新: 1. 为多个插件(xq0002/0003/0004/0008/0010/0011/0020/0031_xq0032/0033/0035/0037/0045_xq0046/0047/0048)新增 UI 界面及配置支持 2. 新增运行日志集中回传功能,客户端自动上传运行日志到中心机集中存储 3. 新增插件分类字段支持,用于场景库分组管理 4. 优化 xq0034/0037/0048 等插件的功能及描述 5. 修复控制面响应兼容问题,支持新旧版本数据格式 6. 新增 xq0033 插件的状态存储模块,用于历史记录管理 7. 优化 CRM 登录窗口模式,支持全屏置顶
266 lines
11 KiB
JavaScript
266 lines
11 KiB
JavaScript
// 与 FastAPI 后端交互,控制面前端直连 /api/*
|
||
import { store } from './store.js'
|
||
|
||
async function req(url, opts) {
|
||
const res = await fetch(url, opts)
|
||
if (!res.ok) {
|
||
let msg = `HTTP ${res.status}`
|
||
try {
|
||
const detail = (await res.json()).detail
|
||
// FastAPI 422 的 detail 是 [{loc,msg,type},…] 数组;普通错误是字符串/含 msg 的对象
|
||
if (typeof detail === 'string') msg = detail
|
||
else if (Array.isArray(detail)) {
|
||
msg = detail.map((d) => {
|
||
const where = Array.isArray(d.loc) ? d.loc.filter((p) => p !== 'body').join('.') : ''
|
||
return where ? `${where}: ${d.msg}` : (d.msg || JSON.stringify(d))
|
||
}).join(';')
|
||
} else if (detail && typeof detail === 'object') msg = detail.msg || JSON.stringify(detail)
|
||
} catch (_) { /* 非 JSON 响应,保留 HTTP 状态码提示 */ }
|
||
throw new Error(msg)
|
||
}
|
||
return res.json()
|
||
}
|
||
|
||
// 管理接口无状态鉴权:携带登录后签发的签名票据(X-Admin-Token),后端验票解得当前账号
|
||
function adminHeaders(extra = {}) {
|
||
return { 'X-Admin-Token': store.token || '', ...extra }
|
||
}
|
||
|
||
// 统一分页规范:所有列表接口 page(page≥1) + page_size,返回 {items, total}
|
||
function pagedQuery(params = {}) {
|
||
const qs = new URLSearchParams()
|
||
if (params.page) qs.set('page', params.page)
|
||
if (params.page_size) qs.set('page_size', params.page_size)
|
||
return qs.toString()
|
||
}
|
||
|
||
export async function adminLogin(account, password) {
|
||
return req('/api/admin/auth', {
|
||
method: 'POST',
|
||
headers: { 'Content-Type': 'application/json' },
|
||
body: JSON.stringify({ account, password })
|
||
})
|
||
}
|
||
export async function adminPassword(body) {
|
||
return req('/api/admin/password', {
|
||
method: 'POST',
|
||
headers: adminHeaders({ 'Content-Type': 'application/json' }),
|
||
body: JSON.stringify(body)
|
||
})
|
||
}
|
||
export async function adminMe() {
|
||
return req('/api/admin/me', { headers: adminHeaders() })
|
||
}
|
||
export async function adminOverview() {
|
||
return req('/api/admin/overview', { headers: adminHeaders() })
|
||
}
|
||
export async function adminGrantData() {
|
||
return req('/api/admin/grant_data', { headers: adminHeaders() })
|
||
}
|
||
export async function adminAccountGrants(account) {
|
||
return req('/api/admin/account_grants?account=' + encodeURIComponent(account), { headers: adminHeaders() })
|
||
}
|
||
export async function adminPlatforms() {
|
||
return req('/api/admin/platforms', { headers: adminHeaders() })
|
||
}
|
||
export async function adminGrant(body) {
|
||
return req('/api/admin/account_plugins', {
|
||
method: 'POST',
|
||
headers: adminHeaders({ 'Content-Type': 'application/json' }),
|
||
body: JSON.stringify(body)
|
||
})
|
||
}
|
||
export async function adminPlatformGrant(body) {
|
||
return req('/api/admin/account_platforms', {
|
||
method: 'POST',
|
||
headers: adminHeaders({ 'Content-Type': 'application/json' }),
|
||
body: JSON.stringify(body)
|
||
})
|
||
}
|
||
export async function adminAccounts(page = 1, pageSize = 20, q = '') {
|
||
const qs = new URLSearchParams()
|
||
qs.set('page', page); qs.set('page_size', pageSize)
|
||
if (q) qs.set('q', q)
|
||
return req('/api/admin/accounts?' + qs.toString(), { headers: adminHeaders() })
|
||
}
|
||
export async function adminPlugins(page = 1, pageSize = 20, q = '') {
|
||
const qs = new URLSearchParams()
|
||
qs.set('page', page); qs.set('page_size', pageSize)
|
||
if (q) qs.set('q', q)
|
||
return req('/api/admin/plugins?' + qs.toString(), { headers: adminHeaders() })
|
||
}
|
||
export async function adminAccountCreate(body) {
|
||
return req('/api/admin/account', {
|
||
method: 'POST',
|
||
headers: adminHeaders({ 'Content-Type': 'application/json' }),
|
||
body: JSON.stringify(body)
|
||
})
|
||
}
|
||
export async function adminAccountUpdate(body) {
|
||
return req('/api/admin/account/update', {
|
||
method: 'POST',
|
||
headers: adminHeaders({ 'Content-Type': 'application/json' }),
|
||
body: JSON.stringify(body)
|
||
})
|
||
}
|
||
export async function adminAccountReset(body) {
|
||
return req('/api/admin/account/reset_pwd', {
|
||
method: 'POST',
|
||
headers: adminHeaders({ 'Content-Type': 'application/json' }),
|
||
body: JSON.stringify(body)
|
||
})
|
||
}
|
||
export async function adminAccountDelete(body) {
|
||
return req('/api/admin/account/delete', {
|
||
method: 'POST',
|
||
headers: adminHeaders({ 'Content-Type': 'application/json' }),
|
||
body: JSON.stringify(body)
|
||
})
|
||
}
|
||
export async function adminAvatarUpload(account, file) {
|
||
const fd = new FormData()
|
||
fd.append('file', file)
|
||
const qs = new URLSearchParams({ account })
|
||
return req('/api/admin/avatar?' + qs.toString(), { method: 'POST', headers: adminHeaders(), body: fd })
|
||
}
|
||
export async function adminPluginDelete(body) {
|
||
return req('/api/admin/plugin/delete', {
|
||
method: 'POST',
|
||
headers: adminHeaders({ 'Content-Type': 'application/json' }),
|
||
body: JSON.stringify(body)
|
||
})
|
||
}
|
||
export async function adminPluginPatch(pluginId, body) {
|
||
const qs = new URLSearchParams({ plugin_id: pluginId })
|
||
return req('/api/admin/plugin/patch?' + qs.toString(), {
|
||
method: 'POST',
|
||
headers: adminHeaders({ 'Content-Type': 'application/json' }),
|
||
body: JSON.stringify(body)
|
||
})
|
||
}
|
||
export async function adminAudit(page = 1, pageSize = 20, filters = {}) {
|
||
const qs = new URLSearchParams()
|
||
qs.set('page', page); qs.set('page_size', pageSize)
|
||
if (filters.action) qs.set('action', filters.action)
|
||
if (filters.q) qs.set('q', filters.q)
|
||
return req('/api/admin/audit?' + qs.toString(), { headers: adminHeaders() })
|
||
}
|
||
export async function adminReports(page = 1, pageSize = 20, filters = {}) {
|
||
const qs = new URLSearchParams()
|
||
qs.set('page', page); qs.set('page_size', pageSize)
|
||
if (filters.status) qs.set('status', filters.status)
|
||
if (filters.q) qs.set('q', filters.q)
|
||
return req('/api/admin/reports?' + qs.toString(), { headers: adminHeaders() })
|
||
}
|
||
export async function adminStats() {
|
||
return req('/api/admin/stats', { headers: adminHeaders() })
|
||
}
|
||
// 客户端升级追踪:按版本统计 + 按账号分组(关旧控制面依据)
|
||
export async function adminDevicesSummary() {
|
||
return req('/api/admin/devices/summary', { headers: adminHeaders() })
|
||
}
|
||
export async function adminDevices(page = 1, pageSize = 20) {
|
||
return req('/api/admin/devices?' + pagedQuery({ page, page_size: pageSize }), { headers: adminHeaders() })
|
||
}
|
||
export async function adminCredsList(page = 1, pageSize = 20) {
|
||
return req('/api/admin/creds/manage?' + pagedQuery({ page, page_size: pageSize }), { headers: adminHeaders() })
|
||
}
|
||
export async function adminCredsSeed(body) {
|
||
return req('/api/admin/creds/seed', {
|
||
method: 'POST',
|
||
headers: adminHeaders({ 'Content-Type': 'application/json' }),
|
||
body: JSON.stringify(body)
|
||
})
|
||
}
|
||
export async function adminFileSources(category = '', page = 1, pageSize = 20) {
|
||
const parts = []
|
||
if (category) parts.push('category=' + encodeURIComponent(category))
|
||
const pq = pagedQuery({ page, page_size: pageSize })
|
||
if (pq) parts.push(pq)
|
||
const qs = parts.length ? '?' + parts.join('&') : ''
|
||
return req('/api/admin/file-sources' + qs, { headers: adminHeaders() })
|
||
}
|
||
export async function adminFileSourceCreate(body) {
|
||
return req('/api/admin/file-sources', {
|
||
method: 'POST',
|
||
headers: adminHeaders({ 'Content-Type': 'application/json' }),
|
||
body: JSON.stringify(body)
|
||
})
|
||
}
|
||
export async function adminFileSourcePatch(id, body) {
|
||
return req(`/api/admin/file-sources/${id}`, {
|
||
method: 'PATCH',
|
||
headers: adminHeaders({ 'Content-Type': 'application/json' }),
|
||
body: JSON.stringify(body)
|
||
})
|
||
}
|
||
export async function adminFileSourceDelete(id) {
|
||
return req(`/api/admin/file-sources/${id}`, {
|
||
method: 'DELETE',
|
||
headers: adminHeaders({ 'Content-Type': 'application/json' })
|
||
})
|
||
}
|
||
export async function adminUpload(pluginId, name, version, file) {
|
||
const fd = new FormData()
|
||
fd.append('upload', file)
|
||
const qs = new URLSearchParams({ plugin_id: pluginId, name, version })
|
||
return req('/api/admin/pkg?' + qs.toString(), { method: 'POST', headers: adminHeaders(), body: fd })
|
||
}
|
||
// ===== 通知推送 =====
|
||
export async function adminNotifications(page = 1, pageSize = 20, q = '') {
|
||
const qs = pagedQuery({ page, page_size: pageSize })
|
||
const url = '/api/admin/notifications?' + qs + (q ? '&q=' + encodeURIComponent(q) : '')
|
||
return req(url, { headers: adminHeaders() })
|
||
}
|
||
export async function adminNotificationCreate(body) {
|
||
return req('/api/admin/notifications', {
|
||
method: 'POST',
|
||
headers: adminHeaders({ 'Content-Type': 'application/json' }),
|
||
body: JSON.stringify(body)
|
||
})
|
||
}
|
||
export async function adminNotificationToggle(id, active) {
|
||
return req(`/api/admin/notifications/${id}?active=${active}`, { method: 'PATCH', headers: adminHeaders() })
|
||
}
|
||
export async function adminNotificationDelete(id) {
|
||
return req(`/api/admin/notifications/${id}`, { method: 'DELETE', headers: adminHeaders() })
|
||
}
|
||
// ===== 集中日志(运行日志 / 客户端壳日志) =====
|
||
export async function adminRunLogs(page = 1, pageSize = 20, filters = {}) {
|
||
const qs = new URLSearchParams()
|
||
qs.set('page', page); qs.set('page_size', pageSize)
|
||
if (filters.account) qs.set('account', filters.account)
|
||
if (filters.device) qs.set('device', filters.device)
|
||
if (filters.plugin) qs.set('plugin', filters.plugin)
|
||
if (filters.status) qs.set('status', filters.status)
|
||
if (filters.q) qs.set('q', filters.q)
|
||
if (filters.days) qs.set('days', filters.days)
|
||
return req('/api/admin/run-logs?' + qs.toString(), { headers: adminHeaders() })
|
||
}
|
||
export async function adminRunLogDetail(runId) {
|
||
return req('/api/admin/run-logs/' + encodeURIComponent(runId), { headers: adminHeaders() })
|
||
}
|
||
export async function adminDeviceLogs(page = 1, pageSize = 20, filters = {}) {
|
||
const qs = new URLSearchParams()
|
||
qs.set('page', page); qs.set('page_size', pageSize)
|
||
if (filters.account) qs.set('account', filters.account)
|
||
if (filters.q) qs.set('q', filters.q)
|
||
return req('/api/admin/device-logs?' + qs.toString(), { headers: adminHeaders() })
|
||
}
|
||
export async function adminDeviceLogDetail(deviceId) {
|
||
return req('/api/admin/device-logs/' + encodeURIComponent(deviceId), { headers: adminHeaders() })
|
||
}
|
||
// 壳日志全文下载:需携带管理票据,用 fetch 取 blob 再触发浏览器下载
|
||
export async function adminDeviceLogDownload(deviceId, filename) {
|
||
const res = await fetch('/api/admin/device-logs/' + encodeURIComponent(deviceId) + '/download', { headers: adminHeaders() })
|
||
if (!res.ok) throw new Error(`下载失败 HTTP ${res.status}`)
|
||
const blob = await res.blob()
|
||
const url = URL.createObjectURL(blob)
|
||
const a = document.createElement('a')
|
||
a.href = url; a.download = filename || `shell-${deviceId}.log`
|
||
document.body.appendChild(a); a.click(); a.remove()
|
||
setTimeout(() => URL.revokeObjectURL(url), 5000)
|
||
}
|
||
export async function adminLogsCleanup() {
|
||
return req('/api/admin/logs/cleanup', { method: 'POST', headers: adminHeaders() })
|
||
} |