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

298 lines
10 KiB
TypeScript
Raw Normal View History

2025-04-09 09:45:06 +08:00
"use client"
import { useState, useEffect } from "react"
2025-04-09 09:45:06 +08:00
import Link from "next/link"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@/components/ui/dropdown-menu"
import { Search, MoreHorizontal, Edit, Trash, UserPlus, Loader2 } from "lucide-react"
2025-04-09 09:45:06 +08:00
import { Badge } from "@/components/ui/badge"
import { useToast } from "@/components/ui/use-toast"
2025-04-10 16:34:02 +08:00
import { getAdministrators, deleteAdministrator, Administrator } from "@/lib/admin-api"
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from "@/components/ui/alert-dialog"
2025-04-09 09:45:06 +08:00
export default function AdminsPage() {
const [searchTerm, setSearchTerm] = useState("")
const [isLoading, setIsLoading] = useState(true)
2025-04-10 16:34:02 +08:00
const [isDeleting, setIsDeleting] = useState(false)
const [administrators, setAdministrators] = useState<Administrator[]>([])
const [totalCount, setTotalCount] = useState(0)
const [currentPage, setCurrentPage] = useState(1)
const [pageSize] = useState(10)
const { toast } = useToast()
2025-04-10 16:34:02 +08:00
// 删除对话框状态
const [deleteDialogOpen, setDeleteDialogOpen] = useState(false)
const [adminToDelete, setAdminToDelete] = useState<Administrator | null>(null)
2025-04-09 09:45:06 +08:00
// 加载管理员列表
useEffect(() => {
fetchAdministrators()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [currentPage])
// 获取管理员列表
const fetchAdministrators = async (keyword: string = searchTerm) => {
setIsLoading(true)
try {
const response = await getAdministrators(currentPage, pageSize, keyword)
if (response.code === 200 && response.data) {
setAdministrators(response.data.list)
setTotalCount(response.data.total)
} else {
toast({
title: "获取管理员列表失败",
description: response.msg || "请稍后重试",
variant: "destructive",
})
setAdministrators([])
setTotalCount(0)
}
} catch (error) {
console.error("获取管理员列表出错:", error)
toast({
title: "获取管理员列表失败",
description: "请检查网络连接后重试",
variant: "destructive",
})
setAdministrators([])
setTotalCount(0)
} finally {
setIsLoading(false)
}
}
// 处理搜索
const handleSearch = () => {
setCurrentPage(1) // 重置为第一页
fetchAdministrators()
}
// Enter键搜索
const handleSearchKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key === "Enter") {
handleSearch()
}
}
// 检查是否为超级管理员id为1
const isSuperAdmin = (id: number) => {
return id === 1
}
2025-04-10 16:34:02 +08:00
// 打开删除确认对话框
const openDeleteDialog = (admin: Administrator) => {
setAdminToDelete(admin)
setDeleteDialogOpen(true)
}
// 确认删除管理员
const confirmDelete = async () => {
if (!adminToDelete) return
setIsDeleting(true)
try {
const response = await deleteAdministrator(adminToDelete.id)
if (response.code === 200) {
toast({
title: "删除成功",
description: `管理员 ${adminToDelete.username} 已成功删除`,
2025-04-10 16:34:02 +08:00
variant: "success",
})
// 重新获取管理员列表
fetchAdministrators()
} else {
toast({
title: "删除失败",
description: response.msg || "请稍后重试",
variant: "destructive",
})
}
} catch (error) {
console.error("删除管理员出错:", error)
toast({
title: "删除失败",
description: "请检查网络连接后重试",
variant: "destructive",
})
} finally {
setIsDeleting(false)
setDeleteDialogOpen(false)
setAdminToDelete(null)
}
}
2025-04-09 09:45:06 +08:00
return (
<div className="space-y-6">
<div className="flex justify-between">
<h1 className="text-2xl font-bold"></h1>
<Button asChild>
<Link href="/dashboard/admins/new">
<UserPlus className="mr-2 h-4 w-4" />
</Link>
</Button>
</div>
<div className="flex items-center gap-2">
<div className="relative flex-1">
<Search className="absolute left-2.5 top-2.5 h-4 w-4 text-muted-foreground" />
<Input
type="search"
placeholder="搜索管理员账号、姓名或角色..."
className="pl-8"
value={searchTerm}
onChange={(e) => setSearchTerm(e.target.value)}
onKeyDown={handleSearchKeyDown}
2025-04-09 09:45:06 +08:00
/>
</div>
<Button onClick={handleSearch}></Button>
2025-04-09 09:45:06 +08:00
</div>
<div className="rounded-md border">
<Table>
<TableHeader>
<TableRow>
<TableHead></TableHead>
<TableHead></TableHead>
2025-04-09 09:45:06 +08:00
<TableHead></TableHead>
<TableHead></TableHead>
<TableHead></TableHead>
<TableHead></TableHead>
<TableHead className="text-right"></TableHead>
</TableRow>
</TableHeader>
<TableBody>
{isLoading ? (
<TableRow>
<TableCell colSpan={7} className="h-24 text-center">
<div className="flex justify-center">
<Loader2 className="h-6 w-6 animate-spin text-primary" />
</div>
</TableCell>
</TableRow>
) : administrators.length > 0 ? (
administrators.map((admin) => (
2025-04-09 09:45:06 +08:00
<TableRow key={admin.id}>
<TableCell className="font-medium">{admin.account}</TableCell>
<TableCell>{admin.username}</TableCell>
2025-04-09 09:45:06 +08:00
<TableCell>
<Badge variant={admin.role === "超级管理员" ? "default" : "outline"}>{admin.role}</Badge>
</TableCell>
<TableCell>
<div className="flex flex-wrap gap-1">
{admin.permissions.map((permission) => (
<Badge key={permission} variant="secondary">
{permission}
</Badge>
))}
</div>
</TableCell>
<TableCell>{admin.createdAt}</TableCell>
<TableCell>{admin.lastLogin}</TableCell>
<TableCell className="text-right">
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="ghost" size="icon">
<MoreHorizontal className="h-4 w-4" />
<span className="sr-only"></span>
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuItem asChild>
<Link href={`/dashboard/admins/${admin.id}/edit`}>
<Edit className="mr-2 h-4 w-4" />
</Link>
</DropdownMenuItem>
{!isSuperAdmin(admin.id) && (
2025-04-10 16:34:02 +08:00
<DropdownMenuItem
className="text-destructive"
onClick={() => openDeleteDialog(admin)}
>
<Trash className="mr-2 h-4 w-4" />
</DropdownMenuItem>
)}
2025-04-09 09:45:06 +08:00
</DropdownMenuContent>
</DropdownMenu>
</TableCell>
</TableRow>
))
) : (
<TableRow>
<TableCell colSpan={7} className="h-24 text-center">
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
</div>
{totalCount > pageSize && (
<div className="flex justify-center gap-2 mt-4">
<Button
variant="outline"
size="sm"
onClick={() => setCurrentPage((prev) => Math.max(prev - 1, 1))}
disabled={currentPage === 1 || isLoading}
>
</Button>
<span className="py-2 px-4 text-sm">
{currentPage} / {Math.ceil(totalCount / pageSize)}
</span>
<Button
variant="outline"
size="sm"
onClick={() => setCurrentPage((prev) => prev + 1)}
disabled={currentPage >= Math.ceil(totalCount / pageSize) || isLoading}
>
</Button>
</div>
)}
2025-04-10 16:34:02 +08:00
{/* 删除确认对话框 */}
<AlertDialog open={deleteDialogOpen} onOpenChange={setDeleteDialogOpen}>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle></AlertDialogTitle>
<AlertDialogDescription>
"{adminToDelete?.username}"
2025-04-10 16:34:02 +08:00
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel disabled={isDeleting}></AlertDialogCancel>
<AlertDialogAction
onClick={confirmDelete}
disabled={isDeleting}
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
>
{isDeleting ? (
<>
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
...
</>
) : (
"确认删除"
)}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
2025-04-09 09:45:06 +08:00
</div>
)
}