fix: 消息通知页不能点进详情 — 补全 product_serial_number
问题: 点击通知卡片跳转 detail?taskId=xxx,但 detail.vue onLoad 只认 serial 参数导致空白页 修复: 1. 后端 NotificationResponse 新增 product_serial_number 字段 2. 后端 notifications API 联表 tasks+products 批量填充 serial 3. 前端 notify/index.vue handleCardTap 优先用 product_serial_number 跳转,兜底从 content 正则解析 4. 前端 detail.vue onLoad 新增 taskId 兜底 → doQueryByTask 反查 product_sn
This commit is contained in:
@ -4,9 +4,12 @@ import uuid
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from sqlalchemy import select, func
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.orm import selectinload
|
||||
|
||||
from app.core.database import get_db
|
||||
from app.models.notification import Notification
|
||||
from app.models.task import Task
|
||||
from app.models.product import Product
|
||||
from app.schemas.notification import NotificationResponse, NotificationListResponse
|
||||
|
||||
router = APIRouter(prefix="/notifications", tags=["消息通知"])
|
||||
@ -46,10 +49,28 @@ async def list_notifications(
|
||||
result = await db.execute(stmt)
|
||||
notifications = result.scalars().all()
|
||||
|
||||
# 🚀 批量查询关联的 product_serial_number
|
||||
task_ids = [n.task_id for n in notifications if n.task_id]
|
||||
serial_map: dict[uuid.UUID, str] = {}
|
||||
if task_ids:
|
||||
task_result = await db.execute(
|
||||
select(Task.id, Product.serial_number)
|
||||
.join(Product, Task.product_id == Product.id)
|
||||
.where(Task.id.in_(task_ids))
|
||||
)
|
||||
for row in task_result:
|
||||
serial_map[row[0]] = row[1]
|
||||
|
||||
# 组装响应
|
||||
response_list: list[NotificationResponse] = []
|
||||
for n in notifications:
|
||||
resp = NotificationResponse.model_validate(n)
|
||||
if n.task_id and n.task_id in serial_map:
|
||||
resp.product_serial_number = serial_map[n.task_id]
|
||||
response_list.append(resp)
|
||||
|
||||
return NotificationListResponse(
|
||||
notifications=[
|
||||
NotificationResponse.model_validate(n) for n in notifications
|
||||
],
|
||||
notifications=response_list,
|
||||
total=total,
|
||||
unread_count=unread_count,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user