新增设备别名管理功能,支持设备别名的保存与解绑

This commit is contained in:
2026-04-17 15:08:39 +08:00
parent 71760cbb1f
commit 70d42e6792
9 changed files with 570 additions and 72 deletions
+82
View File
@@ -0,0 +1,82 @@
import { NextResponse } from "next/server";
import {
unbindDeviceFromCaregiver,
updateCaregiverDeviceAlias,
} from "@/lib/monitor-data";
import { getDeviceName } from "@/lib/panel-format";
import { getCaregiverSession } from "@/lib/session";
export async function PATCH(request: Request) {
const body = (await request.json().catch(() => null)) as
| {
deviceUuid?: string;
alias?: string | null;
}
| null;
if (!body?.deviceUuid || typeof body.deviceUuid !== "string") {
return NextResponse.json({ error: "请先提供目标设备。" }, { status: 400 });
}
try {
const caregiver = await getCaregiverSession();
if (!caregiver) {
return NextResponse.json({ error: "请先登录账号。" }, { status: 401 });
}
const binding = await updateCaregiverDeviceAlias({
caregiverId: caregiver.id,
rawDeviceValue: body.deviceUuid,
alias: typeof body.alias === "string" || body.alias === null ? body.alias : undefined,
});
return NextResponse.json({
ok: true,
alias: binding.alias,
deviceName: getDeviceName(binding.alias, binding.elderDevice.displayName),
});
} catch (error) {
return NextResponse.json(
{
error: error instanceof Error ? error.message : "设备别名保存失败。",
},
{ status: 400 },
);
}
}
export async function DELETE(request: Request) {
const body = (await request.json().catch(() => null)) as
| {
deviceUuid?: string;
}
| null;
if (!body?.deviceUuid || typeof body.deviceUuid !== "string") {
return NextResponse.json({ error: "请先提供目标设备。" }, { status: 400 });
}
try {
const caregiver = await getCaregiverSession();
if (!caregiver) {
return NextResponse.json({ error: "请先登录账号。" }, { status: 401 });
}
const result = await unbindDeviceFromCaregiver(caregiver.id, body.deviceUuid);
return NextResponse.json({
ok: true,
deviceUuid: result.deviceUuid,
});
} catch (error) {
return NextResponse.json(
{
error: error instanceof Error ? error.message : "解绑设备失败。",
},
{ status: 400 },
);
}
}
+7 -1
View File
@@ -1,7 +1,7 @@
import Link from "next/link";
import { notFound } from "next/navigation";
import { SendMessageForm } from "@/components/dashboard-actions";
import { ManageDeviceForm, SendMessageForm } from "@/components/dashboard-actions";
import { PanelShell } from "@/components/panel-shell";
import { getCaregiverDeviceDetail } from "@/lib/caregiver-panel";
import {
@@ -74,6 +74,12 @@ export default async function DeviceDetailPage({ params }: DeviceDetailPageProps
<section className="grid gap-6 xl:grid-cols-[0.95fr_1.05fr]">
<div className="space-y-6">
<ManageDeviceForm
deviceUuid={binding.elderDevice.deviceUuid}
initialAlias={binding.alias}
deviceLabel={getDeviceName(binding.elderDevice.displayName)}
/>
<SendMessageForm deviceUuid={binding.elderDevice.deviceUuid} />
<section className="rounded-[32px] border border-white/75 bg-white/82 p-6 shadow-[0_26px_60px_rgba(115,76,42,0.12)]">