超管后台 - 编辑管理员参数调整及解决React页面错误报告
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import type React from "react"
|
import type React from "react"
|
||||||
|
|
||||||
import { useState, useEffect } from "react"
|
import { useState, useEffect, use } from "react"
|
||||||
import { useRouter } from "next/navigation"
|
import { useRouter } from "next/navigation"
|
||||||
import { Button } from "@/components/ui/button"
|
import { Button } from "@/components/ui/button"
|
||||||
import { Input } from "@/components/ui/input"
|
import { Input } from "@/components/ui/input"
|
||||||
@@ -11,7 +11,7 @@ import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle }
|
|||||||
import { ArrowLeft, Loader2 } from "lucide-react"
|
import { ArrowLeft, Loader2 } from "lucide-react"
|
||||||
import Link from "next/link"
|
import Link from "next/link"
|
||||||
import { Checkbox } from "@/components/ui/checkbox"
|
import { Checkbox } from "@/components/ui/checkbox"
|
||||||
import { getAdministratorDetail, updateAdministrator } from "@/lib/admin-api"
|
import { getAdministratorDetail, updateAdministrator, AdministratorDetail } from "@/lib/admin-api"
|
||||||
import { useToast } from "@/components/ui/use-toast"
|
import { useToast } from "@/components/ui/use-toast"
|
||||||
import { getTopLevelMenus } from "@/lib/menu-api"
|
import { getTopLevelMenus } from "@/lib/menu-api"
|
||||||
import { getAdminInfo } from "@/lib/utils"
|
import { getAdminInfo } from "@/lib/utils"
|
||||||
@@ -29,11 +29,13 @@ interface MenuPermission {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default function EditAdminPage({ params }: { params: { id: string } }) {
|
export default function EditAdminPage({ params }: { params: { id: string } }) {
|
||||||
|
const { id } = use(params);
|
||||||
|
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const { toast } = useToast()
|
const { toast } = useToast()
|
||||||
const [isLoading, setIsLoading] = useState(true)
|
const [isLoading, setIsLoading] = useState(true)
|
||||||
const [isSubmitting, setIsSubmitting] = useState(false)
|
const [isSubmitting, setIsSubmitting] = useState(false)
|
||||||
const [adminInfo, setAdminInfo] = useState<any | null>(null)
|
const [adminInfo, setAdminInfo] = useState<AdministratorDetail | null>(null)
|
||||||
const [account, setAccount] = useState("")
|
const [account, setAccount] = useState("")
|
||||||
const [username, setUserName] = useState("")
|
const [username, setUserName] = useState("")
|
||||||
const [password, setPassword] = useState("")
|
const [password, setPassword] = useState("")
|
||||||
@@ -50,41 +52,30 @@ export default function EditAdminPage({ params }: { params: { id: string } }) {
|
|||||||
const fetchData = async () => {
|
const fetchData = async () => {
|
||||||
setIsLoading(true)
|
setIsLoading(true)
|
||||||
try {
|
try {
|
||||||
// 获取当前登录的管理员信息
|
|
||||||
const currentAdminInfo = getAdminInfo()
|
const currentAdminInfo = getAdminInfo()
|
||||||
setCurrentAdmin(currentAdminInfo)
|
setCurrentAdmin(currentAdminInfo)
|
||||||
|
|
||||||
// 获取管理员详情
|
const adminResponse = await getAdministratorDetail(id)
|
||||||
const adminResponse = await getAdministratorDetail(params.id)
|
|
||||||
|
|
||||||
if (adminResponse.code === 200 && adminResponse.data) {
|
if (adminResponse.code === 200 && adminResponse.data) {
|
||||||
setAdminInfo(adminResponse.data)
|
const adminData = adminResponse.data;
|
||||||
setAccount(adminResponse.data.account)
|
setAdminInfo(adminData)
|
||||||
setUserName(adminResponse.data.username)
|
setAccount(adminData.account)
|
||||||
|
setUserName(adminData.username || "")
|
||||||
|
|
||||||
// 判断是否可以编辑权限
|
const isEditingSelf = currentAdminInfo && parseInt(id) === currentAdminInfo.id
|
||||||
// 只有超级管理员(ID为1)可以编辑其他人的权限
|
|
||||||
// 编辑自己时不能修改权限
|
|
||||||
const isEditingSelf = currentAdminInfo && parseInt(params.id) === currentAdminInfo.id
|
|
||||||
const isSuperAdmin = currentAdminInfo && currentAdminInfo.id === 1
|
const isSuperAdmin = currentAdminInfo && currentAdminInfo.id === 1
|
||||||
|
|
||||||
setCanEditPermissions(!!(isSuperAdmin && !isEditingSelf))
|
setCanEditPermissions(!!(isSuperAdmin && !isEditingSelf))
|
||||||
|
|
||||||
// 如果可以编辑权限,则获取菜单权限
|
|
||||||
if (isSuperAdmin && !isEditingSelf) {
|
if (isSuperAdmin && !isEditingSelf) {
|
||||||
const menuResponse = await getTopLevelMenus()
|
const menuResponse = await getTopLevelMenus()
|
||||||
if (menuResponse.code === 200 && menuResponse.data) {
|
if (menuResponse.code === 200 && menuResponse.data) {
|
||||||
setMenuPermissions(menuResponse.data)
|
setMenuPermissions(menuResponse.data)
|
||||||
|
|
||||||
// 获取管理员已有的权限
|
if (adminData.permissions) {
|
||||||
const permissionsResponse = await getAdministratorDetail(params.id)
|
const permissionIds = adminData.permissions.map(Number);
|
||||||
if (permissionsResponse.code === 200 && permissionsResponse.data) {
|
setSelectedPermissions(permissionIds);
|
||||||
// 如果有权限数据,则设置选中的权限
|
|
||||||
if (permissionsResponse.data.permissions) {
|
|
||||||
// 处理权限ID数组,确保是数字类型
|
|
||||||
const permissionIds = permissionsResponse.data.permissions.map(Number);
|
|
||||||
setSelectedPermissions(permissionIds);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -108,20 +99,17 @@ export default function EditAdminPage({ params }: { params: { id: string } }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fetchData()
|
fetchData()
|
||||||
}, [params.id])
|
}, [id])
|
||||||
|
|
||||||
// 切换权限选择
|
|
||||||
const togglePermission = (permissionId: number) => {
|
const togglePermission = (permissionId: number) => {
|
||||||
setSelectedPermissions((prev) =>
|
setSelectedPermissions((prev) =>
|
||||||
prev.includes(permissionId) ? prev.filter((id) => id !== permissionId) : [...prev, permissionId],
|
prev.includes(permissionId) ? prev.filter((id) => id !== permissionId) : [...prev, permissionId],
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 提交表单
|
|
||||||
const handleSubmit = async (e: React.FormEvent) => {
|
const handleSubmit = async (e: React.FormEvent) => {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
|
|
||||||
// 验证密码
|
|
||||||
if (password && password !== confirmPassword) {
|
if (password && password !== confirmPassword) {
|
||||||
setErrorMessage("两次输入的密码不一致")
|
setErrorMessage("两次输入的密码不一致")
|
||||||
setErrorDialogOpen(true)
|
setErrorDialogOpen(true)
|
||||||
@@ -131,24 +119,20 @@ export default function EditAdminPage({ params }: { params: { id: string } }) {
|
|||||||
setIsSubmitting(true)
|
setIsSubmitting(true)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 准备提交的数据
|
|
||||||
const updateData: any = {
|
const updateData: any = {
|
||||||
account,
|
account: account,
|
||||||
username,
|
username: username,
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果有设置密码,则添加密码字段
|
|
||||||
if (password) {
|
if (password) {
|
||||||
updateData.password = password
|
updateData.password = password
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果可以编辑权限,则添加权限字段
|
|
||||||
if (canEditPermissions) {
|
if (canEditPermissions) {
|
||||||
updateData.permissionIds = selectedPermissions
|
updateData.permissionIds = selectedPermissions
|
||||||
}
|
}
|
||||||
|
|
||||||
// 调用更新API
|
const response = await updateAdministrator(id, updateData)
|
||||||
const response = await updateAdministrator(params.id, updateData)
|
|
||||||
|
|
||||||
if (response.code === 200) {
|
if (response.code === 200) {
|
||||||
toast({
|
toast({
|
||||||
@@ -156,8 +140,6 @@ export default function EditAdminPage({ params }: { params: { id: string } }) {
|
|||||||
description: "管理员信息已更新",
|
description: "管理员信息已更新",
|
||||||
variant: "success",
|
variant: "success",
|
||||||
})
|
})
|
||||||
|
|
||||||
// 更新成功后返回列表页
|
|
||||||
router.push("/dashboard/admins")
|
router.push("/dashboard/admins")
|
||||||
} else {
|
} else {
|
||||||
setErrorMessage(response.msg || "更新失败,请稍后重试")
|
setErrorMessage(response.msg || "更新失败,请稍后重试")
|
||||||
@@ -222,7 +204,7 @@ export default function EditAdminPage({ params }: { params: { id: string } }) {
|
|||||||
id="account"
|
id="account"
|
||||||
value={account}
|
value={account}
|
||||||
onChange={(e) => setAccount(e.target.value)}
|
onChange={(e) => setAccount(e.target.value)}
|
||||||
placeholder="只能用数字或者字母或者数字字母组合"
|
placeholder="请输入账号"
|
||||||
required
|
required
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -166,7 +166,7 @@ export default function NewAdminPage() {
|
|||||||
id="account"
|
id="account"
|
||||||
value={account}
|
value={account}
|
||||||
onChange={(e) => setAccount(e.target.value)}
|
onChange={(e) => setAccount(e.target.value)}
|
||||||
placeholder="请输入账号"
|
placeholder="只能用数字或者字母或者数字字母组合"
|
||||||
required
|
required
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ interface Device {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default function EditProjectPage({ params }: { params: { id: string } }) {
|
export default function EditProjectPage({ params }: { params: { id: string } }) {
|
||||||
|
const id = params.id
|
||||||
|
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const [isSubmitting, setIsSubmitting] = useState(false)
|
const [isSubmitting, setIsSubmitting] = useState(false)
|
||||||
const [isLoading, setIsLoading] = useState(true)
|
const [isLoading, setIsLoading] = useState(true)
|
||||||
@@ -34,7 +36,7 @@ export default function EditProjectPage({ params }: { params: { id: string } })
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchProjectDetail = async () => {
|
const fetchProjectDetail = async () => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`http://yishi.com/company/detail/${params.id}`)
|
const response = await fetch(`http://yishi.com/company/detail/${id}`)
|
||||||
const data = await response.json()
|
const data = await response.json()
|
||||||
|
|
||||||
if (data.code === 200) {
|
if (data.code === 200) {
|
||||||
@@ -56,7 +58,7 @@ export default function EditProjectPage({ params }: { params: { id: string } })
|
|||||||
}
|
}
|
||||||
|
|
||||||
fetchProjectDetail()
|
fetchProjectDetail()
|
||||||
}, [params.id])
|
}, [id])
|
||||||
|
|
||||||
const handleSubmit = async (e: React.FormEvent) => {
|
const handleSubmit = async (e: React.FormEvent) => {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
@@ -75,7 +77,7 @@ export default function EditProjectPage({ params }: { params: { id: string } })
|
|||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
id: params.id,
|
id: id,
|
||||||
name: projectName,
|
name: projectName,
|
||||||
account,
|
account,
|
||||||
memo: description,
|
memo: description,
|
||||||
@@ -102,7 +104,7 @@ export default function EditProjectPage({ params }: { params: { id: string } })
|
|||||||
}
|
}
|
||||||
|
|
||||||
const handleAddDevice = () => {
|
const handleAddDevice = () => {
|
||||||
router.push(`/dashboard/projects/${params.id}/devices/new`)
|
router.push(`/dashboard/projects/${id}/devices/new`)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isLoading) {
|
if (isLoading) {
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { apiRequest, ApiResponse } from './api-utils';
|
|||||||
export interface Administrator {
|
export interface Administrator {
|
||||||
id: number;
|
id: number;
|
||||||
username: string;
|
username: string;
|
||||||
name: string;
|
account: string;
|
||||||
role: string;
|
role: string;
|
||||||
status: number;
|
status: number;
|
||||||
createdAt: string;
|
createdAt: string;
|
||||||
@@ -16,7 +16,7 @@ export interface Administrator {
|
|||||||
export interface AdministratorDetail {
|
export interface AdministratorDetail {
|
||||||
id: number;
|
id: number;
|
||||||
username: string;
|
username: string;
|
||||||
name: string;
|
account: string;
|
||||||
authId: number;
|
authId: number;
|
||||||
roleName: string;
|
roleName: string;
|
||||||
status: number;
|
status: number;
|
||||||
@@ -96,7 +96,7 @@ export async function updateAdministrator(
|
|||||||
id: number | string,
|
id: number | string,
|
||||||
data: {
|
data: {
|
||||||
username: string;
|
username: string;
|
||||||
name: string;
|
account: string;
|
||||||
password?: string;
|
password?: string;
|
||||||
permissionIds?: number[];
|
permissionIds?: number[];
|
||||||
}
|
}
|
||||||
@@ -115,7 +115,7 @@ export async function updateAdministrator(
|
|||||||
export async function addAdministrator(
|
export async function addAdministrator(
|
||||||
data: {
|
data: {
|
||||||
username: string;
|
username: string;
|
||||||
name: string;
|
account: string;
|
||||||
password: string;
|
password: string;
|
||||||
permissionIds?: number[];
|
permissionIds?: number[];
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user