Merge branch 'develop' of https://gitee.com/Tyssen/yi-shi into develop

This commit is contained in:
Ghost
2025-04-10 17:34:00 +08:00
3 changed files with 69 additions and 21 deletions

View File

@@ -4,6 +4,8 @@ import { useEffect, useState } from "react"
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
import { Users, FolderKanban, UserCog } from "lucide-react"
import useAuthCheck from "@/hooks/useAuthCheck"
import { getAdminInfo, getGreeting } from "@/lib/utils"
import ClientOnly from "@/components/ClientOnly"
export default function DashboardPage() {
const [greeting, setGreeting] = useState("")
@@ -14,36 +16,39 @@ export default function DashboardPage() {
useEffect(() => {
// 获取用户信息
const adminInfo = localStorage.getItem("admin_info")
const adminInfo = getAdminInfo()
if (adminInfo) {
try {
const userData = JSON.parse(adminInfo)
setUserName(userData.name || "管理员")
} catch (err) {
console.error("解析用户信息失败:", err)
setUserName("管理员")
}
}
// 获取当前时间
const hour = new Date().getHours()
if (hour >= 5 && hour < 12) {
setGreeting("上午好")
} else if (hour >= 12 && hour < 14) {
setGreeting("中午好")
} else if (hour >= 14 && hour < 18) {
setGreeting("下午好")
setUserName(adminInfo.name || "管理员")
} else {
setGreeting("晚上好")
setUserName("管理员")
}
}, [])
// 单独处理问候语,避免依赖问题
useEffect(() => {
// 设置问候语
const updateGreeting = () => {
if (userName) {
setGreeting(getGreeting(userName))
}
}
updateGreeting()
// 每分钟更新一次问候语,以防用户长时间停留在页面
const interval = setInterval(updateGreeting, 60000)
return () => clearInterval(interval)
}, [userName])
return (
<div className="space-y-6">
<h1 className="text-3xl font-bold">使</h1>
<p className="text-muted-foreground">
{greeting}{userName}
<ClientOnly fallback={`你好,${userName}`}>
{greeting || getGreeting(userName)}
</ClientOnly>
</p>
<div className="grid gap-6 md:grid-cols-2 lg:grid-cols-3">

View File

@@ -0,0 +1,18 @@
"use client"
import { useState, useEffect, type ReactNode } from 'react'
/**
* ClientOnly组件
* 该组件专门用于包装那些只能在客户端渲染的内容,避免水合不匹配错误
* 例如使用了Date.now()或window对象的组件
*/
export default function ClientOnly({ children, fallback = null }: { children: ReactNode, fallback?: ReactNode }) {
const [isClient, setIsClient] = useState(false)
useEffect(() => {
setIsClient(true)
}, [])
return isClient ? <>{children}</> : <>{fallback}</>
}

View File

@@ -73,3 +73,28 @@ export function clearAdminInfo(): void {
localStorage.removeItem('admin_token');
}
}
/**
* 根据当前时间获取问候语
* @param username 用户名
* @returns 包含时间段的问候语
*/
export function getGreeting(username: string): string {
if (typeof window === 'undefined') {
return `你好,${username}`;
}
const hours = new Date().getHours();
if (hours >= 0 && hours < 6) {
return `凌晨好,${username}`;
} else if (hours >= 6 && hours < 12) {
return `上午好,${username}`;
} else if (hours >= 12 && hours < 14) {
return `中午好,${username}`;
} else if (hours >= 14 && hours < 18) {
return `下午好,${username}`;
} else {
return `晚上好,${username}`;
}
}