/** 消息通知 — 1:1 复刻 uni-app pages/notify/index.vue + 真实 API */ import { useEffect, useState } from "react"; import { useNavigate } from "react-router-dom"; import { useAuth } from "../contexts/AuthContext"; import { getNotifications, markNotificationRead, type NotificationItem, } from "../services/notificationApi"; // ============================================================ // 类型常量 // ============================================================ const TYPE_CONFIG: Record = { TRANSFER: { icon: "🟢", title: "新任务派发" }, REJECT: { icon: "🔴", title: "品质驳回提醒" }, }; function formatTime(t: string) { if (!t) return ""; const d = new Date(t); const pad = (n: number) => String(n).padStart(2, "0"); return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}`; } // ============================================================ // Page 组件 // ============================================================ export default function NotificationsPage() { const { user } = useAuth(); const navigate = useNavigate(); const [notifications, setNotifications] = useState([]); const [loading, setLoading] = useState(true); useEffect(() => { fetchNotifications(); }, [user]); // eslint-disable-line react-hooks/exhaustive-deps async function fetchNotifications() { const userId = user?.username || user?.id || ""; if (!userId) { setLoading(false); return; } setLoading(true); try { const res = await getNotifications(userId); setNotifications(res.notifications || []); } catch { setNotifications([]); } finally { setLoading(false); } } async function handleCardTap(item: NotificationItem) { // 标记已读 if (!item.is_read) { try { await markNotificationRead(item.id); setNotifications((prev) => prev.map((n) => (n.id === item.id ? { ...n, is_read: true } : n)) ); } catch { // 静默 } } // 跳转详情页 if (item.task_id) { navigate(`/scan?taskId=${item.task_id}`); } } // ========================================================== // 渲染 // ========================================================== return (
{/* 标题区 — 与 uni-app 完全一致 */}

消息通知

任务流转和系统通知

{/* 加载中 */} {loading && (
加载中...
)} {/* 空状态 */} {!loading && notifications.length === 0 && (
🔔 暂无新消息
)} {/* 通知列表 */} {!loading && notifications.length > 0 && (
{notifications.map((item) => { const cfg = TYPE_CONFIG[item.type] || { icon: "📌", title: "系统通知", }; return (
handleCardTap(item)} className={`flex cursor-pointer items-start gap-2.5 rounded-xl bg-white p-3.5 shadow-[0_1px_3px_rgba(0,0,0,0.06)] transition-all active:scale-[0.98] ${ !item.is_read ? "border-l-[3px] border-l-blue-600 shadow-[0_1px_6px_rgba(37,99,235,0.1)]" : "" }`} > {/* 左侧:未读红点 + 类型图标 */}
{!item.is_read && ( )} {cfg.icon}
{/* 右侧:内容 */}
{cfg.title} {formatTime(item.created_at)}

{item.content}

›
); })}
)}
); }