feat: 支持先绑定设备后关联儿童档案

- 绑定模型新增 owner_user_id,支持 child_id 为空\n- 新增绑定后设置儿童接口 /bindings/{device_id}/child\n- 前端绑定页改为先绑设备后补充儿童\n- 补充数据库迁移脚本与 DAO 测试用例
This commit is contained in:
ChengCan
2026-04-14 12:47:52 +08:00
parent a22fcafea9
commit 1b4c47f77a
11 changed files with 699 additions and 246 deletions

View File

@@ -9,7 +9,7 @@ class BindingService:
def __init__(self, db):
self.dao = BindingDAO(db)
def start_bind(self, user_id: int, device_id: str, child_id: int) -> tuple[str, datetime]:
def start_bind(self, user_id: int, device_id: str, child_id: int | None = None) -> tuple[str, datetime]:
bind_token = self.dao.start_bind(user_id, device_id, child_id)
return bind_token, datetime.utcnow()
@@ -31,10 +31,16 @@ class BindingService:
def get_current_binding(self, user_id: int) -> Optional[Mapping]:
return self.dao.get_current_by_user(user_id)
def direct_bind(self, device_id: str, child_id: int, user_id: int) -> Mapping:
def direct_bind(self, device_id: str, child_id: int | None, user_id: int) -> Mapping:
self.dao.direct_bind(device_id, child_id, user_id)
return {"device_id": device_id, "child_id": child_id}
def set_binding_child(self, device_id: str, child_id: int, user_id: int) -> Mapping:
ok = self.dao.set_binding_child(device_id=device_id, child_id=child_id, user_id=user_id)
if not ok:
raise ValueError("binding not found")
return {"device_id": device_id, "child_id": child_id}
def unbind(self, device_id: str, user_id: int) -> bool:
return self.dao.unbind(device_id, user_id)
@@ -42,4 +48,4 @@ class BindingService:
rows = self.dao.list_history(device_id, limit, cursor)
has_more = len(rows) > limit
rows = rows[:limit]
return rows, has_more
return rows, has_more