2025-04-09 09:45:06 +08:00
|
|
|
|
"use client"
|
|
|
|
|
|
|
2025-04-15 17:41:52 +08:00
|
|
|
|
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"
|
2025-04-15 17:41:52 +08:00
|
|
|
|
import { Plus, Search, MoreHorizontal, Edit, Eye, Trash, ChevronLeft, ChevronRight } from "lucide-react"
|
|
|
|
|
|
import { toast } from "sonner"
|
2025-04-21 11:46:54 +08:00
|
|
|
|
import {
|
|
|
|
|
|
Dialog,
|
|
|
|
|
|
DialogContent,
|
|
|
|
|
|
DialogDescription,
|
|
|
|
|
|
DialogFooter,
|
|
|
|
|
|
DialogHeader,
|
|
|
|
|
|
DialogTitle,
|
|
|
|
|
|
} from "@/components/ui/dialog"
|
2025-04-09 09:45:06 +08:00
|
|
|
|
|
2025-04-15 17:41:52 +08:00
|
|
|
|
interface Project {
|
|
|
|
|
|
id: number
|
|
|
|
|
|
name: string
|
|
|
|
|
|
status: number
|
|
|
|
|
|
tenantId: number
|
|
|
|
|
|
companyId: number
|
|
|
|
|
|
memo: string | null
|
|
|
|
|
|
userCount: number
|
|
|
|
|
|
createTime: string
|
|
|
|
|
|
}
|
2025-04-09 09:45:06 +08:00
|
|
|
|
|
|
|
|
|
|
export default function ProjectsPage() {
|
|
|
|
|
|
const [searchTerm, setSearchTerm] = useState("")
|
2025-04-15 17:41:52 +08:00
|
|
|
|
const [projects, setProjects] = useState<Project[]>([])
|
|
|
|
|
|
const [isLoading, setIsLoading] = useState(true)
|
|
|
|
|
|
const [currentPage, setCurrentPage] = useState(1)
|
|
|
|
|
|
const [totalPages, setTotalPages] = useState(1)
|
|
|
|
|
|
const [pageSize] = useState(10)
|
2025-04-21 11:46:54 +08:00
|
|
|
|
const [deleteDialogOpen, setDeleteDialogOpen] = useState(false)
|
|
|
|
|
|
const [deletingProjectId, setDeletingProjectId] = useState<number | null>(null)
|
|
|
|
|
|
const [isDeleting, setIsDeleting] = useState(false)
|
2025-04-09 09:45:06 +08:00
|
|
|
|
|
2025-04-15 17:41:52 +08:00
|
|
|
|
// 获取项目列表
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
const fetchProjects = async () => {
|
|
|
|
|
|
setIsLoading(true)
|
|
|
|
|
|
try {
|
|
|
|
|
|
const response = await fetch(`http://yishi.com/company/list?page=${currentPage}&limit=${pageSize}`)
|
|
|
|
|
|
const data = await response.json()
|
|
|
|
|
|
|
|
|
|
|
|
if (data.code === 200) {
|
|
|
|
|
|
setProjects(data.data.list)
|
|
|
|
|
|
setTotalPages(Math.ceil(data.data.total / pageSize))
|
|
|
|
|
|
} else {
|
|
|
|
|
|
toast.error(data.msg || "获取项目列表失败")
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
toast.error("获取项目列表失败")
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
setIsLoading(false)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fetchProjects()
|
|
|
|
|
|
}, [currentPage, pageSize])
|
|
|
|
|
|
|
|
|
|
|
|
// 切换页码
|
|
|
|
|
|
const handlePageChange = (page: number) => {
|
|
|
|
|
|
if (page >= 1 && page <= totalPages) {
|
|
|
|
|
|
setCurrentPage(page)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-04-09 09:45:06 +08:00
|
|
|
|
|
2025-04-21 11:46:54 +08:00
|
|
|
|
const handleDeleteClick = (projectId: number) => {
|
|
|
|
|
|
setDeletingProjectId(projectId)
|
|
|
|
|
|
setDeleteDialogOpen(true)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const handleConfirmDelete = async () => {
|
|
|
|
|
|
if (!deletingProjectId) return
|
|
|
|
|
|
|
|
|
|
|
|
setIsDeleting(true)
|
|
|
|
|
|
try {
|
|
|
|
|
|
const response = await fetch("http://yishi.com/company/delete", {
|
|
|
|
|
|
method: "POST",
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
|
},
|
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
|
id: deletingProjectId
|
|
|
|
|
|
}),
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
const data = await response.json()
|
|
|
|
|
|
|
|
|
|
|
|
if (data.code === 200) {
|
|
|
|
|
|
toast.success("删除成功")
|
|
|
|
|
|
// 刷新项目列表
|
|
|
|
|
|
window.location.reload()
|
|
|
|
|
|
} else {
|
|
|
|
|
|
toast.error(data.msg || "删除失败")
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
toast.error("网络错误,请稍后重试")
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
setIsDeleting(false)
|
|
|
|
|
|
setDeleteDialogOpen(false)
|
|
|
|
|
|
setDeletingProjectId(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/projects/new">
|
|
|
|
|
|
<Plus 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"
|
2025-04-15 17:41:52 +08:00
|
|
|
|
placeholder="搜索项目名称..."
|
2025-04-09 09:45:06 +08:00
|
|
|
|
className="pl-8"
|
|
|
|
|
|
value={searchTerm}
|
|
|
|
|
|
onChange={(e) => setSearchTerm(e.target.value)}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<div className="rounded-md border">
|
|
|
|
|
|
<Table>
|
|
|
|
|
|
<TableHeader>
|
|
|
|
|
|
<TableRow>
|
|
|
|
|
|
<TableHead>项目名称</TableHead>
|
2025-04-15 17:41:52 +08:00
|
|
|
|
<TableHead>状态</TableHead>
|
|
|
|
|
|
<TableHead className="text-center">用户数量</TableHead>
|
|
|
|
|
|
<TableHead className="text-center">创建时间</TableHead>
|
2025-04-09 09:45:06 +08:00
|
|
|
|
<TableHead className="text-right">操作</TableHead>
|
|
|
|
|
|
</TableRow>
|
|
|
|
|
|
</TableHeader>
|
|
|
|
|
|
<TableBody>
|
2025-04-15 17:41:52 +08:00
|
|
|
|
{isLoading ? (
|
|
|
|
|
|
<TableRow>
|
|
|
|
|
|
<TableCell colSpan={5} className="h-24 text-center">
|
|
|
|
|
|
加载中...
|
|
|
|
|
|
</TableCell>
|
|
|
|
|
|
</TableRow>
|
|
|
|
|
|
) : projects.length > 0 ? (
|
|
|
|
|
|
projects.map((project) => (
|
2025-04-09 09:45:06 +08:00
|
|
|
|
<TableRow key={project.id}>
|
|
|
|
|
|
<TableCell className="font-medium">{project.name}</TableCell>
|
2025-04-15 17:41:52 +08:00
|
|
|
|
<TableCell>{project.status === 1 ? '启用' : '禁用'}</TableCell>
|
|
|
|
|
|
<TableCell className="text-center">{project.userCount}</TableCell>
|
|
|
|
|
|
<TableCell className="text-center">{project.createTime}</TableCell>
|
2025-04-09 09:45:06 +08:00
|
|
|
|
<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/projects/${project.id}`}>
|
|
|
|
|
|
<Eye className="mr-2 h-4 w-4" /> 查看详情
|
|
|
|
|
|
</Link>
|
|
|
|
|
|
</DropdownMenuItem>
|
|
|
|
|
|
<DropdownMenuItem asChild>
|
|
|
|
|
|
<Link href={`/dashboard/projects/${project.id}/edit`}>
|
|
|
|
|
|
<Edit className="mr-2 h-4 w-4" /> 编辑项目
|
|
|
|
|
|
</Link>
|
|
|
|
|
|
</DropdownMenuItem>
|
2025-04-21 11:46:54 +08:00
|
|
|
|
<DropdownMenuItem
|
|
|
|
|
|
className="text-red-600"
|
|
|
|
|
|
onClick={() => handleDeleteClick(project.id)}
|
|
|
|
|
|
>
|
2025-04-09 09:45:06 +08:00
|
|
|
|
<Trash className="mr-2 h-4 w-4" /> 删除项目
|
|
|
|
|
|
</DropdownMenuItem>
|
|
|
|
|
|
</DropdownMenuContent>
|
|
|
|
|
|
</DropdownMenu>
|
|
|
|
|
|
</TableCell>
|
|
|
|
|
|
</TableRow>
|
|
|
|
|
|
))
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<TableRow>
|
2025-04-15 17:41:52 +08:00
|
|
|
|
<TableCell colSpan={5} className="h-24 text-center">
|
2025-04-09 09:45:06 +08:00
|
|
|
|
未找到项目
|
|
|
|
|
|
</TableCell>
|
|
|
|
|
|
</TableRow>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</TableBody>
|
|
|
|
|
|
</Table>
|
|
|
|
|
|
</div>
|
2025-04-15 17:41:52 +08:00
|
|
|
|
|
|
|
|
|
|
{/* 分页控件 */}
|
|
|
|
|
|
{!isLoading && projects.length > 0 && (
|
|
|
|
|
|
<div className="flex items-center justify-center gap-2">
|
|
|
|
|
|
<Button
|
|
|
|
|
|
variant="outline"
|
|
|
|
|
|
size="icon"
|
|
|
|
|
|
onClick={() => handlePageChange(currentPage - 1)}
|
|
|
|
|
|
disabled={currentPage === 1}
|
|
|
|
|
|
>
|
|
|
|
|
|
<ChevronLeft className="h-4 w-4" />
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
|
|
|
|
|
|
{Array.from({ length: totalPages }, (_, i) => i + 1).map((page) => (
|
|
|
|
|
|
<Button
|
|
|
|
|
|
key={page}
|
|
|
|
|
|
variant={page === currentPage ? "default" : "outline"}
|
|
|
|
|
|
size="icon"
|
|
|
|
|
|
onClick={() => handlePageChange(page)}
|
|
|
|
|
|
>
|
|
|
|
|
|
{page}
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
))}
|
|
|
|
|
|
|
|
|
|
|
|
<Button
|
|
|
|
|
|
variant="outline"
|
|
|
|
|
|
size="icon"
|
|
|
|
|
|
onClick={() => handlePageChange(currentPage + 1)}
|
|
|
|
|
|
disabled={currentPage === totalPages}
|
|
|
|
|
|
>
|
|
|
|
|
|
<ChevronRight className="h-4 w-4" />
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
2025-04-21 11:46:54 +08:00
|
|
|
|
|
|
|
|
|
|
{/* 删除确认对话框 */}
|
|
|
|
|
|
<Dialog open={deleteDialogOpen} onOpenChange={setDeleteDialogOpen}>
|
|
|
|
|
|
<DialogContent>
|
|
|
|
|
|
<DialogHeader>
|
|
|
|
|
|
<DialogTitle>确认删除</DialogTitle>
|
|
|
|
|
|
<DialogDescription>
|
|
|
|
|
|
删除项目将会删除本项目关联的所有账号,项目删除后不可恢复,是否确认删除?
|
|
|
|
|
|
</DialogDescription>
|
|
|
|
|
|
</DialogHeader>
|
|
|
|
|
|
<DialogFooter>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
variant="outline"
|
|
|
|
|
|
onClick={() => setDeleteDialogOpen(false)}
|
|
|
|
|
|
disabled={isDeleting}
|
|
|
|
|
|
>
|
|
|
|
|
|
取消
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
variant="destructive"
|
|
|
|
|
|
onClick={handleConfirmDelete}
|
|
|
|
|
|
disabled={isDeleting}
|
|
|
|
|
|
>
|
|
|
|
|
|
{isDeleting ? "删除中..." : "确认删除"}
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</DialogFooter>
|
|
|
|
|
|
</DialogContent>
|
|
|
|
|
|
</Dialog>
|
2025-04-09 09:45:06 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|
|
|
|
|
|
|