From ffb420f7e76025c7a5a594f036d4720455477447 Mon Sep 17 00:00:00 2001 From: zetaloop Date: Fri, 24 Apr 2026 08:58:35 +0800 Subject: [PATCH] fix(pages): scope order views to the active role --- app/(dashboard)/dashboard/page.tsx | 25 ++++- app/(order)/orders/page.tsx | 171 +++++++++++++++++------------ components/header.tsx | 5 +- 3 files changed, 120 insertions(+), 81 deletions(-) diff --git a/app/(dashboard)/dashboard/page.tsx b/app/(dashboard)/dashboard/page.tsx index 543359e..8e87611 100644 --- a/app/(dashboard)/dashboard/page.tsx +++ b/app/(dashboard)/dashboard/page.tsx @@ -12,9 +12,17 @@ import { CheckCircle, Clock, DollarSign, ListOrdered, Star, TrendingUp, Users } import Link from "next/link" import { useEffect, useState } from "react" +function getOrderRole(role: "consumer" | "player" | "owner" | "admin") { + if (role === "consumer" || role === "player" || role === "owner") { + return role + } + return undefined +} + export default function DashboardPage() { - const { currentRole } = useAuthStore() + const { currentRole, user } = useAuthStore() const isOwner = currentRole === "owner" + const orderRole = getOrderRole(currentRole) const [player, setPlayer] = useState(null) const [shop, setShop] = useState(null) @@ -28,8 +36,8 @@ export default function DashboardPage() { Promise.all([listPlayers(), listShops(), listServices()]) .then(([players, shops, services]) => { if (cancelled) return - setPlayer(players[0] ?? null) - setShop(shops[0] ?? null) + setPlayer(players.find((item) => item.user.id === user?.id) ?? null) + setShop(shops.find((item) => item.owner.id === user?.id) ?? null) setServices(services) }) .catch(() => { @@ -42,14 +50,19 @@ export default function DashboardPage() { return () => { cancelled = true } - }, []) + }, [user?.id]) useEffect(() => { let cancelled = false void (async () => { try { - const orders = await Promise.resolve(listOrders()) + if (!orderRole) { + setOrders([]) + return + } + + const orders = await Promise.resolve(listOrders({ role: orderRole })) if (cancelled) return setOrders(orders) } catch { @@ -61,7 +74,7 @@ export default function DashboardPage() { return () => { cancelled = true } - }, []) + }, [orderRole]) const totalOrders = isOwner ? (shop?.totalOrders ?? 0) : (player?.totalOrders ?? 0) const rating = isOwner ? (shop?.rating ?? 0) : (player?.rating ?? 0) diff --git a/app/(order)/orders/page.tsx b/app/(order)/orders/page.tsx index 8b88e0a..ee3bdaf 100644 --- a/app/(order)/orders/page.tsx +++ b/app/(order)/orders/page.tsx @@ -56,6 +56,13 @@ const ownerTabs = [ { value: "disputed", label: "争议" }, ] +function getOrderRole(role: UserRole): "consumer" | "player" | "owner" | undefined { + if (role === "consumer" || role === "player" || role === "owner") { + return role + } + return undefined +} + export default function OrderListPage() { const { currentRole, user } = useAuthStore() const shops = useShopStore((state) => state.shops) @@ -80,6 +87,7 @@ function OrderListContent({ userId?: string ownerShopId?: string }) { + const orderRole = getOrderRole(currentRole) const [tab, setTab] = useState("all") const [orders, setOrders] = useState>>([]) const [sessions, setSessions] = useState>>([]) @@ -87,9 +95,15 @@ function OrderListContent({ useEffect(() => { let cancelled = false + if (!orderRole) { + return () => { + cancelled = true + } + } + void (async () => { try { - const items = await Promise.resolve(listOrders()) + const items = await Promise.resolve(listOrders({ role: orderRole })) if (cancelled) return setOrders(items) } catch { @@ -101,7 +115,7 @@ function OrderListContent({ return () => { cancelled = true } - }, []) + }, [orderRole]) useEffect(() => { let cancelled = false @@ -148,83 +162,94 @@ function OrderListContent({ ? "客户视角" : currentRole === "player" ? "打手视角" - : "店主视角"} + : currentRole === "owner" + ? "店主视角" + : "管理员视角"} - setTab(v as TabFilter | "pending")}> - - {tabs.map((item) => ( - - {item.label} - - ))} - + {!orderRole ? ( + + + 当前身份暂不支持订单视角 + + + ) : ( + setTab(v as TabFilter | "pending")}> + + {tabs.map((item) => ( + + {item.label} + + ))} + - - {filtered.length === 0 ? ( - - - 暂无订单 - - - ) : ( - filtered.map((order) => ( - - -
- {order.service.title} - - {statusLabels[order.status]} - -
-

{order.service.title}

-
- -
-
- ¥{order.totalPrice} - - - {new Date(order.createdAt).toLocaleDateString("zh-CN")} - -
-
- {(() => { - if (order.status !== "in_progress" && order.status !== "pending_close") - return null - const session = sessions.find( - (item) => item.type === "order" && item.orderId === order.id, - ) - if (!session) return null - return ( - - ) - })()} - {order.status === "completed" && ( - - )} - -
-
+ + {filtered.length === 0 ? ( + + + 暂无订单 - )) - )} - -
+ ) : ( + filtered.map((order) => ( + + +
+ {order.service.title} + + {statusLabels[order.status]} + +
+

{order.service.title}

+
+ +
+
+ ¥{order.totalPrice} + + + {new Date(order.createdAt).toLocaleDateString("zh-CN")} + +
+
+ {(() => { + if (order.status !== "in_progress" && order.status !== "pending_close") { + return null + } + const session = sessions.find( + (item) => item.type === "order" && item.orderId === order.id, + ) + if (!session) return null + return ( + + ) + })()} + {order.status === "completed" && ( + + )} + +
+
+
+
+ )) + )} + +
+ )} ) } diff --git a/components/header.tsx b/components/header.tsx index c143296..14d8ddb 100644 --- a/components/header.tsx +++ b/components/header.tsx @@ -50,6 +50,7 @@ export function Header() { const ownerShop = useShopStore((state) => user ? state.shops.find((shop) => shop.owner.id === user.id) : undefined, ) + const canOpenDashboard = currentRole === "player" || currentRole === "owner" const navLinks = currentRole === "consumer" @@ -64,7 +65,7 @@ export function Header() { { href: "/community", label: "社区" }, { href: "/orders", label: "订单" }, { href: "/chat", label: "消息" }, - { href: "/dashboard", label: "管理后台" }, + ...(canOpenDashboard ? [{ href: "/dashboard", label: "管理后台" }] : []), ] const availableRoles = (Object.entries(roleLabels) as [UserRole, string][]).filter(([role]) => @@ -209,7 +210,7 @@ export function Header() { 钱包 - {(currentRole === "player" || currentRole === "owner") && ( + {canOpenDashboard && (