Files
cunkebao_v3/SuperAdmin/app/dashboard/page.tsx

128 lines
3.8 KiB
TypeScript
Raw Normal View History

2025-04-09 14:07:54 +08:00
"use client"
import { useState, useEffect } from "react"
2025-04-09 09:45:06 +08:00
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
import { Building2, Users, UserCog } from "lucide-react"
import { toast } from "sonner"
import useAuthCheck from "@/hooks/useAuthCheck"
import { getAdminInfo, getGreeting } from "@/lib/utils"
import ClientOnly from "@/components/ClientOnly"
2025-04-09 09:45:06 +08:00
interface DashboardStats {
companyCount: number
adminCount: number
customerCount: number
}
2025-04-09 14:07:54 +08:00
export default function DashboardPage() {
const [stats, setStats] = useState<DashboardStats>({
companyCount: 0,
adminCount: 0,
customerCount: 0
})
const [isLoading, setIsLoading] = useState(true)
const [greeting, setGreeting] = useState("")
const [userName, setUserName] = useState("")
// 验证用户是否已登录
useAuthCheck()
2025-04-09 14:07:54 +08:00
useEffect(() => {
const fetchData = async () => {
setIsLoading(true)
try {
// 获取统计信息
const response = await fetch(`${process.env.NEXT_PUBLIC_API_BASE_URL}/dashboard/base`)
const data = await response.json()
if (data.code === 200) {
setStats(data.data)
} else {
toast.error(data.msg || "获取统计信息失败")
}
// 获取用户信息
const adminInfo = getAdminInfo()
if (adminInfo) {
setUserName(adminInfo.name || "管理员")
} else {
setUserName("管理员")
}
} catch (error) {
toast.error("网络错误,请稍后重试")
} finally {
setIsLoading(false)
}
}
fetchData()
}, [])
// 单独处理问候语,避免依赖问题
useEffect(() => {
const updateGreeting = () => {
if (userName) {
setGreeting(getGreeting(userName))
}
}
updateGreeting()
// 每分钟更新一次问候语,以防用户长时间停留在页面
const interval = setInterval(updateGreeting, 60000)
return () => clearInterval(interval)
}, [userName])
2025-04-09 09:45:06 +08:00
return (
<div className="space-y-6">
<h1 className="text-3xl font-bold">使</h1>
<p className="text-muted-foreground">
<ClientOnly fallback={`你好,${userName}`}>
{greeting || getGreeting(userName)}
</ClientOnly>
</p>
2025-04-09 09:45:06 +08:00
<div className="grid gap-4 md:grid-cols-3">
2025-04-09 09:45:06 +08:00
<Card>
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
<CardTitle className="text-sm font-medium"></CardTitle>
<Building2 className="h-4 w-4 text-muted-foreground" />
2025-04-09 09:45:06 +08:00
</CardHeader>
<CardContent>
<div className="text-2xl font-bold">
{isLoading ? "..." : stats.companyCount}
</div>
2025-04-09 09:45:06 +08:00
</CardContent>
</Card>
<Card>
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
<CardTitle className="text-sm font-medium"></CardTitle>
<Users className="h-4 w-4 text-muted-foreground" />
</CardHeader>
<CardContent>
<div className="text-2xl font-bold">
{isLoading ? "..." : stats.customerCount}
</div>
2025-04-09 09:45:06 +08:00
</CardContent>
</Card>
<Card>
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
<CardTitle className="text-sm font-medium"></CardTitle>
<UserCog className="h-4 w-4 text-muted-foreground" />
</CardHeader>
<CardContent>
<div className="text-2xl font-bold">
{isLoading ? "..." : stats.adminCount}
</div>
2025-04-09 09:45:06 +08:00
</CardContent>
</Card>
</div>
</div>
)
}