更新客户管理的修改功能参数

This commit is contained in:
Eamon
2026-03-28 18:04:19 +08:00
parent 8c26d81846
commit 3387056ea9
6 changed files with 440 additions and 164 deletions

View File

@@ -2,11 +2,10 @@
import { zodResolver } from "@hookform/resolvers/zod"
import { format } from "date-fns"
import { Suspense, useCallback, useRef, useState } from "react"
import { Suspense, useCallback, useState } from "react"
import { Controller, useForm } from "react-hook-form"
import { toast } from "sonner"
import { z } from "zod"
import { getPageCusts, updateCust } from "@/actions/cust"
import { getPageCusts } from "@/actions/cust"
import { DataTable, useDataTable } from "@/components/data-table"
import { Badge } from "@/components/ui/badge"
import { Button } from "@/components/ui/button"
@@ -26,6 +25,7 @@ import {
} from "@/components/ui/select"
import type { Cust } from "@/models/cust"
import { AddUserDialog } from "./create"
import { UpdateDialog } from "./update"
type FilterValues = {
account?: string
@@ -64,14 +64,9 @@ type FormValues = z.infer<typeof filterSchema>
export default function UserPage() {
const [filters, setFilters] = useState<FilterValues>({})
const [editingRowId, setEditingRowId] = useState<number | null>(null)
const [editPhone, setEditPhone] = useState("")
const [editEmail, setEditEmail] = useState("")
const [isSaving, setIsSaving] = useState(false)
const [isAddDialogOpen, setIsAddDialogOpen] = useState(false)
const editingRowRef = useRef<Cust | null>(null)
const [isEditDialogOpen, setIsEditDialogOpen] = useState(false)
const [currentEditUser, setCurrentEditUser] = useState<Cust | null>(null)
const { control, handleSubmit, reset } = useForm<FormValues>({
resolver: zodResolver(filterSchema),
defaultValues: {
@@ -90,6 +85,7 @@ export default function UserPage() {
)
const table = useDataTable<Cust>(fetchUsers)
console.log(table, "客户管理table")
const onFilter = handleSubmit(data => {
const result: FilterValues = {}
@@ -107,58 +103,6 @@ export default function UserPage() {
table.refresh()
}, [table])
const startEdit = (row: Cust) => {
setEditingRowId(row.id)
setEditPhone(row.phone || "")
setEditEmail(row.email || "")
editingRowRef.current = row
}
const cancelEdit = () => {
setEditingRowId(null)
setEditPhone("")
setEditEmail("")
editingRowRef.current = null
}
const saveEdit = async (row: Cust) => {
const phoneRegex = /^1[3-9]\d{9}$/
if (editPhone && !phoneRegex.test(editPhone)) {
toast.error("请输入正确的手机号格式")
return false
}
const emailRegex = /^[^\s@]+@([^\s@]+\.)+[^\s@]+$/
if (editEmail && !emailRegex.test(editEmail)) {
toast.error("请输入正确的邮箱格式")
return false
}
setIsSaving(true)
try {
const result = await updateCust({
id: row.id,
phone: editPhone,
email: editEmail,
})
if (result.success) {
toast.success("更新成功")
refreshTable()
cancelEdit()
return true
} else {
toast.error(result.message || "更新失败")
return false
}
} catch (error) {
toast.error("更新失败,请稍后重试")
console.error(error)
return false
} finally {
setIsSaving(false)
}
}
const handleAddUserSuccess = () => {
refreshTable()
}
@@ -301,77 +245,8 @@ export default function UserPage() {
columns={[
{ header: "ID", accessorKey: "id" },
{ header: "账号", accessorKey: "username" },
{
header: "手机",
accessorKey: "phone",
cell: ({ row }) => {
const isEditing = editingRowId === row.original.id
if (isEditing) {
return (
<Input
value={editPhone}
onChange={e => setEditPhone(e.target.value)}
onBlur={() => {
if (editingRowRef.current) {
saveEdit(editingRowRef.current)
}
}}
onKeyDown={e => {
if (e.key === "Enter") {
e.preventDefault()
if (editingRowRef.current) {
saveEdit(editingRowRef.current)
}
}
if (e.key === "Escape") {
e.preventDefault()
cancelEdit()
}
}}
placeholder="手机号"
className="w-32"
autoFocus
/>
)
}
return row.original.phone || "-"
},
},
{
header: "邮箱",
accessorKey: "email",
cell: ({ row }) => {
const isEditing = editingRowId === row.original.id
if (isEditing) {
return (
<Input
value={editEmail}
onChange={e => setEditEmail(e.target.value)}
onBlur={() => {
if (editingRowRef.current) {
saveEdit(editingRowRef.current)
}
}}
onKeyDown={e => {
if (e.key === "Enter") {
e.preventDefault()
if (editingRowRef.current) {
saveEdit(editingRowRef.current)
}
}
if (e.key === "Escape") {
e.preventDefault()
cancelEdit()
}
}}
placeholder="邮箱"
className="w-40"
/>
)
}
return row.original.email || "-"
},
},
{ header: "手机", accessorKey: "phone" },
{ header: "邮箱", accessorKey: "email" },
{ header: "姓名", accessorKey: "name" },
{
header: "客户来源",
@@ -432,7 +307,22 @@ export default function UserPage() {
accessorKey: "status",
cell: ({ row }) => (row.original.status === 1 ? "正常" : "禁用"),
},
{ header: "联系方式", accessorKey: "contact_wechat" },
{
header: "联系方式",
cell: ({ row }) => {
const qq = row.original.contact_qq || ""
const wechat = row.original.contact_wechat || ""
const hasQQ = qq.trim() !== ""
const hasWechat = wechat.trim() !== ""
if (!hasQQ && !hasWechat) return null
return (
<div className="space-y-1">
{hasWechat && <div>{wechat}</div>}
{hasQQ && <div>QQ{qq}</div>}
</div>
)
},
},
{ header: "客户经理", accessorKey: "admin.name" },
{
header: "最后登录时间",
@@ -461,30 +351,14 @@ export default function UserPage() {
meta: { pin: "right" },
header: "操作",
cell: ({ row }) => {
const isEditing = editingRowId === row.original.id
if (isEditing) {
return (
<div className="flex gap-2">
<Button
size="sm"
onClick={() => saveEdit(row.original)}
disabled={isSaving}
>
</Button>
<Button
size="sm"
variant="outline"
onClick={cancelEdit}
disabled={isSaving}
>
</Button>
</div>
)
}
return (
<Button size="sm" onClick={() => startEdit(row.original)}>
<Button
size="sm"
onClick={() => {
setCurrentEditUser(row.original)
setIsEditDialogOpen(true)
}}
>
</Button>
)
@@ -499,6 +373,13 @@ export default function UserPage() {
onOpenChange={setIsAddDialogOpen}
onSuccess={handleAddUserSuccess}
/>
<UpdateDialog
open={isEditDialogOpen}
onOpenChange={setIsEditDialogOpen}
currentUser={currentEditUser}
onSuccess={refreshTable}
/>
</div>
)
}