import { mockNotifyEvents, mockSecuritySetting, mockSystemLogs, mockTaskSetting, mockTelegramSetting, } from './mock' import { mockOn, mocked, request } from './request' import type { AboutInfo, NotifyChannelItem, NotifyChannelType, NotifyEventsSetting, NotifyTemplateItem, SecuritySetting, SystemLogPage, TaskSetting, TelegramSetting, UpdateNotifyChannelBody, } from '@/types/api' export function getTelegramSetting(): Promise { if (mockOn) return mocked({ ...mockTelegramSetting }) return request('/settings/telegram') } export interface UpdateTelegramBody { enabled: boolean /** 缺省表示沿用已保存的 token,空串表示清除 */ botToken?: string chatId: string } export function updateTelegramSetting(body: UpdateTelegramBody): Promise { if (mockOn) { mockTelegramSetting.enabled = body.enabled mockTelegramSetting.chatId = body.chatId if (body.botToken !== undefined) { mockTelegramSetting.tokenSet = body.botToken !== '' mockTelegramSetting.tokenTail = body.botToken.length >= 8 ? body.botToken.slice(-4) : '' } return mocked({ ...mockTelegramSetting }, 400) } return request('/settings/telegram', { method: 'PUT', body }) } export function testTelegram(): Promise { if (mockOn) return mocked(undefined, 800) return request('/settings/telegram/test', { method: 'POST' }) } /** 通知渠道(webhook/ntfy/bark/smtp)脱敏视图列表 */ export function listNotifyChannels(): Promise { if (mockOn) return mocked( (['webhook', 'ntfy', 'bark', 'smtp'] as NotifyChannelType[]).map((type) => ({ type, enabled: false, secretSet: false, })), ) return request<{ items: NotifyChannelItem[] }>('/settings/notify-channels').then((r) => r.items) } /** 保存单渠道配置,返回全渠道最新视图 */ export function updateNotifyChannel( type: NotifyChannelType, body: UpdateNotifyChannelBody, ): Promise { if (mockOn) return mocked([], 400) return request<{ items: NotifyChannelItem[] }>(`/settings/notify-channels/${type}`, { method: 'PUT', body, }).then((r) => r.items) } /** 向指定渠道发送测试消息(用已保存配置,不看启用开关) */ export function testNotifyChannel(type: NotifyChannelType): Promise { if (mockOn) return mocked(undefined, 800) return request(`/settings/notify-channels/${type}/test`, { method: 'POST' }) } export function getNotifyEvents(): Promise { if (mockOn) return mocked({ ...mockNotifyEvents }) return request('/settings/notify-events') } /** 全量保存五个事件开关,返回最新值 */ export function updateNotifyEvents(body: NotifyEventsSetting): Promise { if (mockOn) { Object.assign(mockNotifyEvents, body) return mocked({ ...mockNotifyEvents }, 400) } return request('/settings/notify-events', { method: 'PUT', body }) } export function listNotifyTemplates(): Promise { if (mockOn) return mocked([ { kind: 'task_fail', label: '任务失败', vars: ['task_name', 'error'], defaultTemplate: '❌ 任务失败:{{task_name}}\n{{error}}', template: '', }, ]) return request<{ items: NotifyTemplateItem[] }>('/settings/notify-templates').then((r) => r.items) } /** 保存单个通知模板;空串清除自定义(恢复默认) */ export function updateNotifyTemplate(kind: string, template: string): Promise { if (mockOn) return mocked(undefined, 300) return request(`/settings/notify-templates/${kind}`, { method: 'PUT', body: { template } }) } /** 用示例变量渲染并发送测试消息;template 非空时按编辑中内容渲染(不落库) */ export function testNotifyTemplate(kind: string, template: string): Promise { if (mockOn) return mocked(undefined, 600) return request(`/settings/notify-templates/${kind}/test`, { method: 'POST', body: { template } }) } export function getTaskSetting(): Promise { if (mockOn) return mocked({ ...mockTaskSetting }) return request('/settings/task') } /** 保存任务行为设置,返回最新值;阈值越界(1-10 之外)后端返回 400 */ export function updateTaskSetting(body: TaskSetting): Promise { if (mockOn) { Object.assign(mockTaskSetting, body) return mocked({ ...mockTaskSetting }, 400) } return request('/settings/task', { method: 'PUT', body }) } export function getSecuritySetting(): Promise { if (mockOn) return mocked({ ...mockSecuritySetting }) return request('/settings/security') } /** 保存安全设置,返回最新值;越界或非法后端返回 400,保存后立即生效 */ export function updateSecuritySetting(body: SecuritySetting): Promise { if (mockOn) { Object.assign(mockSecuritySetting, body) return mocked({ ...mockSecuritySetting }, 400) } return request('/settings/security', { method: 'PUT', body }) } export interface SystemLogParams { page: number pageSize: number /** 关键字,对路径与用户名模糊匹配 */ keyword?: string } export function listSystemLogs(params: SystemLogParams): Promise { if (mockOn) { const kw = (params.keyword ?? '').toLowerCase() const filtered = kw ? mockSystemLogs.filter( (l) => l.path.toLowerCase().includes(kw) || l.username.toLowerCase().includes(kw), ) : mockSystemLogs const start = (params.page - 1) * params.pageSize return mocked({ items: filtered.slice(start, start + params.pageSize), total: filtered.length, }) } return request('/system-logs', { query: { ...params } }) } // ---- 关于 ---- /** 构建与运行环境信息(设置 · 关于) */ export function getAbout(): Promise { if (mockOn) return mocked({ version: 'v1.0.0', buildTime: '2026-07-09T12:00Z', goVersion: 'go1.26.4', platform: 'linux/amd64', startedAt: '2026-07-01T08:23:00+08:00', uptimeSeconds: 1053360, // 12 天 4 小时 36 分 resources: { cpuAvgPercent: 1.4, numCpu: 4, goroutines: 36, memHeapBytes: 50537000, memSysBytes: 101187000, dbEngine: 'sqlite', dbBytes: 13212000, }, }) return request('/about') }