家庭动态:老人/家属发言、三种可见范围,家庭内部内容不会播给老人。 共同照护:家庭备忘录、任务创建、认领、完成与交接记录。 用药闭环:计划、未来 7 天提醒实例、确认服用、延后、异常反馈。 风险闭环:红橙黄蓝分级、30 分钟去重、推送、认领、处理与误报记录。 报平安:家属发起、老人语音回应、状态同步。 Web 导航升级为守护、家庭、照护、陪伴、我的。 设备接口已升级为设备令牌认证,UUID 不再单独承担认证。
57 lines
1.9 KiB
TypeScript
57 lines
1.9 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
|
|
import { createBindUrl, ensureDeviceRegistration } from "@/lib/monitor-data";
|
|
import { createDeviceToken, hashDeviceToken } from "@/lib/device-auth";
|
|
import { prisma } from "@/lib/prisma";
|
|
|
|
export async function POST(request: Request) {
|
|
const body = (await request.json().catch(() => null)) as
|
|
| {
|
|
deviceUuid?: string;
|
|
displayName?: string;
|
|
appVersion?: string;
|
|
deviceToken?: string;
|
|
}
|
|
| null;
|
|
|
|
if (!body?.deviceUuid || typeof body.deviceUuid !== "string") {
|
|
return NextResponse.json(
|
|
{ error: "deviceUuid 是必填项。" },
|
|
{ status: 400 },
|
|
);
|
|
}
|
|
|
|
try {
|
|
const device = await ensureDeviceRegistration({
|
|
deviceUuid: body.deviceUuid,
|
|
displayName:
|
|
typeof body.displayName === "string" ? body.displayName : undefined,
|
|
appVersion:
|
|
typeof body.appVersion === "string" ? body.appVersion : undefined,
|
|
});
|
|
const suppliedToken = typeof body.deviceToken === "string" ? body.deviceToken.trim() : "";
|
|
const tokenMatches = suppliedToken && device.deviceTokenHash === hashDeviceToken(suppliedToken);
|
|
if (device.deviceTokenHash && !tokenMatches) {
|
|
return NextResponse.json({ error: "设备认证失败,不能覆盖已注册设备。" }, { status: 401 });
|
|
}
|
|
const deviceToken = tokenMatches ? suppliedToken : createDeviceToken();
|
|
if (!device.deviceTokenHash) {
|
|
await prisma.elderDevice.update({ where: { id: device.id }, data: { deviceTokenHash: hashDeviceToken(deviceToken) } });
|
|
}
|
|
|
|
return NextResponse.json({
|
|
deviceUuid: device.deviceUuid,
|
|
bindUrl: createBindUrl(device.deviceUuid),
|
|
deviceToken,
|
|
});
|
|
} catch (error) {
|
|
return NextResponse.json(
|
|
{
|
|
error:
|
|
error instanceof Error ? error.message : "设备注册失败,请稍后再试。",
|
|
},
|
|
{ status: 500 },
|
|
);
|
|
}
|
|
}
|