新增小程序后端代码,包括数据库、路由、服务层等。
小程序前后端打通,包括登录、注册、绑定设备、查询绑定信息,修改小朋友名称等功能。
This commit is contained in:
45
mini-program/app/service/binding.py
Normal file
45
mini-program/app/service/binding.py
Normal file
@@ -0,0 +1,45 @@
|
||||
from collections.abc import Mapping
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
from app.dao.binding import BindingDAO
|
||||
|
||||
|
||||
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]:
|
||||
bind_token = self.dao.start_bind(user_id, device_id, child_id)
|
||||
return bind_token, datetime.utcnow()
|
||||
|
||||
def confirm_bind(self, bind_token: str, user_id: int) -> Mapping:
|
||||
session = self.dao.get_session(bind_token, user_id)
|
||||
if not session:
|
||||
raise ValueError("Bind session not found")
|
||||
if datetime.utcnow() > session["expires_at"]:
|
||||
raise ValueError("Bind session expired")
|
||||
if session["status"] != 1:
|
||||
raise ValueError("Bind session already processed")
|
||||
|
||||
self.dao.confirm_bind(session["id"], session["device_id"], session["target_child_id"], user_id)
|
||||
return {"device_id": session["device_id"], "child_id": session["target_child_id"]}
|
||||
|
||||
def get_binding(self, device_id: str, user_id: int) -> Optional[Mapping]:
|
||||
return self.dao.get_by_device(device_id, user_id)
|
||||
|
||||
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:
|
||||
self.dao.direct_bind(device_id, child_id, user_id)
|
||||
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)
|
||||
|
||||
def list_history(self, device_id: str, limit: int = 20, cursor: datetime = None) -> tuple[list, bool]:
|
||||
rows = self.dao.list_history(device_id, limit, cursor)
|
||||
has_more = len(rows) > limit
|
||||
rows = rows[:limit]
|
||||
return rows, has_more
|
||||
Reference in New Issue
Block a user