修改客户管理认领和管理员字段展示逻辑

This commit is contained in:
Eamon
2026-01-09 18:36:08 +08:00
parent c85293fd1d
commit 887ff2f07c
5 changed files with 31 additions and 12 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name": "lanhu-admin", "name": "lanhu-admin",
"version": "1.0.1", "version": "1.0.2",
"private": true, "private": true,
"scripts": { "scripts": {
"dev": "next dev -H 0.0.0.0 --turbopack", "dev": "next dev -H 0.0.0.0 --turbopack",

View File

@@ -10,7 +10,7 @@ import type { User } from "@/models/user"
export default function UserPage() { export default function UserPage() {
const table = useDataTable<User>((page, size) => getPageUsers({ page, size })) const table = useDataTable<User>((page, size) => getPageUsers({ page, size }))
const bind = useFetch((id: number) => bindAdmin({ id }), { const bind = useFetch(table, (id: number) => bindAdmin({ id }), {
done: "用户已认领", done: "用户已认领",
fail: "用户认领失败", fail: "用户认领失败",
}) })
@@ -73,7 +73,10 @@ export default function UserPage() {
}, },
}, },
{ header: "联系方式", accessorKey: "contact_wechat" }, { header: "联系方式", accessorKey: "contact_wechat" },
{ header: "管理员", accessorKey: "admin_id" }, {
header: "管理员",
cell: ({ row }) => row.original.admin?.name,
},
{ {
header: "最后登录时间", header: "最后登录时间",
accessorKey: "last_login", accessorKey: "last_login",
@@ -92,9 +95,9 @@ export default function UserPage() {
<Button <Button
size={"sm"} size={"sm"}
onClick={() => bind(ctx.row.original.id)} onClick={() => bind(ctx.row.original.id)}
disabled={ctx.row.original.admin_id !== null} disabled={!!ctx.row.original.admin_id}
> >
{ctx.row.original.admin_id !== null ? "已认领" : "认领"} {ctx.row.original.admin_id ? "已认领" : "认领"}
</Button> </Button>
), ),
}, },

View File

@@ -51,8 +51,9 @@ export function useDataTable<T>(
}, [refresh, page, size]) }, [refresh, page, size])
return { return {
status,
data, data,
status,
setStatus,
pagination: { pagination: {
page, page,
size, size,
@@ -60,5 +61,6 @@ export function useDataTable<T>(
onPageChange, onPageChange,
onSizeChange, onSizeChange,
}, },
refresh,
} }
} }

View File

@@ -5,6 +5,7 @@ import {
useState, useState,
} from "react" } from "react"
import { toast } from "sonner" import { toast } from "sonner"
import type { useDataTable } from "@/components/data-table"
import type { ApiResponse } from "@/lib/api" import type { ApiResponse } from "@/lib/api"
export function useStatus() { export function useStatus() {
@@ -12,31 +13,39 @@ export function useStatus() {
} }
export function useFetch<TArgs extends unknown[], TResult>( export function useFetch<TArgs extends unknown[], TResult>(
table: ReturnType<typeof useDataTable>,
fetchData: (...args: TArgs) => Promise<ApiResponse<TResult>>, fetchData: (...args: TArgs) => Promise<ApiResponse<TResult>>,
messages: { messages: {
done?: string done?: string
fail?: string fail?: string
}, },
setStatus?: Dispatch<SetStateAction<"load" | "fail" | "done">>,
) { ) {
return useCallback( return useCallback(
async (...args: TArgs) => { async (...args: TArgs) => {
try { try {
setStatus?.("load") table.setStatus?.("load")
const resp = await fetchData(...args) const resp = await fetchData(...args)
if (!resp.success) { if (!resp.success) {
throw new Error(resp.message) throw new Error(resp.message)
} }
setStatus?.("done") table.setStatus?.("done")
table.refresh(table.pagination.page, table.pagination.size)
toast.success(messages.done || "获取数据成功") toast.success(messages.done || "获取数据成功")
} catch (e) { } catch (e) {
setStatus?.("fail") table.setStatus?.("fail")
toast.error(messages.fail || "获取数据失败", { toast.error(messages.fail || "获取数据失败", {
description: (e as Error).message || "未知错误", description: (e as Error).message || "未知错误",
}) })
} }
}, },
[fetchData, setStatus, messages], [
fetchData,
table.setStatus,
table.pagination.page,
table.pagination.size,
table.refresh,
messages,
],
) )
} }

View File

@@ -1,6 +1,7 @@
export type User = { export type User = {
id: number id: number
admin_id: number admin_id?: number
admin?: Admin
phone: string phone: string
has_password: boolean has_password: boolean
username: string username: string
@@ -20,3 +21,7 @@ export type User = {
created_at: Date created_at: Date
updated_at: Date updated_at: Date
} }
export type Admin = {
name: string
}