From 8e62b154030b85b8797669bdc2be378c46cee0e3 Mon Sep 17 00:00:00 2001 From: zetaloop Date: Mon, 23 Feb 2026 11:04:16 +0800 Subject: [PATCH] refactor(api): add adapter layer for order/chat/review/dispute writes --- app/(order)/chat/[id]/page.tsx | 46 ++++++++--------- app/(order)/dispute/[id]/page.tsx | 25 ++++----- app/(order)/order/[id]/page.tsx | 7 --- app/(order)/review/[id]/page.tsx | 9 ++-- components/order-actions.tsx | 60 ++++++++------------- lib/api/chat.ts | 42 +++++++++++++++ lib/api/disputes.ts | 41 +++++++++++++++ lib/api/orders.ts | 86 +++++++++++++++++++++++++++++++ lib/api/reviews.ts | 16 ++++++ store/chat.ts | 24 ++++----- 10 files changed, 258 insertions(+), 98 deletions(-) diff --git a/app/(order)/chat/[id]/page.tsx b/app/(order)/chat/[id]/page.tsx index 3837a06..19f0ef5 100644 --- a/app/(order)/chat/[id]/page.tsx +++ b/app/(order)/chat/[id]/page.tsx @@ -9,6 +9,7 @@ import { Badge } from "@/components/ui/badge" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { ScrollArea } from "@/components/ui/scroll-area" +import { sendImageMessage, sendTextMessage } from "@/lib/api/chat" import { cn } from "@/lib/utils" import { useAuthStore } from "@/store/auth" import { useChatStore } from "@/store/chat" @@ -25,8 +26,6 @@ export default function ChatDetailPage({ params }: { params: Promise<{ id: strin () => allMessages.filter((item) => item.sessionId === id), [allMessages, id], ) - const sendTextMessage = useChatStore((state) => state.sendTextMessage) - const sendImageMessage = useChatStore((state) => state.sendImageMessage) const [input, setInput] = useState("") const imageInputRef = useRef(null) const { user } = useAuthStore() @@ -39,8 +38,25 @@ export default function ChatDetailPage({ params }: { params: Promise<{ id: strin ) } - const userId = user?.id ?? session.participants[0].id - const other = session.participants.find((p) => p.id !== userId) ?? session.participants[1] + if (!user?.id) { + return ( +
+ 请先登录后查看会话 +
+ ) + } + + const userId = user.id + const isParticipant = session.participants.some((participant) => participant.id === userId) + if (!isParticipant) { + return ( +
+ 仅会话参与方可查看并发送消息 +
+ ) + } + + const other = session.participants.find((p) => p.id !== userId) ?? session.participants[0] return (
@@ -130,16 +146,7 @@ export default function ChatDetailPage({ params }: { params: Promise<{ id: strin onChange={(event) => { const file = event.target.files?.[0] if (!file) return - const sender = session.participants.find((participant) => participant.id === userId) - sendImageMessage( - session.id, - { - id: userId, - name: sender?.name ?? user?.nickname ?? "", - avatar: sender?.avatar ?? user?.avatar ?? "", - }, - URL.createObjectURL(file), - ) + sendImageMessage(session.id, URL.createObjectURL(file)) event.target.value = "" }} /> @@ -150,16 +157,7 @@ export default function ChatDetailPage({ params }: { params: Promise<{ id: strin const text = input.trim() if (!text) return - const sender = session.participants.find((participant) => participant.id === userId) - sendTextMessage( - session.id, - { - id: userId, - name: sender?.name ?? user?.nickname ?? "", - avatar: sender?.avatar ?? user?.avatar ?? "", - }, - text, - ) + sendTextMessage(session.id, text) setInput("") }} > diff --git a/app/(order)/dispute/[id]/page.tsx b/app/(order)/dispute/[id]/page.tsx index 455cf9f..a757d77 100644 --- a/app/(order)/dispute/[id]/page.tsx +++ b/app/(order)/dispute/[id]/page.tsx @@ -16,6 +16,7 @@ import { import { Badge } from "@/components/ui/badge" import { Button } from "@/components/ui/button" import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card" +import { submitDispute, submitDisputeAppeal, submitDisputeResponse } from "@/lib/api/disputes" import { DISPUTE_TO_RESOLVED_MS } from "@/lib/config/demo-timers" import { notifyInfo } from "@/lib/toast" import { Label } from "@/components/ui/label" @@ -38,11 +39,7 @@ export default function DisputePage({ params }: { params: Promise<{ id: string } const searchParams = useSearchParams() const order = useOrderStore((state) => state.orders.find((item) => item.id === id)) const userId = useAuthStore((state) => state.user?.id) - const userName = useAuthStore((state) => state.user?.nickname) const existingDispute = useDisputeStore((state) => state.getDisputeByOrderId(id)) - const submitDispute = useDisputeStore((state) => state.submitDispute) - const submitResponse = useDisputeStore((state) => state.submitResponse) - const submitAppeal = useDisputeStore((state) => state.submitAppeal) const [reason, setReason] = useState("") const [files, setFiles] = useState([]) @@ -108,11 +105,9 @@ export default function DisputePage({ params }: { params: Promise<{ id: string } } const handleSubmit = () => { - if (!userId || !userName || !reason.trim()) return + if (!userId || !reason.trim()) return const result = submitDispute({ orderId: id, - initiatorId: userId, - initiatorName: userName, reason, evidence: files, }) @@ -287,12 +282,11 @@ export default function DisputePage({ params }: { params: Promise<{ id: string }