From e5546efa417171cc3e25be4a6dfa56667e530541 Mon Sep 17 00:00:00 2001 From: iven Date: Tue, 28 Apr 2026 19:47:48 +0800 Subject: [PATCH] =?UTF-8?q?refactor(web):=20alerts=20+=20deviceReadings=20?= =?UTF-8?q?API=20=E8=BF=81=E7=A7=BB=E4=B8=BA=E5=AF=B9=E8=B1=A1=E9=A3=8E?= =?UTF-8?q?=E6=A0=BC=E5=AF=BC=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - alerts.ts: listAlerts → alertApi.list, acknowledgeAlert → alertApi.acknowledge 等 - deviceReadings.ts: batchCreateReadings → deviceReadingApi.batchCreate 等 - AlertList/AlertRuleList 引用处同步更新 - 其余 19 个函数式 API 文件记为待迁移(旧文件不强制迁移) --- apps/web/src/api/health/alerts.ts | 64 +++++++-------------- apps/web/src/api/health/deviceReadings.ts | 38 ++++-------- apps/web/src/pages/health/AlertList.tsx | 10 ++-- apps/web/src/pages/health/AlertRuleList.tsx | 13 ++--- 4 files changed, 42 insertions(+), 83 deletions(-) diff --git a/apps/web/src/api/health/alerts.ts b/apps/web/src/api/health/alerts.ts index 4347367..afe6101 100644 --- a/apps/web/src/api/health/alerts.ts +++ b/apps/web/src/api/health/alerts.ts @@ -57,53 +57,31 @@ export interface UpdateAlertRuleReq { version: number; } -// --- Alert API --- -export async function listAlerts(params?: { - patient_id?: string; - status?: string; - page?: number; - page_size?: number; -}) { - const res = await client.get('/health/alerts', { params }); - return res.data.data as PaginatedResponse; -} +// --- API --- +export const alertApi = { + list: (params?: { patient_id?: string; status?: string; page?: number; page_size?: number }) => + client.get('/health/alerts', { params }).then((r) => r.data.data as PaginatedResponse), -export async function acknowledgeAlert(id: string, version: number) { - const res = await client.put(`/health/alerts/${id}/acknowledge`, { version }); - return res.data.data as Alert; -} + acknowledge: (id: string, version: number) => + client.put(`/health/alerts/${id}/acknowledge`, { version }).then((r) => r.data.data as Alert), -export async function dismissAlert(id: string, version: number) { - const res = await client.put(`/health/alerts/${id}/dismiss`, { version }); - return res.data.data as Alert; -} + dismiss: (id: string, version: number) => + client.put(`/health/alerts/${id}/dismiss`, { version }).then((r) => r.data.data as Alert), -export async function resolveAlert(id: string, version: number) { - const res = await client.put(`/health/alerts/${id}/resolve`, { version }); - return res.data.data as Alert; -} + resolve: (id: string, version: number) => + client.put(`/health/alerts/${id}/resolve`, { version }).then((r) => r.data.data as Alert), +}; -// --- Alert Rule API --- -export async function listAlertRules(params?: { - device_type?: string; - page?: number; - page_size?: number; -}) { - const res = await client.get('/health/alert-rules', { params }); - return res.data.data as PaginatedResponse; -} +export const alertRuleApi = { + list: (params?: { device_type?: string; page?: number; page_size?: number }) => + client.get('/health/alert-rules', { params }).then((r) => r.data.data as PaginatedResponse), -export async function createAlertRule(data: CreateAlertRuleReq) { - const res = await client.post('/health/alert-rules', data); - return res.data.data as AlertRule; -} + create: (data: CreateAlertRuleReq) => + client.post('/health/alert-rules', data).then((r) => r.data.data as AlertRule), -export async function updateAlertRule(id: string, data: UpdateAlertRuleReq) { - const res = await client.put(`/health/alert-rules/${id}`, data); - return res.data.data as AlertRule; -} + update: (id: string, data: UpdateAlertRuleReq) => + client.put(`/health/alert-rules/${id}`, data).then((r) => r.data.data as AlertRule), -export async function deactivateAlertRule(id: string, version: number) { - const res = await client.put(`/health/alert-rules/${id}/deactivate`, { version }); - return res.data.data as AlertRule; -} + deactivate: (id: string, version: number) => + client.put(`/health/alert-rules/${id}/deactivate`, { version }).then((r) => r.data.data as AlertRule), +}; diff --git a/apps/web/src/api/health/deviceReadings.ts b/apps/web/src/api/health/deviceReadings.ts index ceb3e43..b1fdfca 100644 --- a/apps/web/src/api/health/deviceReadings.ts +++ b/apps/web/src/api/health/deviceReadings.ts @@ -40,31 +40,17 @@ export interface BatchResult { } // --- API --- -export async function batchCreateReadings(patientId: string, data: BatchReadingRequest) { - const res = await client.post(`/health/patients/${patientId}/device-readings/batch`, data); - return res.data.data as BatchResult; -} +export const deviceReadingApi = { + batchCreate: (patientId: string, data: BatchReadingRequest) => + client.post(`/health/patients/${patientId}/device-readings/batch`, data).then((r) => r.data.data as BatchResult), -export async function queryReadings(params: { - patient_id: string; - device_type?: string; - hours?: number; - page?: number; - page_size?: number; -}) { - const { patient_id, ...query } = params; - const res = await client.get(`/health/patients/${patient_id}/device-readings`, { params: query }); - return res.data.data as PaginatedResponse; -} + query: (params: { patient_id: string; device_type?: string; hours?: number; page?: number; page_size?: number }) => { + const { patient_id, ...query } = params; + return client.get(`/health/patients/${patient_id}/device-readings`, { params: query }).then((r) => r.data.data as PaginatedResponse); + }, -export async function queryHourlyReadings(params: { - patient_id: string; - device_type: string; - days?: number; - page?: number; - page_size?: number; -}) { - const { patient_id, ...query } = params; - const res = await client.get(`/health/patients/${patient_id}/device-readings/hourly`, { params: query }); - return res.data.data as PaginatedResponse; -} + queryHourly: (params: { patient_id: string; device_type: string; days?: number; page?: number; page_size?: number }) => { + const { patient_id, ...query } = params; + return client.get(`/health/patients/${patient_id}/device-readings/hourly`, { params: query }).then((r) => r.data.data as PaginatedResponse); + }, +}; diff --git a/apps/web/src/pages/health/AlertList.tsx b/apps/web/src/pages/health/AlertList.tsx index 8a41e6d..a7ced0c 100644 --- a/apps/web/src/pages/health/AlertList.tsx +++ b/apps/web/src/pages/health/AlertList.tsx @@ -14,9 +14,7 @@ import { import { CheckOutlined, StopOutlined } from '@ant-design/icons'; import type { ColumnsType, TablePaginationConfig } from 'antd/es/table'; import { - listAlerts, - acknowledgeAlert, - dismissAlert, + alertApi, type Alert, } from '../../api/health/alerts'; import { AuthButton } from '../../components/AuthButton'; @@ -110,7 +108,7 @@ export default function AlertList() { refresh, } = usePaginatedData( async (p, pageSize, f) => { - const result = await listAlerts({ + const result = await alertApi.list({ page: p, page_size: pageSize, status: f.status || undefined, @@ -146,7 +144,7 @@ export default function AlertList() { const handleAcknowledge = async (record: Alert) => { setActionLoading(record.id); try { - await acknowledgeAlert(record.id, record.version); + await alertApi.acknowledge(record.id, record.version); message.success('告警已确认'); refresh(); } catch { @@ -159,7 +157,7 @@ export default function AlertList() { const handleDismiss = async (record: Alert) => { setActionLoading(record.id); try { - await dismissAlert(record.id, record.version); + await alertApi.dismiss(record.id, record.version); message.success('告警已忽略'); refresh(); } catch { diff --git a/apps/web/src/pages/health/AlertRuleList.tsx b/apps/web/src/pages/health/AlertRuleList.tsx index e4d850b..9747e5c 100644 --- a/apps/web/src/pages/health/AlertRuleList.tsx +++ b/apps/web/src/pages/health/AlertRuleList.tsx @@ -3,10 +3,7 @@ import { Button, Form, Input, InputNumber, message, Modal, Select, Space, Switch import type { ColumnsType } from 'antd/es/table'; import { - createAlertRule, - deactivateAlertRule, - listAlertRules, - updateAlertRule, + alertRuleApi, type AlertRule, type CreateAlertRuleReq, type UpdateAlertRuleReq, @@ -53,7 +50,7 @@ export default function AlertRuleList() { const fetchRules = useCallback(async () => { setLoading(true); try { - const res = await listAlertRules({ page, page_size: 20 }); + const res = await alertRuleApi.list({ page, page_size: 20 }); setData(res.data); setTotal(res.total); } catch { @@ -105,7 +102,7 @@ export default function AlertRuleList() { cooldown_minutes: values.cooldown_minutes, version: editingRule.version, }; - await updateAlertRule(editingRule.id, req); + await alertRuleApi.update(editingRule.id, req); message.success('规则已更新'); } else { const req: CreateAlertRuleReq = { @@ -117,7 +114,7 @@ export default function AlertRuleList() { severity: values.severity, cooldown_minutes: values.cooldown_minutes, }; - await createAlertRule(req); + await alertRuleApi.create(req); message.success('规则已创建'); } setModalOpen(false); @@ -132,7 +129,7 @@ export default function AlertRuleList() { const handleToggle = async (rule: AlertRule, active: boolean) => { try { if (!active) { - await deactivateAlertRule(rule.id, rule.version); + await alertRuleApi.deactivate(rule.id, rule.version); message.success('规则已禁用'); } fetchRules();