71 lines
2.3 KiB
Python
71 lines
2.3 KiB
Python
from fastapi import APIRouter, Depends, HTTPException
|
|
from pydantic import BaseModel
|
|
|
|
from app.middleware.auth import get_current_user
|
|
from app.models.analysis import Analysis
|
|
from app.models.report import Report
|
|
from app.models.user import User
|
|
from app.utils.helpers import parse_object_id, mask_name
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
class ShareOptions(BaseModel):
|
|
privacy_mode: bool = True # 隐私模式,脱敏个人信息
|
|
include_details: bool = False # 是否包含详细数据
|
|
|
|
|
|
@router.post("/{report_id}/generate")
|
|
async def generate_share_data(
|
|
report_id: str,
|
|
options: ShareOptions = ShareOptions(privacy_mode=True, include_details=True),
|
|
user: User = Depends(get_current_user),
|
|
):
|
|
"""生成分享数据"""
|
|
oid = parse_object_id(report_id)
|
|
report = await Report.get(oid)
|
|
if not report or report.user_id != user.id:
|
|
raise HTTPException(status_code=404, detail="报告不存在")
|
|
|
|
analysis = await Analysis.find_one(Analysis.report_id == report.id)
|
|
if not analysis:
|
|
raise HTTPException(status_code=404, detail="暂无解读结果")
|
|
|
|
# Build share data
|
|
share_data = {
|
|
"report_type": report.report_type,
|
|
"pregnancy_week": report.pregnancy_week,
|
|
"overall_status": analysis.overall_status,
|
|
"risk_level": analysis.risk_level,
|
|
"overall_summary": analysis.overall_summary,
|
|
"emotional_summary": analysis.emotional_summary,
|
|
"action_items": [a.model_dump() for a in analysis.action_items],
|
|
"tips": analysis.tips,
|
|
"categories": [
|
|
{
|
|
"category": cat.category,
|
|
"status": cat.status,
|
|
"indicators": [
|
|
{
|
|
"name": ind.name,
|
|
"nickname": ind.nickname,
|
|
"value": ind.value,
|
|
"status": ind.status,
|
|
"what_is_it": ind.what_is_it,
|
|
"impact": ind.impact,
|
|
"suggestion": ind.suggestion,
|
|
}
|
|
for ind in cat.indicators
|
|
],
|
|
}
|
|
for cat in analysis.categories
|
|
],
|
|
}
|
|
|
|
if not options.privacy_mode:
|
|
share_data["user_name"] = user.name
|
|
else:
|
|
share_data["user_name"] = mask_name(user.name)
|
|
|
|
return {"share_data": share_data}
|