Compare commits
5 Commits
f6ae0a9463
...
ed95f0520d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ed95f0520d | ||
|
|
790180a847 | ||
|
|
cc7e26561d | ||
|
|
8fcf54ae10 | ||
|
|
ff645aaaca |
32
src/actions/balance.ts
Normal file
32
src/actions/balance.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import type { PageRecord } from "@/lib/api"
|
||||
import type { Balance } from "@/models/balance"
|
||||
import { callByUser } from "./base"
|
||||
|
||||
export async function getPageBalance(params: {
|
||||
page: number
|
||||
size: number
|
||||
user_phone?: string
|
||||
bill_id?: string
|
||||
created_at_start?: Date
|
||||
created_at_end?: Date
|
||||
}) {
|
||||
return callByUser<PageRecord<Balance>>(
|
||||
"/api/admin/balance-activity/page",
|
||||
params,
|
||||
)
|
||||
}
|
||||
|
||||
export async function getBalance(params: {
|
||||
page: number
|
||||
size: number
|
||||
user_id: number
|
||||
user_phone?: string
|
||||
bill_id?: string
|
||||
created_at_start?: Date
|
||||
created_at_end?: Date
|
||||
}) {
|
||||
return callByUser<PageRecord<Balance>>(
|
||||
"/api/admin/balance-activity/page/of-user",
|
||||
params,
|
||||
)
|
||||
}
|
||||
@@ -16,3 +16,19 @@ export async function getPageBatch(params: {
|
||||
}) {
|
||||
return callByUser<PageRecord<Batch>>("/api/admin/batch/page", params)
|
||||
}
|
||||
|
||||
export async function getBatch(params: {
|
||||
page: number
|
||||
size: number
|
||||
user_id: number
|
||||
user_phone?: string
|
||||
resource_no?: string
|
||||
batch_no?: string
|
||||
prov?: string
|
||||
city?: string
|
||||
isp?: string
|
||||
created_at_start?: Date
|
||||
created_at_end?: Date
|
||||
}) {
|
||||
return callByUser<PageRecord<Batch>>("/api/admin/batch/page/of-user", params)
|
||||
}
|
||||
|
||||
@@ -26,3 +26,19 @@ export async function getSkuList(params: { product_code?: ProductCode }) {
|
||||
}
|
||||
return callByUser<ProductSku[]>("/api/admin/product/sku/all", requestParams)
|
||||
}
|
||||
|
||||
export async function getBill(params: {
|
||||
page: number
|
||||
size: number
|
||||
user_id: number
|
||||
bill_no?: string
|
||||
user_phone?: string
|
||||
trade_inner_no?: string
|
||||
resource_no?: string
|
||||
sku_code?: string
|
||||
product_code?: string
|
||||
created_at_start?: Date
|
||||
created_at_end?: Date
|
||||
}) {
|
||||
return callByUser<PageRecord<Billing>>("/api/admin/bill/page/of-user", params)
|
||||
}
|
||||
|
||||
@@ -16,3 +16,21 @@ export async function getPageChannel(params: {
|
||||
}) {
|
||||
return callByUser<PageRecord<Channel>>("/api/admin/channel/page", params)
|
||||
}
|
||||
export async function getChannel(params: {
|
||||
page: number
|
||||
size: number
|
||||
user_id: number
|
||||
batch_no?: string
|
||||
user_phone?: string
|
||||
resource_no?: string
|
||||
proxy_port?: number
|
||||
proxy_host?: string
|
||||
node_ip?: string
|
||||
expired_at_start?: Date
|
||||
expired_at_end?: Date
|
||||
}) {
|
||||
return callByUser<PageRecord<Channel>>(
|
||||
"/api/admin/channel/page/of-user",
|
||||
params,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import type { PageRecord } from "@/lib/api"
|
||||
import type { Cust } from "@/models/cust"
|
||||
import type { User } from "@/models/user"
|
||||
import { callByUser } from "./base"
|
||||
|
||||
export async function getPageCusts(params: { page: number; size: number }) {
|
||||
return callByUser<PageRecord<Cust>>("/api/admin/user/page", params)
|
||||
return callByUser<PageRecord<User>>("/api/admin/user/page", params)
|
||||
}
|
||||
export async function updateCust(data: {
|
||||
id: number
|
||||
@@ -16,7 +16,7 @@ export async function updateCust(data: {
|
||||
contact_qq?: string
|
||||
contact_wechat?: string
|
||||
}) {
|
||||
return callByUser<PageRecord<Cust>>("/api/admin/user/update", data)
|
||||
return callByUser<PageRecord<User>>("/api/admin/user/update", data)
|
||||
}
|
||||
|
||||
export async function createCust(data: {
|
||||
@@ -32,9 +32,22 @@ export async function createCust(data: {
|
||||
contact_qq?: string
|
||||
contact_wechat?: string
|
||||
}) {
|
||||
return callByUser<PageRecord<Cust>>("/api/admin/user/create", data)
|
||||
return callByUser<PageRecord<User>>("/api/admin/user/create", data)
|
||||
}
|
||||
|
||||
export async function getBalance(params: { user_id: number; balance: string }) {
|
||||
return callByUser<PageRecord<Cust>>("/api/admin/user/update/balance", params)
|
||||
export async function getDeposit(params: { user_id: number; amount: string }) {
|
||||
return callByUser<PageRecord<User>>(
|
||||
"/api/admin/user/update/balance-inc",
|
||||
params,
|
||||
)
|
||||
}
|
||||
|
||||
export async function getDeduction(params: {
|
||||
user_id: number
|
||||
amount: string
|
||||
}) {
|
||||
return callByUser<PageRecord<User>>(
|
||||
"/api/admin/user/update/balance-dec",
|
||||
params,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ export async function createProductSku(data: {
|
||||
name: string
|
||||
price: string
|
||||
discount_id?: number
|
||||
price_min?: string
|
||||
}) {
|
||||
return callByUser<ProductSku>("/api/admin/product/sku/create", {
|
||||
product_id: data.product_id,
|
||||
@@ -33,6 +34,7 @@ export async function createProductSku(data: {
|
||||
name: data.name,
|
||||
price: data.price,
|
||||
discount_id: data.discount_id,
|
||||
price_min: data.price_min,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -42,6 +44,7 @@ export async function updateProductSku(data: {
|
||||
name?: string
|
||||
price?: string
|
||||
discount_id?: number | null
|
||||
price_min?: string
|
||||
}) {
|
||||
return callByUser<ProductSku>("/api/admin/product/sku/update", {
|
||||
id: data.id,
|
||||
@@ -49,6 +52,7 @@ export async function updateProductSku(data: {
|
||||
name: data.name,
|
||||
price: data.price,
|
||||
discount_id: data.discount_id,
|
||||
price_min: data.price_min,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -65,3 +69,6 @@ export async function batchUpdateProductSkuDiscount(data: {
|
||||
discount_id: data.discount_id,
|
||||
})
|
||||
}
|
||||
export async function activeProductSku(data: { id: number; status: number }) {
|
||||
return callByUser<ProductSku>("/api/admin/product/sku/update/status", data)
|
||||
}
|
||||
|
||||
@@ -31,3 +31,39 @@ export async function listResourceShort(params: ResourceListParams) {
|
||||
export async function updateResource(data: { id: number; active?: boolean }) {
|
||||
return callByUser<Resources>("/api/admin/resource/update", data)
|
||||
}
|
||||
|
||||
export async function ResourceLong(params: {
|
||||
page: number
|
||||
size: number
|
||||
user_id: number
|
||||
user_phone?: string
|
||||
resource_no?: string
|
||||
active?: boolean
|
||||
mode?: number
|
||||
created_at_start?: Date
|
||||
created_at_end?: Date
|
||||
expired?: boolean
|
||||
}) {
|
||||
return callByUser<PageRecord<Resources>>(
|
||||
"/api/admin/resource/long/page/of-user",
|
||||
params,
|
||||
)
|
||||
}
|
||||
|
||||
export async function ResourceShort(params: {
|
||||
page: number
|
||||
size: number
|
||||
user_id: number
|
||||
user_phone?: string
|
||||
resource_no?: string
|
||||
active?: boolean
|
||||
mode?: number
|
||||
created_at_start?: Date
|
||||
created_at_end?: Date
|
||||
expired?: boolean
|
||||
}) {
|
||||
return callByUser<PageRecord<Resources>>(
|
||||
"/api/admin/resource/short/page/of-user",
|
||||
params,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -15,3 +15,25 @@ export async function getPageTrade(params: {
|
||||
}) {
|
||||
return callByUser<PageRecord<Trade>>("/api/admin/trade/page", params)
|
||||
}
|
||||
|
||||
export async function getTrade(params: {
|
||||
page: number
|
||||
size: number
|
||||
user_id: number
|
||||
user_phone?: string
|
||||
inner_no?: string
|
||||
method?: number
|
||||
platform?: number
|
||||
status?: number
|
||||
created_at_start?: Date
|
||||
created_at_end?: Date
|
||||
}) {
|
||||
return callByUser<PageRecord<Trade>>("/api/admin/trade/page/of-user", params)
|
||||
}
|
||||
export async function getTradeComplete(params: {
|
||||
user_id: number
|
||||
trade_no: string
|
||||
method: number
|
||||
}) {
|
||||
return callByUser<PageRecord<Trade>>("/api/admin/trade/complete", params)
|
||||
}
|
||||
|
||||
@@ -6,6 +6,10 @@ export async function getPageUsers(params: { page: number; size: number }) {
|
||||
return callByUser<PageRecord<User>>("/api/admin/user/page", params)
|
||||
}
|
||||
|
||||
export async function getPageUserPage(params: { page: number; size: number }) {
|
||||
return callByUser<PageRecord<User>>("/api/admin/user/page/not-bind", params)
|
||||
}
|
||||
|
||||
export async function bindAdmin(params: {
|
||||
id: number
|
||||
account?: string
|
||||
@@ -14,11 +18,11 @@ export async function bindAdmin(params: {
|
||||
enabled?: boolean
|
||||
assigned?: boolean
|
||||
}) {
|
||||
return callByUser("/api/admin/user/bind", {
|
||||
return callByUser("/api/admin/user/update/bind", {
|
||||
user_id: params.id,
|
||||
})
|
||||
}
|
||||
|
||||
export async function getPageUser(params: object) {
|
||||
export async function getPageUser(params: { phone?: string; name?: string }) {
|
||||
return callByUser<User>("/api/admin/user/get", params)
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
"use client"
|
||||
import { Lock } from "lucide-react"
|
||||
import { Suspense, useState } from "react"
|
||||
import { toast } from "sonner"
|
||||
import { deleteAdmin, getPageAdmin, updateAdmin } from "@/actions/admin"
|
||||
@@ -21,6 +22,11 @@ import {
|
||||
HoverCardContent,
|
||||
HoverCardTrigger,
|
||||
} from "@/components/ui/hover-card"
|
||||
import {
|
||||
Tooltip,
|
||||
TooltipContent,
|
||||
TooltipTrigger,
|
||||
} from "@/components/ui/tooltip"
|
||||
import { type Admin, AdminStatus } from "@/models/admin"
|
||||
import type { Role } from "@/models/role"
|
||||
import { AssignRoles } from "./assign-roles"
|
||||
@@ -29,6 +35,7 @@ import { UpdateAdmin } from "./update"
|
||||
|
||||
export default function AdminPage() {
|
||||
const table = useDataTable((page, size) => getPageAdmin({ page, size }))
|
||||
console.log(table, "table")
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-3">
|
||||
@@ -81,20 +88,43 @@ export default function AdminPage() {
|
||||
id: "action",
|
||||
meta: { pin: "right" },
|
||||
header: "操作",
|
||||
cell: ({ row }) => (
|
||||
<div className="flex gap-2">
|
||||
<UpdateAdmin admin={row.original} onSuccess={table.refresh} />
|
||||
<AssignRoles admin={row.original} onSuccess={table.refresh} />
|
||||
<ToggleStatusButton
|
||||
admin={row.original}
|
||||
onSuccess={table.refresh}
|
||||
/>
|
||||
<DeleteButton
|
||||
admin={row.original}
|
||||
onSuccess={table.refresh}
|
||||
/>
|
||||
</div>
|
||||
),
|
||||
cell: ({ row }) => {
|
||||
const admin = row.original
|
||||
if (admin.lock === true) {
|
||||
return (
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<div className="flex justify-center items-center">
|
||||
<Lock className="h-4 w-4 text-muted-foreground" />
|
||||
</div>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent>
|
||||
<p>该账号为系统账号,不可修改</p>
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
)
|
||||
}
|
||||
return (
|
||||
<div className="flex gap-2">
|
||||
<UpdateAdmin
|
||||
admin={row.original}
|
||||
onSuccess={table.refresh}
|
||||
/>
|
||||
<AssignRoles
|
||||
admin={row.original}
|
||||
onSuccess={table.refresh}
|
||||
/>
|
||||
<ToggleStatusButton
|
||||
admin={row.original}
|
||||
onSuccess={table.refresh}
|
||||
/>
|
||||
<DeleteButton
|
||||
admin={row.original}
|
||||
onSuccess={table.refresh}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
},
|
||||
},
|
||||
]}
|
||||
/>
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
"use client"
|
||||
import {
|
||||
BadgeQuestionMarkIcon,
|
||||
BellIcon,
|
||||
ChevronDownIcon,
|
||||
ChevronRightIcon,
|
||||
InboxIcon,
|
||||
LogOutIcon,
|
||||
SearchIcon,
|
||||
SettingsIcon,
|
||||
UserIcon,
|
||||
} from "lucide-react"
|
||||
@@ -16,36 +13,12 @@ import { usePathname, useRouter } from "next/navigation"
|
||||
import { useEffect, useRef, useState } from "react"
|
||||
import { logout } from "@/actions/auth"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import { Input } from "@/components/ui/input"
|
||||
import type { Admin } from "@/models/admin"
|
||||
|
||||
export default function Appbar(props: { admin: Admin }) {
|
||||
const router = useRouter()
|
||||
const [showDropdown, setShowDropdown] = useState(false)
|
||||
const [showNotifications, setShowNotifications] = useState(false)
|
||||
const [notifications] = useState([
|
||||
{
|
||||
id: 1,
|
||||
title: "系统通知",
|
||||
content: "您有新的待审核内容",
|
||||
time: "10分钟前",
|
||||
read: false,
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
title: "安全提醒",
|
||||
content: "您的账号于昨天登录了新设备",
|
||||
time: "1小时前",
|
||||
read: true,
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
title: "系统更新",
|
||||
content: "系统将在今晚进行例行维护",
|
||||
time: "2小时前",
|
||||
read: true,
|
||||
},
|
||||
])
|
||||
|
||||
const pathname = usePathname()
|
||||
const dropdownRef = useRef<HTMLDivElement>(null)
|
||||
@@ -72,14 +45,15 @@ export default function Appbar(props: { admin: Admin }) {
|
||||
return () => document.removeEventListener("mousedown", handleClickOutside)
|
||||
}, [])
|
||||
|
||||
// 根据路径生成面包屑
|
||||
const generateBreadcrumbs = () => {
|
||||
const paths = pathname.split("/").filter(Boolean)
|
||||
|
||||
const hiddenSegments = ["client"]
|
||||
const filteredPaths = paths.filter(path => !hiddenSegments.includes(path))
|
||||
const breadcrumbs = [
|
||||
{ path: "/", label: "首页" },
|
||||
...paths.map((path, index) => {
|
||||
const url = `/${paths.slice(0, index + 1).join("/")}`
|
||||
...filteredPaths.map((path, index) => {
|
||||
const originalIndex = paths.findIndex(p => p === path)
|
||||
const url = `/${paths.slice(0, originalIndex + 1).join("/")}`
|
||||
const label = getBreadcrumbLabel(path)
|
||||
return { path: url, label }
|
||||
}),
|
||||
@@ -94,7 +68,7 @@ export default function Appbar(props: { admin: Admin }) {
|
||||
content: "内容管理",
|
||||
articles: "文章管理",
|
||||
media: "媒体库",
|
||||
user: "用户管理",
|
||||
user: "客户认领",
|
||||
roles: "角色权限",
|
||||
settings: "系统设置",
|
||||
logs: "系统日志",
|
||||
@@ -113,13 +87,13 @@ export default function Appbar(props: { admin: Admin }) {
|
||||
permissions: "权限列表",
|
||||
discount: "折扣管理",
|
||||
statistics: "数据统计",
|
||||
balance: "余额明细",
|
||||
}
|
||||
|
||||
return labels[path] || path
|
||||
}
|
||||
|
||||
const breadcrumbs = generateBreadcrumbs()
|
||||
const unreadCount = notifications.filter(n => !n.read).length
|
||||
const doLogout = async () => {
|
||||
const resp = await logout()
|
||||
if (resp.success) {
|
||||
@@ -153,88 +127,6 @@ export default function Appbar(props: { admin: Admin }) {
|
||||
|
||||
{/* 右侧用户信息和工具栏 */}
|
||||
<div className="flex items-center space-x-4">
|
||||
{/* 搜索框 */}
|
||||
<div className="hidden md:block relative">
|
||||
<div className="relative">
|
||||
<SearchIcon className="absolute left-3 top-1/2 transform -translate-y-1/2 h-4 w-4 text-gray-400" />
|
||||
<Input
|
||||
type="text"
|
||||
placeholder="搜索..."
|
||||
className="pl-10 pr-4 py-2 bg-gray-100 border border-gray-200 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent w-56"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 通知图标 */}
|
||||
<div className="relative" ref={notificationRef}>
|
||||
<Button
|
||||
onClick={() => setShowNotifications(!showNotifications)}
|
||||
className="relative p-2 rounded-full text-gray-600 bg-gray-100 hover:bg-gray-100 hover:text-gray-800 transition-colors"
|
||||
aria-label="通知"
|
||||
>
|
||||
<BellIcon />
|
||||
{unreadCount > 0 && (
|
||||
<span className="absolute top-1 right-1 h-4 w-4 text-xs flex items-center justify-center rounded-full bg-red-500 text-white">
|
||||
{unreadCount}
|
||||
</span>
|
||||
)}
|
||||
</Button>
|
||||
|
||||
{/* 通知下拉面板 */}
|
||||
{showNotifications && (
|
||||
<div className="absolute right-0 mt-2 w-80 bg-white rounded-md shadow-lg py-1 z-20 border border-gray-200">
|
||||
<div className="px-4 py-2 border-b border-gray-100 flex justify-between items-center">
|
||||
<h3 className="font-medium text-gray-800">通知</h3>
|
||||
<Button className="text-xs text-blue-600 hover:text-blue-800">
|
||||
全部标为已读
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<div className="max-h-72 overflow-y-auto">
|
||||
{notifications.length > 0 ? (
|
||||
notifications.map(notification => (
|
||||
<div
|
||||
key={notification.id}
|
||||
className={`px-4 py-3 border-b border-gray-100 hover:bg-gray-50 ${
|
||||
notification.read ? "bg-white" : "bg-blue-50"
|
||||
}`}
|
||||
>
|
||||
<div className="flex justify-between items-start">
|
||||
<h4 className="text-sm font-medium text-gray-800">
|
||||
{notification.title}
|
||||
</h4>
|
||||
<span className="text-xs text-gray-500">
|
||||
{notification.time}
|
||||
</span>
|
||||
</div>
|
||||
<p className="text-xs text-gray-600 mt-1">
|
||||
{notification.content}
|
||||
</p>
|
||||
</div>
|
||||
))
|
||||
) : (
|
||||
<div className="py-8 px-4 text-center">
|
||||
<InboxIcon size={18} />
|
||||
<p className="mt-2 text-sm text-gray-500">暂无通知</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="border-t border-gray-100 p-2 text-center">
|
||||
<Link
|
||||
href="/notifications"
|
||||
className="text-xs text-blue-600 hover:text-blue-800"
|
||||
>
|
||||
查看全部通知
|
||||
</Link>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* 分隔线 */}
|
||||
<div className="hidden md:block h-8 w-px bg-gray-200"></div>
|
||||
|
||||
{/* 用户下拉菜单 */}
|
||||
<div className="relative" ref={dropdownRef}>
|
||||
<Button
|
||||
|
||||
231
src/app/(root)/balance/page.tsx
Normal file
231
src/app/(root)/balance/page.tsx
Normal file
@@ -0,0 +1,231 @@
|
||||
"use client"
|
||||
import { zodResolver } from "@hookform/resolvers/zod"
|
||||
import { format } from "date-fns"
|
||||
import { Suspense, useCallback, useState } from "react"
|
||||
import { Controller, useForm } from "react-hook-form"
|
||||
import z from "zod"
|
||||
import { getPageBalance } from "@/actions/balance"
|
||||
import { DataTable, useDataTable } from "@/components/data-table"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import {
|
||||
Field,
|
||||
FieldError,
|
||||
FieldGroup,
|
||||
FieldLabel,
|
||||
} from "@/components/ui/field"
|
||||
import { Input } from "@/components/ui/input"
|
||||
|
||||
import type { Balance } from "@/models/balance"
|
||||
|
||||
type FilterValues = {
|
||||
user_phone?: string
|
||||
bill_id?: string
|
||||
admin_id?: string
|
||||
created_at_start?: Date
|
||||
created_at_end?: Date
|
||||
}
|
||||
|
||||
const filterSchema = z
|
||||
.object({
|
||||
phone: z.string().optional(),
|
||||
bill_id: z.string().optional(),
|
||||
admin_id: z.string().optional(),
|
||||
created_at_start: z.string().optional(),
|
||||
created_at_end: z.string().optional(),
|
||||
})
|
||||
.superRefine((data, ctx) => {
|
||||
if (data.created_at_start && data.created_at_end) {
|
||||
const start = new Date(data.created_at_start)
|
||||
const end = new Date(data.created_at_end)
|
||||
|
||||
if (end < start) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: "结束时间不能早于开始时间",
|
||||
path: ["created_at_end"],
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
type FormValues = z.infer<typeof filterSchema>
|
||||
|
||||
export default function BalancePage() {
|
||||
const [filters, setFilters] = useState<FilterValues>({})
|
||||
const { control, handleSubmit, reset } = useForm<FormValues>({
|
||||
resolver: zodResolver(filterSchema),
|
||||
defaultValues: {
|
||||
phone: "",
|
||||
bill_id: "",
|
||||
admin_id: "",
|
||||
created_at_start: "",
|
||||
created_at_end: "",
|
||||
},
|
||||
})
|
||||
|
||||
const fetchUsers = useCallback(
|
||||
(page: number, size: number) => getPageBalance({ page, size, ...filters }),
|
||||
[filters],
|
||||
)
|
||||
|
||||
const table = useDataTable<Balance>(fetchUsers)
|
||||
|
||||
console.log(table, "table")
|
||||
const onFilter = handleSubmit(data => {
|
||||
const result: FilterValues = {}
|
||||
if (data.phone) result.user_phone = data.phone
|
||||
if (data.bill_id) result.bill_id = data.bill_id
|
||||
if (data.created_at_start)
|
||||
result.created_at_start = new Date(data.created_at_start)
|
||||
if (data.created_at_end)
|
||||
result.created_at_end = new Date(data.created_at_end)
|
||||
setFilters(result)
|
||||
table.pagination.onPageChange(1)
|
||||
})
|
||||
|
||||
return (
|
||||
<div className="space-y-3">
|
||||
<form onSubmit={onFilter} className="bg-white p-4 rounded-lg">
|
||||
<div className="flex flex-wrap items-end gap-4">
|
||||
<Controller
|
||||
name="phone"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-40 flex-none"
|
||||
>
|
||||
<FieldLabel>会员号</FieldLabel>
|
||||
<Input {...field} placeholder="请输入会员号" />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="bill_id"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-40 flex-none"
|
||||
>
|
||||
<FieldLabel>账单号</FieldLabel>
|
||||
<Input {...field} placeholder="请输入账单号" />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="created_at_start"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-40 flex-none"
|
||||
>
|
||||
<FieldLabel>开始时间</FieldLabel>
|
||||
<Input type="date" {...field} />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="created_at_end"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-40 flex-none"
|
||||
>
|
||||
<FieldLabel>结束时间</FieldLabel>
|
||||
<Input type="date" {...field} />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<FieldGroup className="flex-row justify-start mt-4 gap-2">
|
||||
<Button type="submit">筛选</Button>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
onClick={() => {
|
||||
reset()
|
||||
setFilters({})
|
||||
table.pagination.onPageChange(1)
|
||||
}}
|
||||
>
|
||||
重置
|
||||
</Button>
|
||||
</FieldGroup>
|
||||
</form>
|
||||
|
||||
<Suspense>
|
||||
<DataTable<Balance>
|
||||
{...table}
|
||||
columns={[
|
||||
{
|
||||
header: "会员号",
|
||||
accessorFn: row => row.user?.phone || "",
|
||||
},
|
||||
{
|
||||
header: "账单号",
|
||||
accessorFn: row => row.bill?.bill_no || "",
|
||||
},
|
||||
{
|
||||
header: "管理员",
|
||||
accessorKey: "admin_id",
|
||||
accessorFn: row => row.admin?.name || "",
|
||||
},
|
||||
{
|
||||
header: "变动金额",
|
||||
accessorKey: "amount",
|
||||
cell: ({ row }) => {
|
||||
const amount = row.original.amount
|
||||
const isPositive = Number(amount) > 0
|
||||
return (
|
||||
<div className="flex items-center gap-1">
|
||||
<span
|
||||
className={`font-semibold ${
|
||||
isPositive ? "text-green-600" : "text-red-600"
|
||||
}`}
|
||||
>
|
||||
{isPositive ? "+" : ""}
|
||||
{Number(amount).toFixed(2)}
|
||||
</span>
|
||||
</div>
|
||||
)
|
||||
},
|
||||
},
|
||||
{
|
||||
header: "余额变化",
|
||||
accessorKey: "balance_prev",
|
||||
cell: ({ row }) => (
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="text-muted-foreground text-sm">
|
||||
¥{Number(row.original.balance_prev).toFixed(2)}
|
||||
</span>
|
||||
<span className="text-muted-foreground">→</span>
|
||||
<span className="font-medium text-foreground">
|
||||
¥{Number(row.original.balance_curr).toFixed(2)}
|
||||
</span>
|
||||
</div>
|
||||
),
|
||||
},
|
||||
{
|
||||
header: "备注",
|
||||
accessorKey: "remark",
|
||||
},
|
||||
{
|
||||
header: "创建时间",
|
||||
accessorKey: "created_at",
|
||||
cell: ({ row }) =>
|
||||
format(new Date(row.original.created_at), "yyyy-MM-dd HH:mm"),
|
||||
},
|
||||
]}
|
||||
/>
|
||||
</Suspense>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -24,7 +24,7 @@ import {
|
||||
import type { Batch } from "@/models/batch"
|
||||
|
||||
type APIFilterParams = {
|
||||
phone?: string
|
||||
user_phone?: string
|
||||
batch_no?: string
|
||||
resource_no?: string
|
||||
prov?: string
|
||||
@@ -84,7 +84,7 @@ export default function BatchPage() {
|
||||
|
||||
const onFilter = handleSubmit(data => {
|
||||
const result: APIFilterParams = {}
|
||||
if (data.user_phone?.trim()) result.phone = data.user_phone.trim()
|
||||
if (data.user_phone?.trim()) result.user_phone = data.user_phone.trim()
|
||||
if (data.batch_no?.trim()) result.batch_no = data.batch_no.trim()
|
||||
if (data.resource_no?.trim()) result.resource_no = data.resource_no.trim()
|
||||
if (data.prov?.trim()) result.prov = data.prov.trim()
|
||||
@@ -252,7 +252,7 @@ export default function BatchPage() {
|
||||
columns={[
|
||||
{
|
||||
header: "会员号",
|
||||
accessorFn: row => row.user?.phone || "-",
|
||||
accessorFn: row => row.user?.phone || "",
|
||||
},
|
||||
{ header: "套餐号", accessorKey: "resource.resource_no" },
|
||||
{ header: "批次号", accessorKey: "batch_no" },
|
||||
|
||||
@@ -181,6 +181,20 @@ export default function BillingPage() {
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="bill_no"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-40 flex-none"
|
||||
>
|
||||
<FieldLabel>账单号</FieldLabel>
|
||||
<Input {...field} placeholder="请输入账单号" />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="inner_no"
|
||||
control={control}
|
||||
@@ -262,20 +276,6 @@ export default function BillingPage() {
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="bill_no"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-40 flex-none"
|
||||
>
|
||||
<FieldLabel>账单号</FieldLabel>
|
||||
<Input {...field} placeholder="请输入账单号" />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="created_at_start"
|
||||
control={control}
|
||||
@@ -328,7 +328,7 @@ export default function BillingPage() {
|
||||
<DataTable<Billing>
|
||||
{...table}
|
||||
columns={[
|
||||
{ header: "会员号", accessorFn: row => row.user?.phone || "-" },
|
||||
{ header: "会员号", accessorFn: row => row.user?.phone || "" },
|
||||
{ header: "套餐号", accessorKey: "resource.resource_no" },
|
||||
{
|
||||
header: "账单详情",
|
||||
|
||||
224
src/app/(root)/client/balance/page.tsx
Normal file
224
src/app/(root)/client/balance/page.tsx
Normal file
@@ -0,0 +1,224 @@
|
||||
"use client"
|
||||
import { zodResolver } from "@hookform/resolvers/zod"
|
||||
import { format } from "date-fns"
|
||||
import { useRouter, useSearchParams } from "next/navigation"
|
||||
import { Suspense, useState } from "react"
|
||||
import { Controller, useForm } from "react-hook-form"
|
||||
import z from "zod"
|
||||
import { getBalance } from "@/actions/balance"
|
||||
import { DataTable, useDataTable } from "@/components/data-table"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import {
|
||||
Field,
|
||||
FieldError,
|
||||
FieldGroup,
|
||||
FieldLabel,
|
||||
} from "@/components/ui/field"
|
||||
import { Input } from "@/components/ui/input"
|
||||
import type { Balance } from "@/models/balance"
|
||||
|
||||
type FilterValues = {
|
||||
user_phone?: string
|
||||
bill_id?: string
|
||||
created_at_start?: Date
|
||||
created_at_end?: Date
|
||||
}
|
||||
|
||||
const filterSchema = z
|
||||
.object({
|
||||
phone: z.string().optional(),
|
||||
bill_id: z.string().optional(),
|
||||
admin_id: z.string().optional(),
|
||||
created_at_start: z.string().optional(),
|
||||
created_at_end: z.string().optional(),
|
||||
})
|
||||
.superRefine((data, ctx) => {
|
||||
if (data.created_at_start && data.created_at_end) {
|
||||
const start = new Date(data.created_at_start)
|
||||
const end = new Date(data.created_at_end)
|
||||
|
||||
if (end < start) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: "结束时间不能早于开始时间",
|
||||
path: ["created_at_end"],
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
type FormValues = z.infer<typeof filterSchema>
|
||||
|
||||
export default function BalancePage() {
|
||||
const searchParams = useSearchParams()
|
||||
const router = useRouter()
|
||||
const userId = searchParams.get("userId")
|
||||
const userPhone = searchParams.get("phone")
|
||||
console.log(userPhone, "userPhone")
|
||||
|
||||
const [filters, setFilters] = useState<FilterValues>({})
|
||||
const { control, handleSubmit, reset } = useForm<FormValues>({
|
||||
resolver: zodResolver(filterSchema),
|
||||
defaultValues: {
|
||||
phone: "",
|
||||
bill_id: "",
|
||||
admin_id: "",
|
||||
created_at_start: "",
|
||||
created_at_end: "",
|
||||
},
|
||||
})
|
||||
|
||||
const table = useDataTable<Balance>((page, size) =>
|
||||
getBalance({ page, size, user_id: Number(userId), ...filters }),
|
||||
)
|
||||
console.log(table, "仅用户的table")
|
||||
|
||||
const onFilter = handleSubmit(data => {
|
||||
const result: FilterValues = {}
|
||||
if (data.phone) result.user_phone = data.phone
|
||||
if (data.bill_id) result.bill_id = data.bill_id
|
||||
if (data.created_at_start)
|
||||
result.created_at_start = new Date(data.created_at_start)
|
||||
if (data.created_at_end)
|
||||
result.created_at_end = new Date(data.created_at_end)
|
||||
setFilters(result)
|
||||
table.pagination.onPageChange(1)
|
||||
})
|
||||
|
||||
return (
|
||||
<div className="space-y-3">
|
||||
<Button
|
||||
onClick={() => {
|
||||
router.back()
|
||||
}}
|
||||
className="gap-2"
|
||||
>
|
||||
返回上一级
|
||||
</Button>
|
||||
<span className="ml-2 text-gray-600">用户会员号: {userPhone}</span>
|
||||
<form onSubmit={onFilter} className="bg-white p-4 rounded-lg">
|
||||
<div className="flex flex-wrap items-end gap-4">
|
||||
<Controller
|
||||
name="bill_id"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-40 flex-none"
|
||||
>
|
||||
<FieldLabel>账单号</FieldLabel>
|
||||
<Input {...field} placeholder="请输入账单号" />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="created_at_start"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-40 flex-none"
|
||||
>
|
||||
<FieldLabel>开始时间</FieldLabel>
|
||||
<Input type="date" {...field} />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="created_at_end"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-40 flex-none"
|
||||
>
|
||||
<FieldLabel>结束时间</FieldLabel>
|
||||
<Input type="date" {...field} />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<FieldGroup className="flex-row justify-start mt-4 gap-2">
|
||||
<Button type="submit">筛选</Button>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
onClick={() => {
|
||||
reset()
|
||||
setFilters({})
|
||||
table.pagination.onPageChange(1)
|
||||
}}
|
||||
>
|
||||
重置
|
||||
</Button>
|
||||
</FieldGroup>
|
||||
</form>
|
||||
|
||||
<Suspense>
|
||||
<DataTable<Balance>
|
||||
{...table}
|
||||
columns={[
|
||||
{
|
||||
header: "账单号",
|
||||
accessorFn: row => row.bill?.bill_no || "",
|
||||
},
|
||||
{
|
||||
header: "管理员",
|
||||
accessorKey: "admin_id",
|
||||
accessorFn: row => row.admin?.name || "",
|
||||
},
|
||||
{
|
||||
header: "变动金额",
|
||||
accessorKey: "amount",
|
||||
cell: ({ row }) => {
|
||||
const amount = row.original.amount
|
||||
const isPositive = Number(amount) > 0
|
||||
return (
|
||||
<div className="flex items-center gap-1">
|
||||
<span
|
||||
className={`font-semibold ${
|
||||
isPositive ? "text-green-600" : "text-red-600"
|
||||
}`}
|
||||
>
|
||||
{isPositive ? "+" : ""}
|
||||
{Number(amount).toFixed(2)}
|
||||
</span>
|
||||
</div>
|
||||
)
|
||||
},
|
||||
},
|
||||
{
|
||||
header: "余额变化",
|
||||
accessorKey: "balance_prev",
|
||||
cell: ({ row }) => (
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="text-muted-foreground text-sm">
|
||||
¥{Number(row.original.balance_prev).toFixed(2)}
|
||||
</span>
|
||||
<span className="text-muted-foreground">→</span>
|
||||
<span className="font-medium text-foreground">
|
||||
¥{Number(row.original.balance_curr).toFixed(2)}
|
||||
</span>
|
||||
</div>
|
||||
),
|
||||
},
|
||||
{
|
||||
header: "备注",
|
||||
accessorKey: "remark",
|
||||
},
|
||||
{
|
||||
header: "创建时间",
|
||||
accessorKey: "created_at",
|
||||
cell: ({ row }) =>
|
||||
format(new Date(row.original.created_at), "yyyy-MM-dd HH:mm"),
|
||||
},
|
||||
]}
|
||||
/>
|
||||
</Suspense>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
258
src/app/(root)/client/batch/page.tsx
Normal file
258
src/app/(root)/client/batch/page.tsx
Normal file
@@ -0,0 +1,258 @@
|
||||
"use client"
|
||||
import { zodResolver } from "@hookform/resolvers/zod"
|
||||
import { format } from "date-fns"
|
||||
import { useSearchParams } from "next/navigation"
|
||||
import { Suspense, useState } from "react"
|
||||
import { Controller, useForm } from "react-hook-form"
|
||||
import { z } from "zod"
|
||||
import { getBatch } from "@/actions/batch"
|
||||
import { DataTable, useDataTable } from "@/components/data-table"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import {
|
||||
Field,
|
||||
FieldError,
|
||||
FieldGroup,
|
||||
FieldLabel,
|
||||
} from "@/components/ui/field"
|
||||
import { Input } from "@/components/ui/input"
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from "@/components/ui/select"
|
||||
import type { Batch } from "@/models/batch"
|
||||
|
||||
type APIFilterParams = {
|
||||
phone?: string
|
||||
batch_no?: string
|
||||
resource_no?: string
|
||||
prov?: string
|
||||
city?: string
|
||||
isp?: string
|
||||
created_at_start?: Date
|
||||
created_at_end?: Date
|
||||
}
|
||||
|
||||
const filterSchema = z
|
||||
.object({
|
||||
user_phone: z.string().optional(),
|
||||
resource_no: z.string().optional(),
|
||||
batch_no: z.string().optional(),
|
||||
prov: z.string().optional(),
|
||||
city: z.string().optional(),
|
||||
isp: z.string().optional(),
|
||||
created_at_start: z.string().optional(),
|
||||
created_at_end: z.string().optional(),
|
||||
})
|
||||
.superRefine((data, ctx) => {
|
||||
if (data.created_at_start && data.created_at_end) {
|
||||
const start = new Date(data.created_at_start)
|
||||
const end = new Date(data.created_at_end)
|
||||
|
||||
if (end < start) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: "结束时间不能早于开始时间",
|
||||
path: ["created_at_end"],
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
type FilterSchema = z.infer<typeof filterSchema>
|
||||
|
||||
export default function BatchPage() {
|
||||
const searchParams = useSearchParams()
|
||||
const userId = searchParams.get("userId")
|
||||
const [filters, setFilters] = useState<APIFilterParams>({})
|
||||
|
||||
const { control, handleSubmit, reset } = useForm<FilterSchema>({
|
||||
resolver: zodResolver(filterSchema),
|
||||
defaultValues: {
|
||||
resource_no: "",
|
||||
batch_no: "",
|
||||
prov: "",
|
||||
city: "",
|
||||
isp: "all",
|
||||
created_at_start: "",
|
||||
created_at_end: "",
|
||||
},
|
||||
})
|
||||
|
||||
const table = useDataTable<Batch>((page, size) =>
|
||||
getBatch({ page, size, user_id: Number(userId), ...filters }),
|
||||
)
|
||||
|
||||
const onFilter = handleSubmit(data => {
|
||||
const result: APIFilterParams = {}
|
||||
if (data.user_phone?.trim()) result.phone = data.user_phone.trim()
|
||||
if (data.batch_no?.trim()) result.batch_no = data.batch_no.trim()
|
||||
if (data.resource_no?.trim()) result.resource_no = data.resource_no.trim()
|
||||
if (data.prov?.trim()) result.prov = data.prov.trim()
|
||||
if (data.city?.trim()) result.city = data.city.trim()
|
||||
if (data.isp && data.isp !== "all") result.isp = data.isp
|
||||
if (data.created_at_start)
|
||||
result.created_at_start = new Date(data.created_at_start)
|
||||
if (data.created_at_end)
|
||||
result.created_at_end = new Date(data.created_at_end)
|
||||
|
||||
setFilters(result)
|
||||
table.pagination.onPageChange(1)
|
||||
})
|
||||
|
||||
return (
|
||||
<div className="space-y-3">
|
||||
{/* 筛选表单 */}
|
||||
<form onSubmit={onFilter} className="bg-white p-4 rounded-lg">
|
||||
<div className="flex flex-wrap items-end gap-4">
|
||||
<Controller
|
||||
name="batch_no"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-40 flex-none"
|
||||
>
|
||||
<FieldLabel>批次号</FieldLabel>
|
||||
<Input {...field} placeholder="请输入批次号" />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="resource_no"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-40 flex-none"
|
||||
>
|
||||
<FieldLabel>套餐号</FieldLabel>
|
||||
<Input {...field} placeholder="请输入套餐号" />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="prov"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-32 flex-none"
|
||||
>
|
||||
<FieldLabel>省份</FieldLabel>
|
||||
<Input {...field} placeholder="请输入省份" />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="city"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-32 flex-none"
|
||||
>
|
||||
<FieldLabel>城市</FieldLabel>
|
||||
<Input {...field} placeholder="请输入城市" />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="isp"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field data-invalid={fieldState.invalid} className="w-32">
|
||||
<FieldLabel>运营商</FieldLabel>
|
||||
<Select value={field.value} onValueChange={field.onChange}>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder="全部" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="all">全部</SelectItem>
|
||||
<SelectItem value="1">电信</SelectItem>
|
||||
<SelectItem value="2">联通</SelectItem>
|
||||
<SelectItem value="3">移动</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="created_at_start"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-40 flex-none"
|
||||
>
|
||||
<FieldLabel>开始时间</FieldLabel>
|
||||
<Input type="date" {...field} />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="created_at_end"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-40 flex-none"
|
||||
>
|
||||
<FieldLabel>结束时间</FieldLabel>
|
||||
<Input type="date" {...field} />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
<FieldGroup className="flex-row justify-start mt-4 gap-2">
|
||||
<Button type="submit">筛选</Button>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
onClick={() => {
|
||||
reset()
|
||||
setFilters({})
|
||||
table.pagination.onPageChange(1)
|
||||
}}
|
||||
>
|
||||
重置
|
||||
</Button>
|
||||
</FieldGroup>
|
||||
</form>
|
||||
|
||||
<Suspense fallback={<div>Loading...</div>}>
|
||||
<DataTable<Batch>
|
||||
{...table}
|
||||
columns={[
|
||||
{
|
||||
header: "会员号",
|
||||
accessorFn: row => row.user?.phone || "",
|
||||
},
|
||||
{ header: "套餐号", accessorKey: "resource.resource_no" },
|
||||
{ header: "批次号", accessorKey: "batch_no" },
|
||||
{ header: "省份", accessorKey: "prov" },
|
||||
{ header: "城市", accessorKey: "city" },
|
||||
{ header: "提取IP", accessorKey: "ip" },
|
||||
{ header: "运营商", accessorKey: "isp" },
|
||||
{ header: "提取数量", accessorKey: "count" },
|
||||
{
|
||||
header: "提取时间",
|
||||
accessorKey: "time",
|
||||
cell: ({ row }) =>
|
||||
format(new Date(row.original.time), "yyyy-MM-dd HH:mm"),
|
||||
},
|
||||
]}
|
||||
/>
|
||||
</Suspense>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
447
src/app/(root)/client/billing/page.tsx
Normal file
447
src/app/(root)/client/billing/page.tsx
Normal file
@@ -0,0 +1,447 @@
|
||||
"use client"
|
||||
import { zodResolver } from "@hookform/resolvers/zod"
|
||||
import { format } from "date-fns"
|
||||
import { CreditCard, Wallet } from "lucide-react"
|
||||
import { useSearchParams } from "next/navigation"
|
||||
import { Suspense, useEffect, useState } from "react"
|
||||
import { Controller, useForm } from "react-hook-form"
|
||||
import { toast } from "sonner"
|
||||
import { z } from "zod"
|
||||
import { getBill, getSkuList } from "@/actions/bill"
|
||||
import { DataTable, useDataTable } from "@/components/data-table"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import {
|
||||
Field,
|
||||
FieldError,
|
||||
FieldGroup,
|
||||
FieldLabel,
|
||||
} from "@/components/ui/field"
|
||||
import { Input } from "@/components/ui/input"
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from "@/components/ui/select"
|
||||
import { ProductCode } from "@/lib/base"
|
||||
import type { Billing } from "@/models/billing"
|
||||
|
||||
type FilterValues = {
|
||||
bill_no?: string
|
||||
user_phone?: string
|
||||
trade_inner_no?: string
|
||||
resource_no?: string
|
||||
sku_code?: string
|
||||
product_code?: string
|
||||
created_at_start?: Date
|
||||
created_at_end?: Date
|
||||
}
|
||||
|
||||
type SkuOption = {
|
||||
resource_code: string
|
||||
resource_name: string
|
||||
}
|
||||
|
||||
const filterSchema = z
|
||||
.object({
|
||||
phone: z
|
||||
.string()
|
||||
.optional()
|
||||
.transform(val => val?.trim()),
|
||||
bill_no: z
|
||||
.string()
|
||||
.optional()
|
||||
.transform(val => val?.trim()),
|
||||
resource_no: z.string().optional(),
|
||||
inner_no: z.string().optional(),
|
||||
created_at_start: z.string().optional(),
|
||||
created_at_end: z.string().optional(),
|
||||
product_code: z.string().optional(),
|
||||
sku_code: z.string().optional(),
|
||||
})
|
||||
.superRefine((data, ctx) => {
|
||||
if (data.created_at_start && data.created_at_end) {
|
||||
const start = new Date(data.created_at_start)
|
||||
const end = new Date(data.created_at_end)
|
||||
if (end < start) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: "结束时间不能早于开始时间",
|
||||
path: ["created_at_end"],
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
type FilterSchema = z.infer<typeof filterSchema>
|
||||
|
||||
export default function BillingPage() {
|
||||
const searchParams = useSearchParams()
|
||||
const userId = searchParams.get("userId")
|
||||
const [filters, setFilters] = useState<FilterValues>({})
|
||||
const [skuOptions, setSkuOptions] = useState<SkuOption[]>([])
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [skuProductCode, setSkuProductCode] = useState<ProductCode>(
|
||||
ProductCode.All,
|
||||
)
|
||||
const { control, handleSubmit, reset } = useForm<FilterSchema>({
|
||||
resolver: zodResolver(filterSchema),
|
||||
defaultValues: {
|
||||
bill_no: "",
|
||||
inner_no: "",
|
||||
created_at_start: "",
|
||||
created_at_end: "",
|
||||
phone: "",
|
||||
resource_no: "",
|
||||
sku_code: "all",
|
||||
product_code: "",
|
||||
},
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
setLoading(true)
|
||||
getSkuList({
|
||||
product_code: skuProductCode,
|
||||
})
|
||||
.then(resp => {
|
||||
if (!resp.success) {
|
||||
throw new Error(resp.message)
|
||||
}
|
||||
setSkuOptions(
|
||||
resp.data.map(sku => ({
|
||||
resource_code: sku.code,
|
||||
resource_name: sku.name,
|
||||
})),
|
||||
)
|
||||
})
|
||||
.catch(e => {
|
||||
console.error("获取套餐类型失败:", e)
|
||||
toast.error(
|
||||
`获取套餐类型失败:${e instanceof Error ? e.message : String(e)}`,
|
||||
)
|
||||
setSkuOptions([])
|
||||
})
|
||||
.finally(() => {
|
||||
setLoading(false)
|
||||
})
|
||||
}, [skuProductCode])
|
||||
|
||||
const table = useDataTable<Billing>((page, size) =>
|
||||
getBill({ page, size, user_id: Number(userId), ...filters }),
|
||||
)
|
||||
|
||||
const onFilter = handleSubmit(data => {
|
||||
const result: FilterValues = {}
|
||||
if (data.phone?.trim()) result.user_phone = data.phone.trim()
|
||||
if (data.inner_no?.trim()) result.trade_inner_no = data.inner_no.trim()
|
||||
if (data.bill_no?.trim()) result.bill_no = data.bill_no.trim()
|
||||
if (data.resource_no?.trim()) result.resource_no = data.resource_no.trim()
|
||||
if (data.product_code && data.product_code !== ProductCode.All) {
|
||||
result.product_code = data.product_code
|
||||
}
|
||||
if (data.sku_code && data.sku_code !== "all") {
|
||||
result.sku_code = data.sku_code
|
||||
}
|
||||
if (data.created_at_start)
|
||||
result.created_at_start = new Date(data.created_at_start)
|
||||
if (data.created_at_end)
|
||||
result.created_at_end = new Date(data.created_at_end)
|
||||
|
||||
setFilters(result)
|
||||
table.pagination.onPageChange(1)
|
||||
})
|
||||
|
||||
return (
|
||||
<div className="space-y-3">
|
||||
<form onSubmit={onFilter} className="bg-white p-4">
|
||||
<div className="flex flex-wrap items-end gap-4">
|
||||
<Controller
|
||||
name="resource_no"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-40 flex-none"
|
||||
>
|
||||
<FieldLabel>套餐号</FieldLabel>
|
||||
<Input {...field} placeholder="请输入套餐号" />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="bill_no"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-40 flex-none"
|
||||
>
|
||||
<FieldLabel>账单号</FieldLabel>
|
||||
<Input {...field} placeholder="请输入账单号" />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="inner_no"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-40 flex-none"
|
||||
>
|
||||
<FieldLabel>订单号</FieldLabel>
|
||||
<Input {...field} placeholder="请输入订单号" />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
|
||||
<Controller
|
||||
name="product_code"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-32 flex-none"
|
||||
>
|
||||
<FieldLabel>产品类型</FieldLabel>
|
||||
<Select
|
||||
value={skuProductCode}
|
||||
onValueChange={value => {
|
||||
setSkuProductCode(value as ProductCode)
|
||||
field.onChange(value)
|
||||
}}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectValue
|
||||
placeholder={"请选择"}
|
||||
defaultValue={
|
||||
skuProductCode === ProductCode.All ? "全部" : ""
|
||||
}
|
||||
/>
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value={ProductCode.All}>全部</SelectItem>
|
||||
<SelectItem value={ProductCode.Short}>短效</SelectItem>
|
||||
<SelectItem value={ProductCode.Long}>长效</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
|
||||
<Controller
|
||||
name="sku_code"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-32 flex-none"
|
||||
>
|
||||
<FieldLabel>套餐类型</FieldLabel>
|
||||
<Select
|
||||
value={loading ? undefined : field.value || "all"}
|
||||
onValueChange={field.onChange}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder={loading ? "加载中..." : "全部"} />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="all">全部</SelectItem>
|
||||
{skuOptions.map(option => (
|
||||
<SelectItem
|
||||
key={option.resource_code}
|
||||
value={option.resource_code}
|
||||
>
|
||||
{option.resource_name || option.resource_code}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="created_at_start"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-40 flex-none"
|
||||
>
|
||||
<FieldLabel>开始时间</FieldLabel>
|
||||
<Input type="date" {...field} />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
|
||||
<Controller
|
||||
name="created_at_end"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-40 flex-none"
|
||||
>
|
||||
<FieldLabel>结束时间</FieldLabel>
|
||||
<Input type="date" {...field} />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<FieldGroup className="flex-row justify-start mt-4 gap-2">
|
||||
<Button type="submit">筛选</Button>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
onClick={() => {
|
||||
reset()
|
||||
setSkuProductCode(ProductCode.All)
|
||||
setFilters({})
|
||||
table.pagination.onPageChange(1)
|
||||
}}
|
||||
>
|
||||
重置
|
||||
</Button>
|
||||
</FieldGroup>
|
||||
</form>
|
||||
|
||||
<Suspense>
|
||||
<DataTable<Billing>
|
||||
{...table}
|
||||
columns={[
|
||||
{ header: "套餐号", accessorKey: "resource.resource_no" },
|
||||
{
|
||||
header: "账单详情",
|
||||
accessorKey: "info",
|
||||
cell: ({ row }) => {
|
||||
const bill = row.original
|
||||
|
||||
return (
|
||||
<div className="flex items-center gap-2">
|
||||
<div className="shrink-0">
|
||||
{bill.type === 1 && (
|
||||
<div className="flex gap-2 items-center bg-orange-50 w-fit px-2 py-1 rounded-md">
|
||||
<CreditCard size={16} />
|
||||
<span>消费</span>
|
||||
</div>
|
||||
)}
|
||||
{bill.type === 2 && (
|
||||
<div className="flex gap-2 items-center bg-green-50 w-fit px-2 py-1 rounded-md">
|
||||
<CreditCard size={16} />
|
||||
<span>退款</span>
|
||||
</div>
|
||||
)}
|
||||
{bill.type === 3 && (
|
||||
<div className="flex gap-2 items-center bg-blue-50 w-fit px-2 py-1 rounded-md">
|
||||
<CreditCard size={16} />
|
||||
<span>充值</span>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<div className="text-sm">{bill.info}</div>
|
||||
</div>
|
||||
)
|
||||
},
|
||||
},
|
||||
{
|
||||
header: "应付金额",
|
||||
accessorKey: "amount",
|
||||
cell: ({ row }) => {
|
||||
const amount =
|
||||
typeof row.original.amount === "string"
|
||||
? parseFloat(row.original.amount)
|
||||
: row.original.amount || 0
|
||||
return (
|
||||
<div className="flex gap-1">
|
||||
<span
|
||||
className={
|
||||
amount > 0 ? "text-green-500" : "text-orange-500"
|
||||
}
|
||||
>
|
||||
¥{amount.toFixed(2)}
|
||||
</span>
|
||||
</div>
|
||||
)
|
||||
},
|
||||
},
|
||||
{
|
||||
header: "实付金额",
|
||||
accessorKey: "actual",
|
||||
cell: ({ row }) => {
|
||||
const actual =
|
||||
typeof row.original.actual === "string"
|
||||
? parseFloat(row.original.actual)
|
||||
: row.original.actual || 0
|
||||
return (
|
||||
<div className="flex gap-1">
|
||||
<span
|
||||
className={
|
||||
actual > 0 ? "text-green-500" : "text-orange-500"
|
||||
}
|
||||
>
|
||||
¥{actual.toFixed(2)}
|
||||
</span>
|
||||
</div>
|
||||
)
|
||||
},
|
||||
},
|
||||
{ header: "账单号", accessorKey: "bill_no" },
|
||||
{
|
||||
header: "订单号",
|
||||
accessorKey: "trade.inner_no",
|
||||
cell: ({ row }) => {
|
||||
const bill = row.original
|
||||
|
||||
return (
|
||||
<div className="flex items-center gap-2">
|
||||
<div className="shrink-0 w-20">
|
||||
{bill.trade?.acquirer === 1 && (
|
||||
<div className="flex gap-2 items-center bg-blue-50 w-fit px-2 py-1 rounded-md">
|
||||
<CreditCard size={16} />
|
||||
<span>支付宝</span>
|
||||
</div>
|
||||
)}
|
||||
{bill.trade?.acquirer === 2 && (
|
||||
<div className="flex gap-2 items-center bg-green-50 w-fit px-2 py-1 rounded-md">
|
||||
<CreditCard size={16} />
|
||||
<span>微信</span>
|
||||
</div>
|
||||
)}
|
||||
{bill.trade?.acquirer === 3 && (
|
||||
<div className="flex gap-2 items-center bg-orange-50 w-fit px-2 py-1 rounded-md">
|
||||
<CreditCard size={16} />
|
||||
<span>银联</span>
|
||||
</div>
|
||||
)}
|
||||
{!bill.trade?.acquirer && (
|
||||
<div className="flex gap-2 items-center bg-red-50 w-full px-2 py-1 rounded-md">
|
||||
<Wallet size={16} />
|
||||
<span>余额</span>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<div className="text-sm">{bill.trade?.inner_no}</div>
|
||||
</div>
|
||||
)
|
||||
},
|
||||
},
|
||||
{
|
||||
header: "创建时间",
|
||||
accessorKey: "created_at",
|
||||
cell: ({ row }) =>
|
||||
format(new Date(row.original.created_at), "yyyy-MM-dd HH:mm"),
|
||||
},
|
||||
]}
|
||||
/>
|
||||
</Suspense>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
315
src/app/(root)/client/channel/page.tsx
Normal file
315
src/app/(root)/client/channel/page.tsx
Normal file
@@ -0,0 +1,315 @@
|
||||
"use client"
|
||||
import { zodResolver } from "@hookform/resolvers/zod"
|
||||
import { format } from "date-fns"
|
||||
import { useSearchParams } from "next/navigation"
|
||||
import { Suspense, useState } from "react"
|
||||
import { Controller, useForm } from "react-hook-form"
|
||||
import { z } from "zod"
|
||||
import { getChannel } from "@/actions/channel"
|
||||
import { DataTable, useDataTable } from "@/components/data-table"
|
||||
import { Badge } from "@/components/ui/badge"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import {
|
||||
Field,
|
||||
FieldError,
|
||||
FieldGroup,
|
||||
FieldLabel,
|
||||
} from "@/components/ui/field"
|
||||
import { Input } from "@/components/ui/input"
|
||||
import type { Channel } from "@/models/channel"
|
||||
|
||||
type FilterValues = {
|
||||
batch_no?: string
|
||||
user_phone?: string
|
||||
resource_no?: string
|
||||
proxy_host?: string
|
||||
proxy_port?: number
|
||||
node_ip?: string
|
||||
expired_at_start?: Date
|
||||
expired_at_end?: Date
|
||||
}
|
||||
|
||||
const filterSchema = z
|
||||
.object({
|
||||
batch_no: z.string().optional(),
|
||||
user_phone: z.string().optional(),
|
||||
resource_no: z.string().optional(),
|
||||
proxy_host: z.string().optional(),
|
||||
proxy_port: z.string().optional(),
|
||||
node_ip: z.string().optional(),
|
||||
expired_at_start: z.string().optional(),
|
||||
expired_at_end: z.string().optional(),
|
||||
})
|
||||
.superRefine((data, ctx) => {
|
||||
if (data.expired_at_start && data.expired_at_end) {
|
||||
const start = new Date(data.expired_at_start)
|
||||
const end = new Date(data.expired_at_end)
|
||||
|
||||
if (end < start) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: "结束时间不能早于开始时间",
|
||||
path: ["expired_at_end"],
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
type FilterSchema = z.infer<typeof filterSchema>
|
||||
|
||||
const ispMap: Record<number, string> = {
|
||||
1: "电信",
|
||||
2: "联通",
|
||||
3: "移动",
|
||||
}
|
||||
|
||||
export default function ChannelPage() {
|
||||
const searchParams = useSearchParams()
|
||||
const userId = searchParams.get("userId")
|
||||
const [filters, setFilters] = useState<FilterValues>({})
|
||||
const { control, handleSubmit, reset } = useForm<FilterSchema>({
|
||||
resolver: zodResolver(filterSchema),
|
||||
defaultValues: {
|
||||
batch_no: "",
|
||||
user_phone: "",
|
||||
resource_no: "",
|
||||
proxy_port: "",
|
||||
proxy_host: "",
|
||||
node_ip: "",
|
||||
expired_at_start: "",
|
||||
expired_at_end: "",
|
||||
},
|
||||
})
|
||||
|
||||
const table = useDataTable<Channel>((page, size) =>
|
||||
getChannel({ page, size, user_id: Number(userId), ...filters }),
|
||||
)
|
||||
|
||||
const onFilter = handleSubmit(data => {
|
||||
const result: FilterValues = {}
|
||||
if (data.batch_no?.trim()) result.batch_no = data.batch_no.trim()
|
||||
if (data.user_phone?.trim()) result.user_phone = data.user_phone.trim()
|
||||
if (data.resource_no?.trim()) result.resource_no = data.resource_no.trim()
|
||||
if (data.proxy_host?.trim()) result.proxy_host = data.proxy_host.trim()
|
||||
if (data.proxy_port?.trim())
|
||||
result.proxy_port = Number(data.proxy_port.trim())
|
||||
if (data.node_ip?.trim()) result.node_ip = data.node_ip.trim()
|
||||
if (data.expired_at_start)
|
||||
result.expired_at_start = new Date(data.expired_at_start)
|
||||
if (data.expired_at_end)
|
||||
result.expired_at_end = new Date(data.expired_at_end)
|
||||
setFilters(result)
|
||||
table.pagination.onPageChange(1)
|
||||
})
|
||||
|
||||
return (
|
||||
<div className="space-y-3">
|
||||
<form onSubmit={onFilter} className="bg-white p-4 rounded-lg">
|
||||
<div className="flex flex-wrap items-end gap-4">
|
||||
<Controller
|
||||
name="batch_no"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-40 flex-none"
|
||||
>
|
||||
<FieldLabel>批次号</FieldLabel>
|
||||
<Input {...field} placeholder="请输入批次号" />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="resource_no"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-40 flex-none"
|
||||
>
|
||||
<FieldLabel>套餐号</FieldLabel>
|
||||
<Input {...field} placeholder="请输入套餐号" />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="proxy_host"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-40 flex-none"
|
||||
>
|
||||
<FieldLabel>代理IP</FieldLabel>
|
||||
<Input {...field} placeholder="请输入代理IP" />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="proxy_port"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-32 flex-none"
|
||||
>
|
||||
<FieldLabel>代理端口</FieldLabel>
|
||||
<Input {...field} placeholder="请输入代理端口" />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="node_ip"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-40 flex-none"
|
||||
>
|
||||
<FieldLabel>节点</FieldLabel>
|
||||
<Input {...field} placeholder="请输入节点" />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="expired_at_start"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-40 flex-none"
|
||||
>
|
||||
<FieldLabel>开始时间</FieldLabel>
|
||||
<Input type="date" {...field} />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="expired_at_end"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-40 flex-none"
|
||||
>
|
||||
<FieldLabel>结束时间</FieldLabel>
|
||||
<Input type="date" {...field} />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<FieldGroup className="flex-row justify-start mt-4 gap-2">
|
||||
<Button type="submit">筛选</Button>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
onClick={() => {
|
||||
reset()
|
||||
setFilters({})
|
||||
table.pagination.onPageChange(1)
|
||||
}}
|
||||
>
|
||||
重置
|
||||
</Button>
|
||||
</FieldGroup>
|
||||
</form>
|
||||
|
||||
<Suspense>
|
||||
<DataTable<Channel>
|
||||
{...table}
|
||||
columns={[
|
||||
{
|
||||
header: "会员号",
|
||||
accessorFn: row => row.user?.phone || "",
|
||||
},
|
||||
{ header: "套餐号", accessorKey: "resource.resource_no" },
|
||||
{ header: "批次号", accessorKey: "batch_no" },
|
||||
{
|
||||
header: "节点",
|
||||
accessorFn: row => row.ip || row.edge_ref || row.edge_id,
|
||||
},
|
||||
|
||||
{
|
||||
header: "自动配置",
|
||||
cell: ({ row }) => {
|
||||
const prov = row.original.filter_prov
|
||||
const city = row.original.filter_city
|
||||
const isp = row.original.filter_isp
|
||||
const parts = []
|
||||
if (prov && prov !== "all") parts.push(prov)
|
||||
if (city && city !== "all") parts.push(city)
|
||||
if (isp && isp !== "all") {
|
||||
parts.push(ispMap[Number(isp)] || isp)
|
||||
}
|
||||
return (
|
||||
<div className="text-sm">
|
||||
{parts.length > 0 ? parts.join(" / ") : "不限"}
|
||||
</div>
|
||||
)
|
||||
},
|
||||
},
|
||||
{
|
||||
header: "网关地址",
|
||||
accessorKey: "host",
|
||||
cell: ({ row }) => {
|
||||
return (
|
||||
<span>
|
||||
{row.original.host}:{row.original.port}
|
||||
</span>
|
||||
)
|
||||
},
|
||||
},
|
||||
{
|
||||
header: "认证方式",
|
||||
cell: ({ row }) => {
|
||||
const channel = row.original
|
||||
const hasWhitelist =
|
||||
channel.whitelists && channel.whitelists.trim() !== ""
|
||||
const hasAuth = channel.username && channel.password
|
||||
return (
|
||||
<div className="flex flex-col gap-1 min-w-0">
|
||||
{hasWhitelist ? (
|
||||
<div className="flex flex-col">
|
||||
<span>白名单</span>
|
||||
<div className="flex flex-wrap gap-1 max-w-50">
|
||||
{channel.whitelists.split(",").map(ip => (
|
||||
<Badge key={ip.trim()} variant="secondary">
|
||||
{ip.trim()}
|
||||
</Badge>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
) : hasAuth ? (
|
||||
<div className="flex flex-col">
|
||||
<span>账号密码</span>
|
||||
<Badge variant="secondary">
|
||||
{channel.username}:{channel.password}
|
||||
</Badge>
|
||||
</div>
|
||||
) : (
|
||||
<span className="text-sm text-gray-400">无认证</span>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
},
|
||||
},
|
||||
{
|
||||
header: "过期时间",
|
||||
accessorKey: "expired_at",
|
||||
cell: ({ row }) =>
|
||||
format(new Date(row.original.expired_at), "yyyy-MM-dd HH:mm"),
|
||||
},
|
||||
]}
|
||||
/>
|
||||
</Suspense>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
389
src/app/(root)/client/cust/page.tsx
Normal file
389
src/app/(root)/client/cust/page.tsx
Normal file
@@ -0,0 +1,389 @@
|
||||
"use client"
|
||||
import { zodResolver } from "@hookform/resolvers/zod"
|
||||
import { format } from "date-fns"
|
||||
import { useRouter } from "next/navigation"
|
||||
import { Suspense, useCallback, useState } from "react"
|
||||
import { Controller, useForm } from "react-hook-form"
|
||||
import { toast } from "sonner"
|
||||
import { z } from "zod"
|
||||
import { getPageUser } from "@/actions/user"
|
||||
// import { DeductionDialog } from "@/app/(root)/cust/deduction"
|
||||
// import { DepositDialog } from "@/app/(root)/cust/deposit"
|
||||
import { UpdateDialog } from "@/app/(root)/cust/update"
|
||||
import { Auth } from "@/components/auth"
|
||||
import { DataTable } from "@/components/data-table"
|
||||
import { Badge } from "@/components/ui/badge"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import {
|
||||
Field,
|
||||
FieldError,
|
||||
FieldGroup,
|
||||
FieldLabel,
|
||||
} from "@/components/ui/field"
|
||||
import { Input } from "@/components/ui/input"
|
||||
import {
|
||||
ScopeBalanceActivityReadOfUser,
|
||||
ScopeBatchReadOfUser,
|
||||
ScopeBillReadOfUser,
|
||||
ScopeChannelReadOfUser,
|
||||
ScopeResourceRead,
|
||||
ScopeTradeReadOfUser,
|
||||
ScopeUserWrite,
|
||||
ScopeUserWriteBalance,
|
||||
} from "@/lib/scopes"
|
||||
import type { User } from "@/models/user"
|
||||
import { AddUserDialog } from "../../cust/create"
|
||||
|
||||
interface UserQueryParams {
|
||||
account?: string
|
||||
name?: string
|
||||
}
|
||||
|
||||
const filterSchema = z.object({
|
||||
phone: z.string().optional(),
|
||||
name: z.string().optional(),
|
||||
})
|
||||
|
||||
type FormValues = z.infer<typeof filterSchema>
|
||||
|
||||
export default function UserQueryPage() {
|
||||
const [userList, setUserList] = useState<User[]>([])
|
||||
const [loading, setLoading] = useState(false)
|
||||
// const [depositDialog, setDepositDialog] = useState(false)
|
||||
// const [deposit, setDeposit] = useState<User | null>(null)
|
||||
// const [deductionDialog, setDeductionDialog] = useState(false)
|
||||
// const [deduction, setDeduction] = useState<User | null>(null)
|
||||
const [isEditDialogOpen, setIsEditDialogOpen] = useState(false)
|
||||
const [currentEditUser, setCurrentEditUser] = useState<User | null>(null)
|
||||
const [currentFilters, setCurrentFilters] = useState<UserQueryParams>({})
|
||||
const [isAddDialogOpen, setIsAddDialogOpen] = useState(false)
|
||||
|
||||
const router = useRouter()
|
||||
const { control, handleSubmit, reset } = useForm<FormValues>({
|
||||
resolver: zodResolver(filterSchema),
|
||||
defaultValues: {
|
||||
phone: "",
|
||||
name: "",
|
||||
},
|
||||
})
|
||||
|
||||
const fetchUsers = useCallback(async (filters: UserQueryParams = {}) => {
|
||||
setLoading(true)
|
||||
try {
|
||||
const res = await getPageUser(filters)
|
||||
if (res.success) {
|
||||
const data = Array.isArray(res.data) ? res.data : [res.data]
|
||||
setUserList(data)
|
||||
} else {
|
||||
toast.error(res.message || "获取用户失败")
|
||||
setUserList([])
|
||||
}
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : error
|
||||
toast.error(`获取用户失败: ${message}`)
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}, [])
|
||||
|
||||
const onFilter = handleSubmit((data: FormValues) => {
|
||||
const params: UserQueryParams = {}
|
||||
if (data.phone?.trim()) params.account = data.phone.trim()
|
||||
if (data.name?.trim()) params.name = data.name.trim()
|
||||
if (Object.keys(params).length === 0) {
|
||||
toast.info("请至少输入一个筛选条件")
|
||||
return
|
||||
}
|
||||
setCurrentFilters(params)
|
||||
fetchUsers(params)
|
||||
})
|
||||
|
||||
const refreshTable = useCallback(() => {
|
||||
if (Object.keys(currentFilters).length > 0) {
|
||||
fetchUsers(currentFilters)
|
||||
}
|
||||
}, [fetchUsers, currentFilters])
|
||||
|
||||
const handleReset = () => {
|
||||
reset()
|
||||
setCurrentFilters({})
|
||||
setUserList([])
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-3">
|
||||
<form onSubmit={onFilter} className="bg-white p-4">
|
||||
<div className="flex flex-wrap items-end gap-4">
|
||||
<Controller
|
||||
name="phone"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-40 flex-none"
|
||||
>
|
||||
<FieldLabel>手机号</FieldLabel>
|
||||
<Input {...field} placeholder="请输入手机号" />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
|
||||
<Controller
|
||||
name="name"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-40 flex-none"
|
||||
>
|
||||
<FieldLabel>姓名</FieldLabel>
|
||||
<Input {...field} placeholder="请输入姓名" />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<FieldGroup className="flex-row justify-start mt-4 gap-2">
|
||||
<Auth scope={ScopeUserWrite}>
|
||||
<Button type="button" onClick={() => setIsAddDialogOpen(true)}>
|
||||
添加用户
|
||||
</Button>
|
||||
</Auth>
|
||||
<Button type="button" variant="outline" onClick={handleReset}>
|
||||
重置
|
||||
</Button>
|
||||
<Button type="submit">筛选</Button>
|
||||
</FieldGroup>
|
||||
</form>
|
||||
|
||||
<Suspense>
|
||||
<DataTable<User>
|
||||
data={userList || []}
|
||||
status={loading ? "load" : "done"}
|
||||
columns={[
|
||||
{ header: "手机", accessorKey: "phone" },
|
||||
{
|
||||
header: "创建时间",
|
||||
accessorKey: "created_at",
|
||||
cell: ({ row }) =>
|
||||
format(new Date(row.original.created_at), "yyyy-MM-dd HH:mm"),
|
||||
},
|
||||
{
|
||||
header: "客户来源",
|
||||
accessorKey: "source",
|
||||
cell: ({ row }) => {
|
||||
const sourceMap: Record<number, string> = {
|
||||
0: "官网注册",
|
||||
1: "管理员添加",
|
||||
2: "代理商注册",
|
||||
3: "代理商添加",
|
||||
}
|
||||
return sourceMap[row.original.source] ?? "未知"
|
||||
},
|
||||
},
|
||||
{
|
||||
header: "余额",
|
||||
accessorKey: "balance",
|
||||
cell: ({ row }) => {
|
||||
const balance = Number(row.original.balance) || 0
|
||||
return (
|
||||
<span
|
||||
className={
|
||||
balance > 0 ? "text-green-500" : "text-orange-500"
|
||||
}
|
||||
>
|
||||
¥{balance.toFixed(2)}
|
||||
</span>
|
||||
)
|
||||
},
|
||||
},
|
||||
{ header: "账号", accessorKey: "username" },
|
||||
{
|
||||
header: "账号状态",
|
||||
accessorKey: "status",
|
||||
cell: ({ row }) => (row.original.status === 1 ? "正常" : "禁用"),
|
||||
},
|
||||
{
|
||||
header: "客户经理",
|
||||
cell: ({ row }) => row.original.admin?.name || "",
|
||||
},
|
||||
{ header: "姓名", accessorKey: "name" },
|
||||
{
|
||||
header: "实名状态",
|
||||
accessorKey: "id_type",
|
||||
cell: ({ row }) => (
|
||||
<Badge
|
||||
variant={row.original.id_type === 1 ? "default" : "secondary"}
|
||||
className={
|
||||
row.original.id_type === 1
|
||||
? "bg-green-100 text-green-800"
|
||||
: "bg-gray-100 text-gray-800"
|
||||
}
|
||||
>
|
||||
{row.original.id_type === 1 ? "已认证" : "未认证"}
|
||||
</Badge>
|
||||
),
|
||||
},
|
||||
{
|
||||
header: "最后登录时间",
|
||||
accessorKey: "last_login",
|
||||
cell: ({ row }) =>
|
||||
row.original.last_login
|
||||
? format(
|
||||
new Date(row.original.last_login),
|
||||
"yyyy-MM-dd HH:mm",
|
||||
)
|
||||
: "",
|
||||
},
|
||||
{
|
||||
header: "最后登录IP",
|
||||
accessorKey: "last_login_ip",
|
||||
cell: ({ row }) => row.original.last_login_ip || "",
|
||||
},
|
||||
{ header: "联系方式", accessorKey: "contact_wechat" },
|
||||
{
|
||||
id: "action",
|
||||
meta: { pin: "right" },
|
||||
header: "操作",
|
||||
cell: ({ row }) => {
|
||||
return (
|
||||
<div className="flex flex-wrap gap-2 w-75">
|
||||
{/* <Auth scope={ScopeUserWriteBalanceInc}>
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
setDeposit(row.original)
|
||||
setDepositDialog(true)
|
||||
}}
|
||||
>
|
||||
充值
|
||||
</Button>
|
||||
</Auth>
|
||||
<Auth scope={ScopeUserWriteBalanceDec}>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="destructive"
|
||||
onClick={() => {
|
||||
setDeduction(row.original)
|
||||
setDeductionDialog(true)
|
||||
}}
|
||||
>
|
||||
扣款
|
||||
</Button>
|
||||
</Auth> */}
|
||||
<Auth scope={ScopeUserWriteBalance}>
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
setCurrentEditUser(row.original)
|
||||
setIsEditDialogOpen(true)
|
||||
}}
|
||||
>
|
||||
修改
|
||||
</Button>
|
||||
</Auth>
|
||||
<Auth scope={ScopeTradeReadOfUser}>
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
router.push(`/client/trade?userId=${row.original.id}`)
|
||||
}}
|
||||
>
|
||||
交易明细
|
||||
</Button>
|
||||
</Auth>
|
||||
<Auth scope={ScopeBillReadOfUser}>
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
router.push(
|
||||
`/client/billing?userId=${row.original.id}`,
|
||||
)
|
||||
}}
|
||||
>
|
||||
账单详情
|
||||
</Button>
|
||||
</Auth>
|
||||
<Auth scope={ScopeResourceRead}>
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
router.push(
|
||||
`/client/resources?userId=${row.original.id}`,
|
||||
)
|
||||
}}
|
||||
>
|
||||
套餐管理
|
||||
</Button>
|
||||
</Auth>
|
||||
<Auth scope={ScopeBatchReadOfUser}>
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
router.push(`/client/batch?userId=${row.original.id}`)
|
||||
}}
|
||||
>
|
||||
提取记录
|
||||
</Button>
|
||||
</Auth>
|
||||
<Auth scope={ScopeChannelReadOfUser}>
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
router.push(
|
||||
`/client/channel?userId=${row.original.id}`,
|
||||
)
|
||||
}}
|
||||
>
|
||||
IP管理
|
||||
</Button>
|
||||
</Auth>
|
||||
<Auth scope={ScopeBalanceActivityReadOfUser}>
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
router.push(
|
||||
`/client/balance?userId=${row.original.id}&phone=${row.original.phone}`,
|
||||
)
|
||||
}}
|
||||
>
|
||||
余额操作
|
||||
</Button>
|
||||
</Auth>
|
||||
</div>
|
||||
)
|
||||
},
|
||||
},
|
||||
]}
|
||||
/>
|
||||
</Suspense>
|
||||
<AddUserDialog
|
||||
open={isAddDialogOpen}
|
||||
onOpenChange={setIsAddDialogOpen}
|
||||
onSuccess={refreshTable}
|
||||
/>
|
||||
<UpdateDialog
|
||||
open={isEditDialogOpen}
|
||||
onOpenChange={setIsEditDialogOpen}
|
||||
currentUser={currentEditUser}
|
||||
onSuccess={refreshTable}
|
||||
/>
|
||||
|
||||
{/* <DepositDialog
|
||||
open={depositDialog}
|
||||
onOpenChange={setDepositDialog}
|
||||
currentUser={deposit}
|
||||
onSuccess={refreshTable}
|
||||
/> */}
|
||||
|
||||
{/* <DeductionDialog
|
||||
open={deductionDialog}
|
||||
onOpenChange={setDeductionDialog}
|
||||
currentUser={deduction}
|
||||
onSuccess={refreshTable}
|
||||
/> */}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
567
src/app/(root)/client/resources/page.tsx
Normal file
567
src/app/(root)/client/resources/page.tsx
Normal file
@@ -0,0 +1,567 @@
|
||||
"use client"
|
||||
import { zodResolver } from "@hookform/resolvers/zod"
|
||||
import { format, isBefore, isSameDay } from "date-fns"
|
||||
import { Box, Loader2, Timer } from "lucide-react"
|
||||
import { useSearchParams } from "next/navigation"
|
||||
import { Suspense, useCallback, useMemo, useState } from "react"
|
||||
import { Controller, useForm } from "react-hook-form"
|
||||
import { toast } from "sonner"
|
||||
import { z } from "zod"
|
||||
import {
|
||||
ResourceLong,
|
||||
ResourceShort,
|
||||
updateResource,
|
||||
} from "@/actions/resources"
|
||||
import { DataTable, useDataTable } from "@/components/data-table"
|
||||
import { Badge } from "@/components/ui/badge"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import {
|
||||
Field,
|
||||
FieldError,
|
||||
FieldGroup,
|
||||
FieldLabel,
|
||||
} from "@/components/ui/field"
|
||||
import { Input } from "@/components/ui/input"
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from "@/components/ui/select"
|
||||
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"
|
||||
import type { Resources } from "@/models/resources"
|
||||
|
||||
const filterSchema = z
|
||||
.object({
|
||||
user_phone: z.string().optional(),
|
||||
resource_no: z.string().optional(),
|
||||
status: z.string().optional(),
|
||||
type: z.string().optional(),
|
||||
created_at_start: z.string().optional(),
|
||||
created_at_end: z.string().optional(),
|
||||
expired: z.string().optional(),
|
||||
})
|
||||
.superRefine((data, ctx) => {
|
||||
if (data.created_at_start && data.created_at_end) {
|
||||
const start = new Date(data.created_at_start)
|
||||
const end = new Date(data.created_at_end)
|
||||
|
||||
if (end < start) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: "结束时间不能早于开始时间",
|
||||
path: ["created_at_end"],
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
type FormValues = z.infer<typeof filterSchema>
|
||||
|
||||
interface FilterParams {
|
||||
user_phone?: string
|
||||
resource_no?: string
|
||||
active?: boolean
|
||||
mode?: number
|
||||
created_at_start?: Date
|
||||
created_at_end?: Date
|
||||
expired?: boolean
|
||||
}
|
||||
|
||||
// 获取资源类型
|
||||
function getResourceType(resource: Resources): number {
|
||||
if ("short" in resource && resource.short) {
|
||||
return resource.short.type
|
||||
}
|
||||
if ("long" in resource && resource.long) {
|
||||
return resource.long.type
|
||||
}
|
||||
return resource.type
|
||||
}
|
||||
|
||||
// 获取资源详情对象
|
||||
function getResourceDetail(resource: Resources) {
|
||||
if ("short" in resource && resource.short) {
|
||||
return resource.short
|
||||
}
|
||||
if ("long" in resource && resource.long) {
|
||||
return resource.long
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
// 获取过期时间
|
||||
function getExpireAt(resource: Resources): Date | null | undefined {
|
||||
if ("short" in resource && resource.short) {
|
||||
return resource.short.expire_at
|
||||
}
|
||||
if ("long" in resource && resource.long) {
|
||||
return resource.long.expire_at
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
function getName(resource: Resources): string | null | undefined {
|
||||
if ("short" in resource && resource.short) {
|
||||
return resource.short.sku?.name
|
||||
}
|
||||
if ("long" in resource && resource.long) {
|
||||
return resource.long.sku?.name
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
// 获取最近使用时间
|
||||
function getLastAt(resource: Resources): Date | null | undefined {
|
||||
if ("short" in resource && resource.short) {
|
||||
return resource.short.last_at
|
||||
}
|
||||
if ("long" in resource && resource.long) {
|
||||
return resource.long.last_at
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
// 资源类型徽章
|
||||
function ResourceTypeBadge({ resource }: { resource: Resources }) {
|
||||
const type = getResourceType(resource)
|
||||
if (type === 1) {
|
||||
return (
|
||||
<div className="flex gap-2 items-center bg-green-50 w-fit px-2 py-1 rounded-md">
|
||||
<Timer size={20} />
|
||||
<span>包时</span>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
if (type === 2) {
|
||||
return (
|
||||
<div className="flex gap-2 items-center bg-blue-50 w-fit px-2 py-1 rounded-md">
|
||||
<Box size={20} />
|
||||
<span>包量</span>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
// 过期徽章
|
||||
function ExpireBadge({ expireAt }: { expireAt: Date | null | undefined }) {
|
||||
if (!expireAt) return null
|
||||
if (isBefore(expireAt, new Date())) {
|
||||
return <Badge variant="destructive">过期</Badge>
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
// 格式化日期
|
||||
function formatDateTime(date: Date | null | undefined) {
|
||||
if (!date) return "-"
|
||||
return format(date, "yyyy-MM-dd HH:mm")
|
||||
}
|
||||
|
||||
// 计算今日使用量
|
||||
function getTodayUsage(lastAt: Date | null | undefined, daily: number) {
|
||||
if (lastAt && isSameDay(lastAt, new Date())) {
|
||||
return daily
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
export default function ResourcesPage() {
|
||||
return (
|
||||
<div>
|
||||
<Tabs defaultValue="short">
|
||||
<TabsList className="bg-card p-1.5 rounded-lg">
|
||||
<TabsTrigger
|
||||
value="short"
|
||||
className="w-30 h-9 data-[state=active]:bg-primary-muted text-base rounded-md"
|
||||
>
|
||||
短效套餐
|
||||
</TabsTrigger>
|
||||
<TabsTrigger
|
||||
value="long"
|
||||
className="w-30 h-9 data-[state=active]:bg-primary-muted text-base rounded-md"
|
||||
>
|
||||
长效套餐
|
||||
</TabsTrigger>
|
||||
</TabsList>
|
||||
<TabsContent value="short" className="flex flex-col gap-4">
|
||||
<ResourceList resourceType="short" />
|
||||
</TabsContent>
|
||||
<TabsContent value="long" className="flex flex-col gap-4">
|
||||
<ResourceList resourceType="long" />
|
||||
</TabsContent>
|
||||
</Tabs>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
interface ResourceListProps {
|
||||
resourceType: "long" | "short"
|
||||
}
|
||||
|
||||
function ResourceList({ resourceType }: ResourceListProps) {
|
||||
const searchParams = useSearchParams()
|
||||
const userId = searchParams.get("userId")
|
||||
const [filters, setFilters] = useState<FilterParams>({})
|
||||
const isLong = resourceType === "long"
|
||||
const listFn = isLong ? ResourceLong : ResourceShort
|
||||
const [updatingId, setUpdatingId] = useState<number | null>(null)
|
||||
const { control, handleSubmit, reset } = useForm<FormValues>({
|
||||
resolver: zodResolver(filterSchema),
|
||||
defaultValues: {
|
||||
user_phone: "",
|
||||
resource_no: "",
|
||||
status: "all",
|
||||
type: "all",
|
||||
created_at_start: "",
|
||||
created_at_end: "",
|
||||
expired: "all",
|
||||
},
|
||||
})
|
||||
|
||||
const fetchResources = useCallback(
|
||||
(page: number, size: number) => {
|
||||
return listFn({ page, size, user_id: Number(userId), ...filters })
|
||||
},
|
||||
[listFn, filters, userId],
|
||||
)
|
||||
|
||||
const table = useDataTable<Resources>(fetchResources)
|
||||
const refreshTable = useCallback(() => {
|
||||
setFilters(prev => ({ ...prev }))
|
||||
}, [])
|
||||
|
||||
const handleStatusChange = useCallback(
|
||||
async (resource: Resources, newStatusValue: string) => {
|
||||
const newActive = newStatusValue === "0"
|
||||
if (newActive === resource.active) return
|
||||
setUpdatingId(resource.id)
|
||||
try {
|
||||
await updateResource({
|
||||
id: resource.id,
|
||||
active: newActive,
|
||||
})
|
||||
toast.success("更新成功", {
|
||||
description: `资源状态已更新为${newActive ? "启用" : "禁用"}`,
|
||||
})
|
||||
refreshTable()
|
||||
} catch (error) {
|
||||
console.error("更新状态失败:", error)
|
||||
toast.error("更新失败", {
|
||||
description: error instanceof Error ? error.message : "请稍后重试",
|
||||
})
|
||||
} finally {
|
||||
setUpdatingId(null)
|
||||
}
|
||||
},
|
||||
[refreshTable],
|
||||
)
|
||||
|
||||
const onFilter = handleSubmit(data => {
|
||||
const result: FilterParams = {}
|
||||
if (data.user_phone?.trim()) result.user_phone = data.user_phone.trim()
|
||||
if (data.resource_no?.trim()) result.resource_no = data.resource_no.trim()
|
||||
if (data.status && data.status !== "all") {
|
||||
result.active = data.status === "0"
|
||||
}
|
||||
if (data.type && data.type !== "all") {
|
||||
result.mode = Number(data.type)
|
||||
}
|
||||
if (data.expired && data.expired !== "all") {
|
||||
result.expired = data.expired === "1"
|
||||
}
|
||||
if (data.created_at_start)
|
||||
result.created_at_start = new Date(data.created_at_start)
|
||||
if (data.created_at_end)
|
||||
result.created_at_end = new Date(data.created_at_end)
|
||||
|
||||
setFilters(result)
|
||||
table.pagination.onPageChange(1)
|
||||
})
|
||||
|
||||
const columns = useMemo(
|
||||
() => [
|
||||
{
|
||||
header: "会员号",
|
||||
accessorFn: (row: Resources) => row.user?.phone || "-",
|
||||
},
|
||||
{
|
||||
header: "套餐",
|
||||
cell: ({ row }: { row: { original: Resources } }) => {
|
||||
const resourceNo = row.original.resource_no
|
||||
const name = getName(row.original)
|
||||
const expireAt = getExpireAt(row.original)
|
||||
return (
|
||||
<div className="flex flex-col gap-1">
|
||||
<div>{name}</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="text-xs text-gray-500">{resourceNo}</span>
|
||||
<ExpireBadge expireAt={expireAt} />
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
},
|
||||
},
|
||||
{
|
||||
header: "类型",
|
||||
cell: ({ row }: { row: { original: Resources } }) => {
|
||||
return <ResourceTypeBadge resource={row.original} />
|
||||
},
|
||||
},
|
||||
{
|
||||
header: "IP时效",
|
||||
cell: ({ row }: { row: { original: Resources } }) => {
|
||||
const detail = getResourceDetail(row.original)
|
||||
const live = detail?.live
|
||||
if (live === undefined) return "-"
|
||||
return <span>{isLong ? `${live}小时` : `${live}分钟`}</span>
|
||||
},
|
||||
},
|
||||
{
|
||||
header: "使用情况",
|
||||
cell: ({ row }: { row: { original: Resources } }) => {
|
||||
const detail = getResourceDetail(row.original)
|
||||
const type = getResourceType(row.original)
|
||||
|
||||
if (!detail) return <span>-</span>
|
||||
|
||||
if (type === 1) {
|
||||
// 包时
|
||||
const todayUsage = getTodayUsage(detail.last_at, detail.daily || 0)
|
||||
return (
|
||||
<div className="flex gap-1">
|
||||
<span>
|
||||
{todayUsage}/{detail.quota}
|
||||
</span>
|
||||
</div>
|
||||
)
|
||||
} else {
|
||||
// 包量
|
||||
if (isLong) {
|
||||
return (
|
||||
<div className="flex gap-1">
|
||||
{detail.used < detail.quota ? (
|
||||
<span className="text-green-500">正常</span>
|
||||
) : (
|
||||
<span className="text-red-500">已用完</span>
|
||||
)}
|
||||
<span>|</span>
|
||||
<span>
|
||||
{detail.used}/{detail.quota}
|
||||
</span>
|
||||
</div>
|
||||
)
|
||||
} else {
|
||||
return (
|
||||
<div className="flex gap-1">
|
||||
<span>
|
||||
{detail.used}/{detail.quota}
|
||||
</span>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
header: "最近使用时间",
|
||||
cell: ({ row }: { row: { original: Resources } }) => {
|
||||
const lastAt = getLastAt(row.original)
|
||||
return lastAt ? formatDateTime(lastAt) : "暂未使用"
|
||||
},
|
||||
},
|
||||
{
|
||||
header: "开通时间",
|
||||
cell: ({ row }: { row: { original: Resources } }) => {
|
||||
return formatDateTime(row.original.created_at)
|
||||
},
|
||||
},
|
||||
...(!isLong
|
||||
? [
|
||||
{
|
||||
header: "到期时间",
|
||||
cell: ({ row }: { row: { original: Resources } }) => {
|
||||
return formatDateTime(getExpireAt(row.original))
|
||||
},
|
||||
},
|
||||
]
|
||||
: []),
|
||||
{
|
||||
id: "action",
|
||||
meta: { pin: "right" },
|
||||
header: "状态",
|
||||
cell: ({ row }: { row: { original: Resources } }) => {
|
||||
const resource = row.original
|
||||
const isLoading = updatingId === resource.id
|
||||
const currentActive = resource.active
|
||||
return (
|
||||
<div className="flex items-center gap-2">
|
||||
<Select
|
||||
value={currentActive ? "0" : "1"}
|
||||
onValueChange={val => handleStatusChange(resource, val)}
|
||||
disabled={isLoading}
|
||||
>
|
||||
<SelectTrigger className="min-w-20">
|
||||
<SelectValue placeholder="状态" />
|
||||
</SelectTrigger>
|
||||
<SelectContent className="min-w-(--radix-select-trigger-width)">
|
||||
<SelectItem value="0">启用</SelectItem>
|
||||
<SelectItem value="1">禁用</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
{isLoading && (
|
||||
<Loader2 className="h-4 w-4 animate-spin text-muted-foreground" />
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
},
|
||||
},
|
||||
],
|
||||
[isLong, updatingId, handleStatusChange],
|
||||
)
|
||||
|
||||
return (
|
||||
<div className="space-y-3">
|
||||
<form onSubmit={onFilter} className="bg-white p-4 rounded-lg">
|
||||
<div className="flex flex-wrap items-end gap-4">
|
||||
<Controller
|
||||
name="user_phone"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-40 flex-none"
|
||||
>
|
||||
<FieldLabel>会员号</FieldLabel>
|
||||
<Input {...field} placeholder="请输入会员号" />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="resource_no"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-40 flex-none"
|
||||
>
|
||||
<FieldLabel>套餐号</FieldLabel>
|
||||
<Input {...field} placeholder="请输入套餐号" />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="status"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field data-invalid={fieldState.invalid} className="w-32">
|
||||
<FieldLabel>状态</FieldLabel>
|
||||
<Select value={field.value} onValueChange={field.onChange}>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder="全部" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="all">全部</SelectItem>
|
||||
<SelectItem value="0">启用</SelectItem>
|
||||
<SelectItem value="1">禁用</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="type"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field data-invalid={fieldState.invalid} className="w-32">
|
||||
<FieldLabel>类型</FieldLabel>
|
||||
<Select value={field.value} onValueChange={field.onChange}>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder="全部" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="all">全部</SelectItem>
|
||||
<SelectItem value="1">包时</SelectItem>
|
||||
<SelectItem value="2">包量</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="expired"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field data-invalid={fieldState.invalid} className="w-32">
|
||||
<FieldLabel>是否过期</FieldLabel>
|
||||
<Select value={field.value} onValueChange={field.onChange}>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder="全部" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="all">全部</SelectItem>
|
||||
<SelectItem value="0">未过期</SelectItem>
|
||||
<SelectItem value="1">已过期</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="created_at_start"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-40 flex-none"
|
||||
>
|
||||
<FieldLabel>开始时间</FieldLabel>
|
||||
<Input type="date" {...field} />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="created_at_end"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-40 flex-none"
|
||||
>
|
||||
<FieldLabel>结束时间</FieldLabel>
|
||||
<Input type="date" {...field} />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
<FieldGroup className="flex-row justify-start mt-4 gap-2">
|
||||
<Button type="submit">筛选</Button>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
onClick={() => {
|
||||
reset()
|
||||
setFilters({})
|
||||
table.pagination.onPageChange(1)
|
||||
}}
|
||||
>
|
||||
重置
|
||||
</Button>
|
||||
</FieldGroup>
|
||||
</form>
|
||||
|
||||
<Suspense fallback={<div className="text-center p-4">加载中...</div>}>
|
||||
<DataTable<Resources> {...table} columns={columns} />
|
||||
</Suspense>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
350
src/app/(root)/client/trade/page.tsx
Normal file
350
src/app/(root)/client/trade/page.tsx
Normal file
@@ -0,0 +1,350 @@
|
||||
"use client"
|
||||
import { zodResolver } from "@hookform/resolvers/zod"
|
||||
import { format } from "date-fns"
|
||||
import { CheckCircle, Clock, XCircle } from "lucide-react"
|
||||
import { useSearchParams } from "next/navigation"
|
||||
import { Suspense, useCallback, useState } from "react"
|
||||
import { Controller, useForm } from "react-hook-form"
|
||||
import { z } from "zod"
|
||||
import { getTrade } from "@/actions/trade"
|
||||
import { DataTable, useDataTable } from "@/components/data-table"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import {
|
||||
Field,
|
||||
FieldError,
|
||||
FieldGroup,
|
||||
FieldLabel,
|
||||
} from "@/components/ui/field"
|
||||
import { Input } from "@/components/ui/input"
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from "@/components/ui/select"
|
||||
import type { Trade } from "@/models/trade"
|
||||
|
||||
type FilterValues = {
|
||||
inner_no?: string
|
||||
method?: number
|
||||
platform?: number
|
||||
status?: number
|
||||
created_at_start?: Date
|
||||
created_at_end?: Date
|
||||
}
|
||||
|
||||
const filterSchema = z
|
||||
.object({
|
||||
inner_no: z
|
||||
.string()
|
||||
.optional()
|
||||
.transform(val => val?.trim()),
|
||||
method: z.string().optional(),
|
||||
platform: z.string().optional(),
|
||||
status: z.string().optional(),
|
||||
created_at_start: z.string().optional(),
|
||||
created_at_end: z.string().optional(),
|
||||
})
|
||||
.superRefine((data, ctx) => {
|
||||
if (data.created_at_start && data.created_at_end) {
|
||||
const start = new Date(data.created_at_start)
|
||||
const end = new Date(data.created_at_end)
|
||||
if (end < start) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: "结束时间不能早于开始时间",
|
||||
path: ["created_at_end"],
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
type FilterSchema = z.infer<typeof filterSchema>
|
||||
|
||||
export default function TradePage() {
|
||||
const searchParams = useSearchParams()
|
||||
const userId = searchParams.get("userId")
|
||||
const [filters, setFilters] = useState<FilterValues>({})
|
||||
|
||||
const { control, handleSubmit, reset } = useForm<FilterSchema>({
|
||||
resolver: zodResolver(filterSchema),
|
||||
defaultValues: {
|
||||
inner_no: "",
|
||||
method: "all",
|
||||
platform: "all",
|
||||
status: "all",
|
||||
created_at_start: "",
|
||||
created_at_end: "",
|
||||
},
|
||||
})
|
||||
|
||||
const fetchTrades = useCallback(
|
||||
async (page: number, size: number) => {
|
||||
return getTrade({ page, size, user_id: Number(userId), ...filters })
|
||||
},
|
||||
[filters, userId],
|
||||
)
|
||||
|
||||
const table = useDataTable<Trade>(fetchTrades)
|
||||
|
||||
const onFilter = handleSubmit(data => {
|
||||
const result: FilterValues = {}
|
||||
if (data.inner_no?.trim()) result.inner_no = data.inner_no.trim()
|
||||
if (data.method && data.method !== "all")
|
||||
result.method = Number(data.method)
|
||||
if (data.platform && data.platform !== "all")
|
||||
result.platform = Number(data.platform)
|
||||
if (data.status && data.status !== "all")
|
||||
result.status = Number(data.status)
|
||||
if (data.created_at_start)
|
||||
result.created_at_start = new Date(data.created_at_start)
|
||||
if (data.created_at_end)
|
||||
result.created_at_end = new Date(data.created_at_end)
|
||||
setFilters(result)
|
||||
table.pagination.onPageChange(1)
|
||||
})
|
||||
|
||||
return (
|
||||
<div className="space-y-3">
|
||||
{/* 筛选表单 */}
|
||||
<form onSubmit={onFilter} className="bg-white p-4">
|
||||
<div className="flex flex-wrap items-end gap-4">
|
||||
<Controller
|
||||
name="inner_no"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-40 flex-none"
|
||||
>
|
||||
<FieldLabel>订单号</FieldLabel>
|
||||
<Input {...field} placeholder="请输入订单号" />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
|
||||
<Controller
|
||||
name="method"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field data-invalid={fieldState.invalid} className="w-24">
|
||||
<FieldLabel>支付渠道</FieldLabel>
|
||||
<Select value={field.value} onValueChange={field.onChange}>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder="全部" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="all">全部</SelectItem>
|
||||
<SelectItem value="1">支付宝</SelectItem>
|
||||
<SelectItem value="2">微信</SelectItem>
|
||||
<SelectItem value="3">商福通</SelectItem>
|
||||
<SelectItem value="4">商福通渠道支付宝</SelectItem>
|
||||
<SelectItem value="5">商福通渠道微信</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
|
||||
<Controller
|
||||
name="platform"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field data-invalid={fieldState.invalid} className="w-24">
|
||||
<FieldLabel>支付平台</FieldLabel>
|
||||
<Select value={field.value} onValueChange={field.onChange}>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder="全部" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="all">全部</SelectItem>
|
||||
<SelectItem value="1">电脑网站</SelectItem>
|
||||
<SelectItem value="2">手机网站</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
|
||||
<Controller
|
||||
name="status"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field data-invalid={fieldState.invalid} className="w-24">
|
||||
<FieldLabel>支付状态</FieldLabel>
|
||||
<Select value={field.value} onValueChange={field.onChange}>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder="全部" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="all">全部</SelectItem>
|
||||
<SelectItem value="0">待支付</SelectItem>
|
||||
<SelectItem value="1">支付成功</SelectItem>
|
||||
<SelectItem value="2">取消支付</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
|
||||
<Controller
|
||||
name="created_at_start"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-40 flex-none"
|
||||
>
|
||||
<FieldLabel>开始时间</FieldLabel>
|
||||
<Input type="date" {...field} />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
|
||||
<Controller
|
||||
name="created_at_end"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-40 flex-none"
|
||||
>
|
||||
<FieldLabel>结束时间</FieldLabel>
|
||||
<Input type="date" {...field} />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<FieldGroup className="flex-row justify-start mt-4 gap-2">
|
||||
<Button type="submit">筛选</Button>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
onClick={() => {
|
||||
reset()
|
||||
setFilters({})
|
||||
table.pagination.onPageChange(1)
|
||||
}}
|
||||
>
|
||||
重置
|
||||
</Button>
|
||||
</FieldGroup>
|
||||
</form>
|
||||
|
||||
<Suspense>
|
||||
<DataTable<Trade>
|
||||
{...table}
|
||||
columns={[
|
||||
{
|
||||
header: "订单号",
|
||||
accessorKey: "inner_no",
|
||||
},
|
||||
{
|
||||
header: "渠道订单号",
|
||||
accessorKey: "outer_no",
|
||||
},
|
||||
{
|
||||
header: "支付渠道",
|
||||
accessorKey: "method",
|
||||
cell: ({ row }) => {
|
||||
const methodMap: Record<number, string> = {
|
||||
1: "支付宝",
|
||||
2: "微信",
|
||||
3: "商福通",
|
||||
4: "商福通渠道支付宝",
|
||||
5: "商福通渠道微信",
|
||||
}
|
||||
return (
|
||||
<div>
|
||||
{methodMap[row.original.method as number] || "未知"}
|
||||
</div>
|
||||
)
|
||||
},
|
||||
},
|
||||
{
|
||||
header: "支付金额",
|
||||
accessorKey: "payment",
|
||||
cell: ({ row }) => {
|
||||
const payment =
|
||||
typeof row.original.payment === "string"
|
||||
? parseFloat(row.original.payment)
|
||||
: row.original.payment || 0
|
||||
return (
|
||||
<div className="flex gap-1">
|
||||
<span
|
||||
className={
|
||||
payment > 0 ? "text-green-500" : "text-orange-500"
|
||||
}
|
||||
>
|
||||
¥{payment.toFixed(2)}
|
||||
</span>
|
||||
</div>
|
||||
)
|
||||
},
|
||||
},
|
||||
{
|
||||
header: "支付平台",
|
||||
accessorKey: "platform",
|
||||
cell: ({ row }) => {
|
||||
const platform = row.original.platform
|
||||
if (!platform) return <span>-</span>
|
||||
return platform === 1
|
||||
? "电脑网站"
|
||||
: platform === 2
|
||||
? "手机网站"
|
||||
: "-"
|
||||
},
|
||||
},
|
||||
{
|
||||
header: "支付状态",
|
||||
accessorKey: "status",
|
||||
cell: ({ row }) => {
|
||||
const status = row.original.status
|
||||
switch (status) {
|
||||
case 0:
|
||||
return (
|
||||
<div className="flex items-center gap-2 text-yellow-600">
|
||||
<Clock className="h-4 w-4" />
|
||||
<span>待支付</span>
|
||||
</div>
|
||||
)
|
||||
case 1:
|
||||
return (
|
||||
<div className="flex items-center gap-2 text-green-600">
|
||||
<CheckCircle className="h-4 w-4" />
|
||||
<span>支付成功</span>
|
||||
</div>
|
||||
)
|
||||
case 2:
|
||||
return (
|
||||
<div className="flex items-center gap-2 text-gray-500">
|
||||
<XCircle className="h-4 w-4" />
|
||||
<span>取消支付</span>
|
||||
</div>
|
||||
)
|
||||
default:
|
||||
return <span className="text-gray-400">-</span>
|
||||
}
|
||||
},
|
||||
},
|
||||
{ header: "购买套餐", accessorKey: "subject" },
|
||||
{
|
||||
header: "创建时间",
|
||||
accessorKey: "created_at",
|
||||
cell: ({ row }) =>
|
||||
format(new Date(row.original.created_at), "yyyy-MM-dd HH:mm"),
|
||||
},
|
||||
]}
|
||||
/>
|
||||
</Suspense>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -270,17 +270,6 @@ export function AddUserDialog({
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
<Controller
|
||||
name="name"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field data-invalid={fieldState.invalid}>
|
||||
<FieldLabel>真实姓名</FieldLabel>
|
||||
<Input {...field} placeholder="请输入真实姓名" />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="email"
|
||||
control={control}
|
||||
@@ -292,9 +281,6 @@ export function AddUserDialog({
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
<Controller
|
||||
name="status"
|
||||
control={control}
|
||||
|
||||
144
src/app/(root)/cust/deduction.tsx
Normal file
144
src/app/(root)/cust/deduction.tsx
Normal file
@@ -0,0 +1,144 @@
|
||||
"use client"
|
||||
|
||||
import { zodResolver } from "@hookform/resolvers/zod"
|
||||
import { useState } from "react"
|
||||
import { useForm } from "react-hook-form"
|
||||
import { toast } from "sonner"
|
||||
import { z } from "zod"
|
||||
import { getDeduction } from "@/actions/cust"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from "@/components/ui/dialog"
|
||||
import { Field, FieldError, FieldLabel } from "@/components/ui/field"
|
||||
import { Input } from "@/components/ui/input"
|
||||
import type { User } from "@/models/user"
|
||||
|
||||
const Schema = z.object({
|
||||
deduction: z
|
||||
.string()
|
||||
.min(1, "请输入扣款金额")
|
||||
.refine(val => !Number.isNaN(Number(val)), "请输入有效的数字")
|
||||
.refine(val => Number(val) >= 0, "金额不能为负数"),
|
||||
})
|
||||
|
||||
type FormValues = z.infer<typeof Schema>
|
||||
|
||||
interface UpdateDeductionDialogProps {
|
||||
open: boolean
|
||||
onOpenChange: (open: boolean) => void
|
||||
currentUser: User | null
|
||||
onSuccess: () => void
|
||||
}
|
||||
|
||||
export function DeductionDialog({
|
||||
open,
|
||||
onOpenChange,
|
||||
currentUser,
|
||||
onSuccess,
|
||||
}: UpdateDeductionDialogProps) {
|
||||
const [isLoading, setIsLoading] = useState(false)
|
||||
|
||||
const {
|
||||
register,
|
||||
handleSubmit,
|
||||
reset,
|
||||
setValue,
|
||||
formState: { errors },
|
||||
} = useForm<FormValues>({
|
||||
resolver: zodResolver(Schema),
|
||||
defaultValues: {
|
||||
deduction: "",
|
||||
},
|
||||
})
|
||||
|
||||
const onSubmit = async (data: FormValues) => {
|
||||
if (!currentUser) return
|
||||
setIsLoading(true)
|
||||
try {
|
||||
const result = await getDeduction({
|
||||
user_id: currentUser.id,
|
||||
amount: data.deduction,
|
||||
})
|
||||
|
||||
if (result.success) {
|
||||
toast.success("扣款成功")
|
||||
onOpenChange(false)
|
||||
reset()
|
||||
onSuccess()
|
||||
} else {
|
||||
toast.error(result.message || "扣款失败")
|
||||
}
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : error
|
||||
toast.error(`网络错误,请稍后重试: ${message}`)
|
||||
} finally {
|
||||
setIsLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
const handleOpenChange = (open: boolean) => {
|
||||
if (!open) {
|
||||
reset()
|
||||
}
|
||||
onOpenChange(open)
|
||||
}
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={handleOpenChange}>
|
||||
<DialogContent className="max-w-2xl max-h-[90vh] overflow-y-auto">
|
||||
<DialogHeader>
|
||||
<DialogTitle>扣款</DialogTitle>
|
||||
<DialogDescription>
|
||||
扣减用户 {currentUser?.name || currentUser?.username} 的余额
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<form onSubmit={handleSubmit(onSubmit)}>
|
||||
<div className="grid gap-4 py-4">
|
||||
<Field data-invalid={!!errors.deduction}>
|
||||
<FieldLabel>扣款(元)</FieldLabel>
|
||||
<Input
|
||||
type="text"
|
||||
placeholder="请输入扣款金额"
|
||||
{...register("deduction")}
|
||||
onInput={(e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
let value = e.target.value
|
||||
value = value.replace(/[^\d.]/g, "")
|
||||
const dotCount = (value.match(/\./g) || []).length
|
||||
if (dotCount > 1) {
|
||||
value = value.slice(0, value.lastIndexOf("."))
|
||||
}
|
||||
if (value.includes(".")) {
|
||||
const [int, dec] = value.split(".")
|
||||
value = `${int}.${dec.slice(0, 2)}`
|
||||
}
|
||||
setValue("deduction", value)
|
||||
}}
|
||||
/>
|
||||
<FieldError>{errors.deduction?.message}</FieldError>
|
||||
</Field>
|
||||
</div>
|
||||
|
||||
<DialogFooter>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
onClick={() => handleOpenChange(false)}
|
||||
>
|
||||
取消
|
||||
</Button>
|
||||
<Button type="submit" disabled={isLoading}>
|
||||
{isLoading ? "保存中" : "保存"}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</form>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
"use client"
|
||||
|
||||
import { zodResolver } from "@hookform/resolvers/zod"
|
||||
import { useEffect, useState } from "react"
|
||||
import { useState } from "react"
|
||||
import { useForm } from "react-hook-form"
|
||||
import { toast } from "sonner"
|
||||
import { z } from "zod"
|
||||
import { getBalance } from "@/actions/cust"
|
||||
import { getDeposit } from "@/actions/cust"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import {
|
||||
Dialog,
|
||||
@@ -17,31 +17,31 @@ import {
|
||||
} from "@/components/ui/dialog"
|
||||
import { Field, FieldError, FieldLabel } from "@/components/ui/field"
|
||||
import { Input } from "@/components/ui/input"
|
||||
import type { Cust } from "@/models/cust"
|
||||
import type { User } from "@/models/user"
|
||||
|
||||
const balanceSchema = z.object({
|
||||
balance: z
|
||||
const Schema = z.object({
|
||||
deposit: z
|
||||
.string()
|
||||
.min(1, "请输入余额")
|
||||
.refine(val => !Number.isNaN(Number(val)), "请输入有效的数字")
|
||||
.refine(val => Number(val) >= 0, "余额不能为负数"),
|
||||
})
|
||||
|
||||
type BalanceFormValues = z.infer<typeof balanceSchema>
|
||||
type FormValues = z.infer<typeof Schema>
|
||||
|
||||
interface UpdateBalanceDialogProps {
|
||||
interface UpdateDepositDialogProps {
|
||||
open: boolean
|
||||
onOpenChange: (open: boolean) => void
|
||||
currentUser: Cust | null
|
||||
currentUser: User | null
|
||||
onSuccess: () => void
|
||||
}
|
||||
|
||||
export function BalanceDialog({
|
||||
export function DepositDialog({
|
||||
open,
|
||||
onOpenChange,
|
||||
currentUser,
|
||||
onSuccess,
|
||||
}: UpdateBalanceDialogProps) {
|
||||
}: UpdateDepositDialogProps) {
|
||||
const [isLoading, setIsLoading] = useState(false)
|
||||
|
||||
const {
|
||||
@@ -50,37 +50,29 @@ export function BalanceDialog({
|
||||
reset,
|
||||
setValue,
|
||||
formState: { errors },
|
||||
} = useForm<BalanceFormValues>({
|
||||
resolver: zodResolver(balanceSchema),
|
||||
} = useForm<FormValues>({
|
||||
resolver: zodResolver(Schema),
|
||||
defaultValues: {
|
||||
balance: "",
|
||||
deposit: "",
|
||||
},
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
if (open && currentUser) {
|
||||
const currentBalance = currentUser.balance?.toString() || "0"
|
||||
const formattedBalance = Number(currentBalance).toFixed(2)
|
||||
setValue("balance", formattedBalance)
|
||||
}
|
||||
}, [open, currentUser, setValue])
|
||||
|
||||
const onSubmit = async (data: BalanceFormValues) => {
|
||||
const onSubmit = async (data: FormValues) => {
|
||||
if (!currentUser) return
|
||||
setIsLoading(true)
|
||||
try {
|
||||
const result = await getBalance({
|
||||
const result = await getDeposit({
|
||||
user_id: currentUser.id,
|
||||
balance: data.balance,
|
||||
amount: data.deposit,
|
||||
})
|
||||
|
||||
if (result.success) {
|
||||
toast.success("修改余额成功")
|
||||
toast.success("充值成功")
|
||||
onOpenChange(false)
|
||||
reset()
|
||||
onSuccess()
|
||||
} else {
|
||||
toast.error(result.message || "修改余额失败")
|
||||
toast.error(result.message || "充值失败")
|
||||
}
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : error
|
||||
@@ -101,45 +93,35 @@ export function BalanceDialog({
|
||||
<Dialog open={open} onOpenChange={handleOpenChange}>
|
||||
<DialogContent className="max-w-2xl max-h-[90vh] overflow-y-auto">
|
||||
<DialogHeader>
|
||||
<DialogTitle>修改余额</DialogTitle>
|
||||
<DialogTitle>充值</DialogTitle>
|
||||
<DialogDescription>
|
||||
修改用户 {currentUser?.name || currentUser?.username} 的余额
|
||||
充值用户 {currentUser?.name || currentUser?.username} 的余额
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<form onSubmit={handleSubmit(onSubmit)}>
|
||||
<div className="grid gap-4 py-4">
|
||||
<Field data-invalid={!!errors.balance}>
|
||||
<FieldLabel>余额(元)</FieldLabel>
|
||||
<Field data-invalid={!!errors.deposit}>
|
||||
<FieldLabel>充值(元)</FieldLabel>
|
||||
<Input
|
||||
type="number"
|
||||
step="0.01"
|
||||
min="0"
|
||||
placeholder="请输入余额"
|
||||
{...register("balance", {
|
||||
setValueAs: value => {
|
||||
if (!value) return ""
|
||||
const num = Number(value)
|
||||
if (Number.isNaN(num)) return value
|
||||
return num.toFixed(2)
|
||||
},
|
||||
})}
|
||||
type="text" // 改为 text,避免 number 输入冲突
|
||||
placeholder="请输入充值金额"
|
||||
{...register("deposit")}
|
||||
onInput={(e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
let value = e.target.value
|
||||
if (value.startsWith("-")) {
|
||||
value = value.replace("-", "")
|
||||
value = value.replace(/[^\d.]/g, "")
|
||||
const dotCount = (value.match(/\./g) || []).length
|
||||
if (dotCount > 1) {
|
||||
value = value.slice(0, value.lastIndexOf("."))
|
||||
}
|
||||
if (value.includes(".")) {
|
||||
const parts = value.split(".")
|
||||
if (parts[1] && parts[1].length > 2) {
|
||||
value = `${parts[0]}.${parts[1].slice(0, 2)}`
|
||||
}
|
||||
const [int, dec] = value.split(".")
|
||||
value = `${int}.${dec.slice(0, 2)}`
|
||||
}
|
||||
|
||||
setValue("balance", value)
|
||||
setValue("deposit", value)
|
||||
}}
|
||||
/>
|
||||
<FieldError>{errors.balance?.message}</FieldError>
|
||||
<FieldError>{errors.deposit?.message}</FieldError>
|
||||
</Field>
|
||||
</div>
|
||||
|
||||
@@ -2,10 +2,12 @@
|
||||
|
||||
import { zodResolver } from "@hookform/resolvers/zod"
|
||||
import { format } from "date-fns"
|
||||
import { useRouter } from "next/navigation"
|
||||
import { Suspense, useCallback, useState } from "react"
|
||||
import { Controller, useForm } from "react-hook-form"
|
||||
import { z } from "zod"
|
||||
import { getPageCusts } from "@/actions/cust"
|
||||
import { Auth } from "@/components/auth"
|
||||
import { DataTable, useDataTable } from "@/components/data-table"
|
||||
import { Badge } from "@/components/ui/badge"
|
||||
import { Button } from "@/components/ui/button"
|
||||
@@ -23,9 +25,22 @@ import {
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from "@/components/ui/select"
|
||||
import type { Cust } from "@/models/cust"
|
||||
import { BalanceDialog } from "./balanceDialog"
|
||||
import {
|
||||
ScopeBalanceActivityReadOfUser,
|
||||
ScopeBatchReadOfUser,
|
||||
ScopeBillReadOfUser,
|
||||
ScopeChannelReadOfUser,
|
||||
ScopeResourceRead,
|
||||
ScopeTradeReadOfUser,
|
||||
ScopeUserWrite,
|
||||
ScopeUserWriteBalance,
|
||||
ScopeUserWriteBalanceDec,
|
||||
ScopeUserWriteBalanceInc,
|
||||
} from "@/lib/scopes"
|
||||
import type { User } from "@/models/user"
|
||||
import { AddUserDialog } from "./create"
|
||||
import { DeductionDialog } from "./deduction"
|
||||
import { DepositDialog } from "./deposit"
|
||||
import { UpdateDialog } from "./update"
|
||||
|
||||
type FilterValues = {
|
||||
@@ -63,13 +78,16 @@ const filterSchema = z
|
||||
|
||||
type FormValues = z.infer<typeof filterSchema>
|
||||
|
||||
export default function UserPage() {
|
||||
export default function CustPage() {
|
||||
const [filters, setFilters] = useState<FilterValues>({})
|
||||
const [isAddDialogOpen, setIsAddDialogOpen] = useState(false)
|
||||
const [isEditDialogOpen, setIsEditDialogOpen] = useState(false)
|
||||
const [currentEditUser, setCurrentEditUser] = useState<Cust | null>(null)
|
||||
const [balanceDialog, setBalanceDialog] = useState(false)
|
||||
const [balance, setBalance] = useState<Cust | null>(null)
|
||||
const [currentEditUser, setCurrentEditUser] = useState<User | null>(null)
|
||||
const [depositDialog, setDepositDialog] = useState(false)
|
||||
const [deposit, setDeposit] = useState<User | null>(null)
|
||||
const router = useRouter()
|
||||
const [deductionDialog, setDeductionDialog] = useState(false)
|
||||
const [deduction, setDeduction] = useState<User | null>(null)
|
||||
const { control, handleSubmit, reset } = useForm<FormValues>({
|
||||
resolver: zodResolver(filterSchema),
|
||||
defaultValues: {
|
||||
@@ -87,7 +105,7 @@ export default function UserPage() {
|
||||
[filters],
|
||||
)
|
||||
|
||||
const table = useDataTable<Cust>(fetchUsers)
|
||||
const table = useDataTable<User>(fetchUsers)
|
||||
|
||||
const onFilter = handleSubmit(data => {
|
||||
const result: FilterValues = {}
|
||||
@@ -212,7 +230,11 @@ export default function UserPage() {
|
||||
</div>
|
||||
|
||||
<FieldGroup className="flex-row justify-start mt-4 gap-2">
|
||||
<Button type="submit">筛选</Button>
|
||||
<Auth scope={ScopeUserWrite}>
|
||||
<Button type="button" onClick={() => setIsAddDialogOpen(true)}>
|
||||
添加用户
|
||||
</Button>
|
||||
</Auth>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
@@ -224,20 +246,22 @@ export default function UserPage() {
|
||||
>
|
||||
重置
|
||||
</Button>
|
||||
<Button type="button" onClick={() => setIsAddDialogOpen(true)}>
|
||||
添加用户
|
||||
</Button>
|
||||
<Button type="submit">筛选</Button>
|
||||
</FieldGroup>
|
||||
</form>
|
||||
|
||||
<Suspense>
|
||||
<DataTable<Cust>
|
||||
<DataTable<User>
|
||||
{...table}
|
||||
columns={[
|
||||
{ header: "账号", accessorKey: "username" },
|
||||
{ header: "手机", accessorKey: "phone" },
|
||||
{ header: "邮箱", accessorKey: "email" },
|
||||
{ header: "姓名", accessorKey: "name" },
|
||||
{
|
||||
header: "创建时间",
|
||||
accessorKey: "created_at",
|
||||
cell: ({ row }) =>
|
||||
format(new Date(row.original.created_at), "yyyy-MM-dd HH:mm"),
|
||||
},
|
||||
// { header: "邮箱", accessorKey: "email" },
|
||||
{
|
||||
header: "客户来源",
|
||||
accessorKey: "source",
|
||||
@@ -267,7 +291,22 @@ export default function UserPage() {
|
||||
)
|
||||
},
|
||||
},
|
||||
{ header: "折扣", accessorKey: "discount.name" },
|
||||
{ header: "账号", accessorKey: "username" },
|
||||
{
|
||||
header: "账号状态",
|
||||
accessorKey: "status",
|
||||
cell: ({ row }) => (row.original.status === 1 ? "正常" : "禁用"),
|
||||
},
|
||||
{ header: "客户经理", accessorKey: "admin.name" },
|
||||
{ header: "姓名", accessorKey: "name" },
|
||||
{
|
||||
header: "身份证号",
|
||||
accessorKey: "id_no",
|
||||
cell: ({ row }) => {
|
||||
const idNo = row.original.id_no
|
||||
return idNo ? `${idNo.slice(0, 6)}****${idNo.slice(-4)}` : ""
|
||||
},
|
||||
},
|
||||
{
|
||||
header: "实名状态",
|
||||
accessorKey: "id_type",
|
||||
@@ -285,18 +324,22 @@ export default function UserPage() {
|
||||
),
|
||||
},
|
||||
{
|
||||
header: "身份证号",
|
||||
accessorKey: "id_no",
|
||||
cell: ({ row }) => {
|
||||
const idNo = row.original.id_no
|
||||
return idNo ? `${idNo.slice(0, 6)}****${idNo.slice(-4)}` : "-"
|
||||
},
|
||||
header: "最后登录时间",
|
||||
accessorKey: "last_login",
|
||||
cell: ({ row }) =>
|
||||
row.original.last_login
|
||||
? format(
|
||||
new Date(row.original.last_login),
|
||||
"yyyy-MM-dd HH:mm",
|
||||
)
|
||||
: "",
|
||||
},
|
||||
{
|
||||
header: "账号状态",
|
||||
accessorKey: "status",
|
||||
cell: ({ row }) => (row.original.status === 1 ? "正常" : "禁用"),
|
||||
header: "最后登录IP",
|
||||
accessorKey: "last_login_ip",
|
||||
cell: ({ row }) => row.original.last_login_ip || "",
|
||||
},
|
||||
{ header: "折扣", accessorKey: "discount.name" },
|
||||
{
|
||||
header: "联系方式",
|
||||
cell: ({ row }) => {
|
||||
@@ -313,55 +356,114 @@ export default function UserPage() {
|
||||
)
|
||||
},
|
||||
},
|
||||
{ header: "客户经理", accessorKey: "admin.name" },
|
||||
{
|
||||
header: "最后登录时间",
|
||||
accessorKey: "last_login",
|
||||
cell: ({ row }) =>
|
||||
row.original.last_login
|
||||
? format(
|
||||
new Date(row.original.last_login),
|
||||
"yyyy-MM-dd HH:mm",
|
||||
)
|
||||
: "-",
|
||||
},
|
||||
{
|
||||
header: "最后登录IP",
|
||||
accessorKey: "last_login_ip",
|
||||
cell: ({ row }) => row.original.last_login_ip || "-",
|
||||
},
|
||||
{
|
||||
header: "创建时间",
|
||||
accessorKey: "created_at",
|
||||
cell: ({ row }) =>
|
||||
format(new Date(row.original.created_at), "yyyy-MM-dd HH:mm"),
|
||||
},
|
||||
{
|
||||
id: "action",
|
||||
meta: { pin: "right" },
|
||||
header: "操作",
|
||||
cell: ({ row }) => {
|
||||
return (
|
||||
<div className="flex gap-2">
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
onClick={() => {
|
||||
setBalance(row.original)
|
||||
setBalanceDialog(true)
|
||||
}}
|
||||
>
|
||||
修改余额
|
||||
</Button>
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
setCurrentEditUser(row.original)
|
||||
setIsEditDialogOpen(true)
|
||||
}}
|
||||
>
|
||||
修改
|
||||
</Button>
|
||||
<div className="flex flex-wrap gap-2 w-75">
|
||||
<Auth scope={ScopeUserWriteBalanceInc}>
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
setDeposit(row.original)
|
||||
setDepositDialog(true)
|
||||
}}
|
||||
>
|
||||
充值
|
||||
</Button>
|
||||
</Auth>
|
||||
<Auth scope={ScopeUserWriteBalanceDec}>
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
setDeduction(row.original)
|
||||
setDeductionDialog(true)
|
||||
}}
|
||||
>
|
||||
扣款
|
||||
</Button>
|
||||
</Auth>
|
||||
<Auth scope={ScopeUserWriteBalance}>
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
setCurrentEditUser(row.original)
|
||||
setIsEditDialogOpen(true)
|
||||
}}
|
||||
>
|
||||
修改
|
||||
</Button>
|
||||
</Auth>
|
||||
<Auth scope={ScopeTradeReadOfUser}>
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
router.push(`/client/trade?userId=${row.original.id}`)
|
||||
}}
|
||||
>
|
||||
交易明细
|
||||
</Button>
|
||||
</Auth>
|
||||
<Auth scope={ScopeBillReadOfUser}>
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
router.push(
|
||||
`/client/billing?userId=${row.original.id}`,
|
||||
)
|
||||
}}
|
||||
>
|
||||
账单详情
|
||||
</Button>
|
||||
</Auth>
|
||||
<Auth scope={ScopeResourceRead}>
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
router.push(
|
||||
`/client/resources?userId=${row.original.id}`,
|
||||
)
|
||||
}}
|
||||
>
|
||||
套餐管理
|
||||
</Button>
|
||||
</Auth>
|
||||
<Auth scope={ScopeBatchReadOfUser}>
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
router.push(`/client/batch?userId=${row.original.id}`)
|
||||
}}
|
||||
>
|
||||
提取记录
|
||||
</Button>
|
||||
</Auth>
|
||||
<Auth scope={ScopeChannelReadOfUser}>
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
router.push(
|
||||
`/client/channel?userId=${row.original.id}`,
|
||||
)
|
||||
}}
|
||||
>
|
||||
IP管理
|
||||
</Button>
|
||||
</Auth>
|
||||
<Auth scope={ScopeBalanceActivityReadOfUser}>
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
router.push(
|
||||
`/client/balance?userId=${row.original.id}&phone=${row.original.phone}`,
|
||||
)
|
||||
}}
|
||||
>
|
||||
余额操作
|
||||
</Button>
|
||||
</Auth>
|
||||
</div>
|
||||
)
|
||||
},
|
||||
@@ -383,10 +485,16 @@ export default function UserPage() {
|
||||
onSuccess={refreshTable}
|
||||
/>
|
||||
|
||||
<BalanceDialog
|
||||
open={balanceDialog}
|
||||
onOpenChange={setBalanceDialog}
|
||||
currentUser={balance}
|
||||
<DepositDialog
|
||||
open={depositDialog}
|
||||
onOpenChange={setDepositDialog}
|
||||
currentUser={deposit}
|
||||
onSuccess={refreshTable}
|
||||
/>
|
||||
<DeductionDialog
|
||||
open={deductionDialog}
|
||||
onOpenChange={setDeductionDialog}
|
||||
currentUser={deduction}
|
||||
onSuccess={refreshTable}
|
||||
/>
|
||||
</div>
|
||||
|
||||
@@ -30,7 +30,7 @@ import {
|
||||
SelectValue,
|
||||
} from "@/components/ui/select"
|
||||
import type { Admin } from "@/models/admin"
|
||||
import type { Cust } from "@/models/cust"
|
||||
import type { User } from "@/models/user"
|
||||
import type { ProductDiscount } from "@/models/product_discount"
|
||||
|
||||
// 表单验证规则
|
||||
@@ -69,7 +69,7 @@ export type EditUserFormValues = z.infer<typeof editUserSchema>
|
||||
interface EditUserDialogProps {
|
||||
open: boolean
|
||||
onOpenChange: (open: boolean) => void
|
||||
currentUser: Cust | null
|
||||
currentUser: User | null
|
||||
onSuccess: () => void
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ async function Layout(props: { children: ReactNode }) {
|
||||
const profile = await getProfile()
|
||||
if (!profile.success) throw new Error("页面渲染失败:无法获取账号信息")
|
||||
return (
|
||||
<div className="flex h-screen bg-gray-100">
|
||||
<div className="flex h-screen overflow-hidden bg-gray-100">
|
||||
<SetScopes admin={profile.data} />
|
||||
|
||||
{/* 侧边栏 */}
|
||||
|
||||
@@ -4,15 +4,17 @@ import {
|
||||
BarChart3,
|
||||
ChevronsLeft,
|
||||
ChevronsRight,
|
||||
CircleDollarSign,
|
||||
ClipboardList,
|
||||
Code,
|
||||
ComputerIcon,
|
||||
ContactRound,
|
||||
DollarSign,
|
||||
FolderCode,
|
||||
Home,
|
||||
KeyRound,
|
||||
type LucideIcon,
|
||||
Package,
|
||||
ScanSearch,
|
||||
Shield,
|
||||
ShoppingBag,
|
||||
SquarePercent,
|
||||
@@ -36,6 +38,7 @@ import {
|
||||
import {
|
||||
ScopeAdminRead,
|
||||
ScopeAdminRoleRead,
|
||||
ScopeBalanceActivity,
|
||||
ScopeBatchRead,
|
||||
ScopeBillRead,
|
||||
ScopeChannelRead,
|
||||
@@ -46,6 +49,7 @@ import {
|
||||
ScopeResourceRead,
|
||||
ScopeTradeRead,
|
||||
ScopeUserRead,
|
||||
ScopeUserReadNotBind,
|
||||
ScopeUserReadOne,
|
||||
} from "@/lib/scopes"
|
||||
|
||||
@@ -98,8 +102,6 @@ interface NavItemProps {
|
||||
}
|
||||
|
||||
function NavItem({ href, icon: Icon, label, requiredScope }: NavItemProps) {
|
||||
// console.log(requiredScope, "requiredScope")
|
||||
|
||||
const { collapsed, isActive } = useNavigation()
|
||||
const active = isActive(href)
|
||||
|
||||
@@ -169,6 +171,12 @@ const menuSections: { title: string; items: NavItemProps[] }[] = [
|
||||
href: "/user",
|
||||
icon: Users,
|
||||
label: "客户认领",
|
||||
requiredScope: ScopeUserReadNotBind,
|
||||
},
|
||||
{
|
||||
href: "/client/cust",
|
||||
icon: ScanSearch,
|
||||
label: "客户查询",
|
||||
requiredScope: ScopeUserReadOne,
|
||||
},
|
||||
{
|
||||
@@ -183,6 +191,12 @@ const menuSections: { title: string; items: NavItemProps[] }[] = [
|
||||
label: "交易明细",
|
||||
requiredScope: ScopeTradeRead,
|
||||
},
|
||||
{
|
||||
href: "/balance",
|
||||
icon: CircleDollarSign,
|
||||
label: "余额明细",
|
||||
requiredScope: ScopeBalanceActivity,
|
||||
},
|
||||
{
|
||||
href: "/billing",
|
||||
icon: DollarSign,
|
||||
@@ -226,7 +240,7 @@ const menuSections: { title: string; items: NavItemProps[] }[] = [
|
||||
},
|
||||
{
|
||||
href: "/channel",
|
||||
icon: Code,
|
||||
icon: FolderCode,
|
||||
label: "IP管理",
|
||||
requiredScope: ScopeChannelRead,
|
||||
},
|
||||
@@ -261,9 +275,11 @@ const menuSections: { title: string; items: NavItemProps[] }[] = [
|
||||
export default function Navigation() {
|
||||
const [collapsed, setCollapsed] = useState(false)
|
||||
const pathname = usePathname()
|
||||
|
||||
const isActive = (path: string) => {
|
||||
return path === "/" ? pathname === path : pathname.startsWith(path)
|
||||
if (path === "/") {
|
||||
return pathname === path
|
||||
}
|
||||
return pathname === path || pathname.startsWith(path + "/")
|
||||
}
|
||||
|
||||
const contextValue: NavigationContextType = {
|
||||
@@ -277,12 +293,12 @@ export default function Navigation() {
|
||||
<NavigationContext.Provider value={contextValue}>
|
||||
<aside
|
||||
className={twJoin(
|
||||
"bg-background border-r border-border transition-all duration-300 ease-in-out flex flex-col",
|
||||
"bg-background border-r border-border transition-all duration-300 ease-in-out flex flex-col h-full",
|
||||
collapsed ? "w-16" : "w-64",
|
||||
)}
|
||||
>
|
||||
{/* Logo */}
|
||||
<div className="h-16 flex items-center justify-center border-b border-border">
|
||||
{/*Logo 区域 */}
|
||||
<div className="h-16 flex items-center justify-center border-b border-border p-4 shrink-0">
|
||||
{!collapsed ? (
|
||||
<span className="text-xl font-bold tracking-wide text-foreground">
|
||||
管理系统
|
||||
@@ -294,7 +310,7 @@ export default function Navigation() {
|
||||
)}
|
||||
</div>
|
||||
{/* Navigation Menu */}
|
||||
<ScrollArea className="flex-1 py-3">
|
||||
<ScrollArea className="flex-1 py-3 overflow-hidden">
|
||||
<nav className="space-y-3">
|
||||
{menuSections.map((section, idx) => (
|
||||
<div key={section.title}>
|
||||
@@ -309,8 +325,7 @@ export default function Navigation() {
|
||||
</nav>
|
||||
</ScrollArea>
|
||||
|
||||
{/* Toggle Button */}
|
||||
<div className="p-4 border-t border-border mt-auto">
|
||||
<div className="p-4 border-t border-border mt-auto shrink-0">
|
||||
<Button
|
||||
variant="ghost"
|
||||
onClick={() => setCollapsed(!collapsed)}
|
||||
|
||||
@@ -45,6 +45,14 @@ const schema = z.object({
|
||||
"请输入有效的正数单价",
|
||||
),
|
||||
discount_id: z.string().optional(),
|
||||
price_min: z
|
||||
.string()
|
||||
.optional()
|
||||
.or(z.literal(""))
|
||||
.refine(
|
||||
v => !v || (!Number.isNaN(Number(v)) && Number(v) > 0),
|
||||
"请输入有效的正数价格",
|
||||
),
|
||||
})
|
||||
|
||||
export function CreateProductSku(props: {
|
||||
@@ -61,6 +69,7 @@ export function CreateProductSku(props: {
|
||||
name: "",
|
||||
price: "",
|
||||
discount_id: "",
|
||||
price_min: "",
|
||||
},
|
||||
})
|
||||
|
||||
@@ -89,7 +98,9 @@ export function CreateProductSku(props: {
|
||||
data.discount_id && data.discount_id !== ""
|
||||
? Number(data.discount_id)
|
||||
: undefined,
|
||||
price_min: data.price_min,
|
||||
})
|
||||
|
||||
if (resp.success) {
|
||||
form.reset()
|
||||
toast.success("套餐创建成功")
|
||||
@@ -162,6 +173,25 @@ export function CreateProductSku(props: {
|
||||
)}
|
||||
/>
|
||||
|
||||
<Controller
|
||||
control={form.control}
|
||||
name="price_min"
|
||||
render={({ field, fieldState }) => (
|
||||
<Field>
|
||||
<FieldLabel htmlFor="sku-create-price">最低价格</FieldLabel>
|
||||
<Input
|
||||
id="sku-create-price"
|
||||
placeholder="请输入单价"
|
||||
{...field}
|
||||
aria-invalid={fieldState.invalid}
|
||||
/>
|
||||
{fieldState.invalid && (
|
||||
<FieldError errors={[fieldState.error]} />
|
||||
)}
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
|
||||
<Controller
|
||||
control={form.control}
|
||||
name="discount_id"
|
||||
|
||||
@@ -1,26 +1,16 @@
|
||||
"use client"
|
||||
|
||||
import { format } from "date-fns"
|
||||
import { Loader2 } from "lucide-react"
|
||||
import { Suspense, useCallback, useEffect, useMemo, useState } from "react"
|
||||
import { toast } from "sonner"
|
||||
import {
|
||||
deleteProductSku,
|
||||
activeProductSku,
|
||||
getAllProduct,
|
||||
getPageProductSku,
|
||||
} from "@/actions/product"
|
||||
import { DataTable, useDataTable } from "@/components/data-table"
|
||||
import { SkuCodeBadge } from "@/components/products/format"
|
||||
import {
|
||||
AlertDialog,
|
||||
AlertDialogAction,
|
||||
AlertDialogCancel,
|
||||
AlertDialogContent,
|
||||
AlertDialogDescription,
|
||||
AlertDialogFooter,
|
||||
AlertDialogHeader,
|
||||
AlertDialogTitle,
|
||||
AlertDialogTrigger,
|
||||
} from "@/components/ui/alert-dialog"
|
||||
import { Badge } from "@/components/ui/badge"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import type { ProductCode } from "@/lib/base"
|
||||
@@ -108,6 +98,7 @@ function ProductSkus(props: {
|
||||
)
|
||||
|
||||
const table = useDataTable(action)
|
||||
console.log(table, "table")
|
||||
|
||||
return (
|
||||
<div className="flex-auto overflow-hidden flex flex-col items-stretch gap-3">
|
||||
@@ -149,6 +140,7 @@ function ProductSkus(props: {
|
||||
return Number(value.toFixed(2))
|
||||
},
|
||||
},
|
||||
{ header: "最低价格", accessorKey: "price_min" },
|
||||
{
|
||||
header: "创建时间",
|
||||
accessorFn: row => format(row.created_at, "yyyy-MM-dd HH:mm"),
|
||||
@@ -167,7 +159,7 @@ function ProductSkus(props: {
|
||||
sku={row.original}
|
||||
onSuccess={table.refresh}
|
||||
/>
|
||||
<DeleteButton sku={row.original} onSuccess={table.refresh} />
|
||||
<ActiveButton sku={row.original} onSuccess={table.refresh} />
|
||||
</div>
|
||||
),
|
||||
},
|
||||
@@ -178,18 +170,22 @@ function ProductSkus(props: {
|
||||
)
|
||||
}
|
||||
|
||||
function DeleteButton(props: { sku: ProductSku; onSuccess?: () => void }) {
|
||||
function ActiveButton(props: { sku: ProductSku; onSuccess?: () => void }) {
|
||||
const [loading, setLoading] = useState(false)
|
||||
|
||||
const handleConfirm = async () => {
|
||||
setLoading(true)
|
||||
try {
|
||||
const resp = await deleteProductSku(props.sku.id)
|
||||
const newStatus = props.sku.status === 1 ? 0 : 1
|
||||
const resp = await activeProductSku({
|
||||
id: props.sku.id,
|
||||
status: newStatus,
|
||||
})
|
||||
if (resp.success) {
|
||||
toast.success("删除成功")
|
||||
toast.success(newStatus === 1 ? "已启用" : "已禁用")
|
||||
props.onSuccess?.()
|
||||
} else {
|
||||
toast.error(resp.message ?? "删除失败")
|
||||
toast.error(resp.message ?? "操作失败")
|
||||
}
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : error
|
||||
@@ -200,26 +196,17 @@ function DeleteButton(props: { sku: ProductSku; onSuccess?: () => void }) {
|
||||
}
|
||||
|
||||
return (
|
||||
<AlertDialog>
|
||||
<AlertDialogTrigger asChild>
|
||||
<Button size="sm" variant="destructive" disabled={loading}>
|
||||
删除
|
||||
</Button>
|
||||
</AlertDialogTrigger>
|
||||
<AlertDialogContent size="sm">
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>确认删除</AlertDialogTitle>
|
||||
<AlertDialogDescription>
|
||||
确定要删除套餐「{props.sku.name}」吗?此操作不可撤销。
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel>取消</AlertDialogCancel>
|
||||
<AlertDialogAction variant="destructive" onClick={handleConfirm}>
|
||||
删除
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
<Button
|
||||
size="sm"
|
||||
variant={props.sku?.status === 1 ? "outline" : "default"}
|
||||
onClick={handleConfirm}
|
||||
disabled={loading}
|
||||
>
|
||||
{loading ? (
|
||||
<Loader2 className="h-4 w-4 animate-spin" />
|
||||
) : (
|
||||
<>{props.sku?.status === 1 ? "禁用" : "启用"}</>
|
||||
)}
|
||||
</Button>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -45,6 +45,13 @@ const schema = z.object({
|
||||
"请输入有效的正数单价",
|
||||
),
|
||||
discount_id: z.string().optional(),
|
||||
price_min: z
|
||||
.string()
|
||||
.min(1, "请输入最低价格")
|
||||
.refine(
|
||||
v => !Number.isNaN(Number(v)) && Number(v) > 0,
|
||||
"请输入有效的正数价格",
|
||||
),
|
||||
})
|
||||
|
||||
export function UpdateProductSku(props: {
|
||||
@@ -61,6 +68,7 @@ export function UpdateProductSku(props: {
|
||||
name: props.sku.name,
|
||||
price: props.sku.price,
|
||||
discount_id: props.sku.discount ? String(props.sku.discount.id) : "",
|
||||
price_min: props.sku.price_min ?? "",
|
||||
},
|
||||
})
|
||||
|
||||
@@ -75,6 +83,8 @@ export function UpdateProductSku(props: {
|
||||
}, [open])
|
||||
|
||||
const onSubmit = async (data: z.infer<typeof schema>) => {
|
||||
console.log(data, "data")
|
||||
|
||||
try {
|
||||
const resp = await updateProductSku({
|
||||
id: props.sku.id,
|
||||
@@ -85,7 +95,22 @@ export function UpdateProductSku(props: {
|
||||
data.discount_id && data.discount_id !== ""
|
||||
? Number(data.discount_id)
|
||||
: null,
|
||||
price_min: data.price_min,
|
||||
})
|
||||
console.log({
|
||||
id: props.sku.id,
|
||||
code: data.code,
|
||||
name: data.name,
|
||||
price: data.price,
|
||||
discount_id:
|
||||
data.discount_id && data.discount_id !== ""
|
||||
? Number(data.discount_id)
|
||||
: null,
|
||||
price_min: data.price_min,
|
||||
})
|
||||
|
||||
console.log(resp, "resp")
|
||||
|
||||
if (resp.success) {
|
||||
toast.success("套餐修改成功")
|
||||
props.onSuccess?.()
|
||||
@@ -106,6 +131,7 @@ export function UpdateProductSku(props: {
|
||||
name: props.sku.name,
|
||||
price: props.sku.price,
|
||||
discount_id: props.sku.discount ? String(props.sku.discount.id) : "",
|
||||
price_min: props.sku.price_min ?? "",
|
||||
})
|
||||
}
|
||||
setOpen(value)
|
||||
@@ -163,7 +189,24 @@ export function UpdateProductSku(props: {
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
|
||||
<Controller
|
||||
control={form.control}
|
||||
name="price_min"
|
||||
render={({ field, fieldState }) => (
|
||||
<Field>
|
||||
<FieldLabel htmlFor="sku-create-price">最低价格</FieldLabel>
|
||||
<Input
|
||||
id="sku-create-price"
|
||||
placeholder="请输入最低价格"
|
||||
{...field}
|
||||
aria-invalid={fieldState.invalid}
|
||||
/>
|
||||
{fieldState.invalid && (
|
||||
<FieldError errors={[fieldState.error]} />
|
||||
)}
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
control={form.control}
|
||||
name="discount_id"
|
||||
|
||||
@@ -283,7 +283,7 @@ function ResourceList({ resourceType }: ResourceListProps) {
|
||||
() => [
|
||||
{
|
||||
header: "会员号",
|
||||
accessorFn: (row: Resources) => row.user?.phone || "-",
|
||||
accessorFn: (row: Resources) => row.user?.phone || "",
|
||||
},
|
||||
{
|
||||
header: "套餐",
|
||||
|
||||
@@ -2,10 +2,12 @@
|
||||
import { zodResolver } from "@hookform/resolvers/zod"
|
||||
import { format } from "date-fns"
|
||||
import { CheckCircle, Clock, XCircle } from "lucide-react"
|
||||
import { Suspense, 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 { getPageTrade } from "@/actions/trade"
|
||||
import { getPageTrade, getTradeComplete } from "@/actions/trade"
|
||||
import { Auth } from "@/components/auth"
|
||||
import { DataTable, useDataTable } from "@/components/data-table"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import {
|
||||
@@ -22,6 +24,7 @@ import {
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from "@/components/ui/select"
|
||||
import { ScopeTradeWriteComplete } from "@/lib/scopes"
|
||||
import type { Trade } from "@/models/trade"
|
||||
|
||||
type FilterValues = {
|
||||
@@ -82,9 +85,14 @@ export default function TradePage() {
|
||||
},
|
||||
})
|
||||
|
||||
const table = useDataTable<Trade>((page, size) =>
|
||||
getPageTrade({ page, size, ...filters }),
|
||||
const fetchTrades = useCallback(
|
||||
(page: number, size: number) => {
|
||||
return getPageTrade({ page, size, ...filters })
|
||||
},
|
||||
[filters],
|
||||
)
|
||||
|
||||
const table = useDataTable<Trade>(fetchTrades)
|
||||
const onFilter = handleSubmit(data => {
|
||||
const result: FilterValues = {}
|
||||
|
||||
@@ -105,6 +113,27 @@ export default function TradePage() {
|
||||
table.pagination.onPageChange(1)
|
||||
})
|
||||
|
||||
const [completingId, setCompletingId] = useState<string | null>(null)
|
||||
|
||||
const handleComplete = async (trade: Trade) => {
|
||||
if (completingId) return
|
||||
setCompletingId(trade.inner_no)
|
||||
try {
|
||||
const result = await getTradeComplete({
|
||||
user_id: Number(trade.user_id),
|
||||
trade_no: trade.inner_no,
|
||||
method: trade.method,
|
||||
})
|
||||
if (result.success) {
|
||||
toast.success("订单已完成")
|
||||
} else {
|
||||
toast.error(result.message || "操作失败")
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("完成订单失败:", error)
|
||||
toast.error("网络错误,请稍后重试")
|
||||
}
|
||||
}
|
||||
return (
|
||||
<div className="space-y-3">
|
||||
{/* 筛选表单 */}
|
||||
@@ -260,7 +289,7 @@ export default function TradePage() {
|
||||
columns={[
|
||||
{
|
||||
header: "会员号",
|
||||
accessorFn: row => row.user?.phone || "-",
|
||||
accessorFn: row => row.user?.phone || "",
|
||||
},
|
||||
{
|
||||
header: "订单号",
|
||||
@@ -368,6 +397,30 @@ export default function TradePage() {
|
||||
cell: ({ row }) =>
|
||||
format(new Date(row.original.created_at), "yyyy-MM-dd HH:mm"),
|
||||
},
|
||||
{
|
||||
id: "action",
|
||||
meta: { pin: "right" },
|
||||
header: "操作",
|
||||
cell: ({ row }) => {
|
||||
const isPending = row.original.status === 0
|
||||
const isLoading = completingId === row.original.inner_no
|
||||
return (
|
||||
<div className="flex gap-2">
|
||||
<Auth scope={ScopeTradeWriteComplete}>
|
||||
{isPending && (
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => handleComplete(row.original)}
|
||||
disabled={isLoading}
|
||||
>
|
||||
{isLoading ? "处理中..." : "完成订单"}
|
||||
</Button>
|
||||
)}
|
||||
</Auth>
|
||||
</div>
|
||||
)
|
||||
},
|
||||
},
|
||||
]}
|
||||
/>
|
||||
</Suspense>
|
||||
|
||||
@@ -3,10 +3,10 @@ import { zodResolver } from "@hookform/resolvers/zod"
|
||||
import { format } from "date-fns"
|
||||
import { Suspense, useCallback, useState } from "react"
|
||||
import { Controller, useForm } from "react-hook-form"
|
||||
import { toast } from "sonner"
|
||||
import { z } from "zod"
|
||||
import { bindAdmin, getPageUser } from "@/actions/user"
|
||||
import { DataTable } from "@/components/data-table"
|
||||
import { bindAdmin, getPageUserPage } from "@/actions/user"
|
||||
import { Auth } from "@/components/auth"
|
||||
import { DataTable, useDataTable } from "@/components/data-table"
|
||||
import { Badge } from "@/components/ui/badge"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import {
|
||||
@@ -16,82 +16,47 @@ import {
|
||||
FieldLabel,
|
||||
} from "@/components/ui/field"
|
||||
import { Input } from "@/components/ui/input"
|
||||
import { useFetch } from "@/hooks/data"
|
||||
import { ScopeUserWriteBind } from "@/lib/scopes"
|
||||
import type { User } from "@/models/user"
|
||||
|
||||
interface UserQueryParams {
|
||||
account?: string
|
||||
name?: string
|
||||
interface FilterValues {
|
||||
phone?: string
|
||||
}
|
||||
|
||||
const filterSchema = z.object({
|
||||
account: z.string().optional(),
|
||||
name: z.string().optional(),
|
||||
phone: z.string().optional(),
|
||||
})
|
||||
|
||||
type FormValues = z.infer<typeof filterSchema>
|
||||
|
||||
export default function UserPage() {
|
||||
const [userList, setUserList] = useState<User[]>([])
|
||||
const [filters, setFilters] = useState<FilterValues>({})
|
||||
const [loading, setLoading] = useState(false)
|
||||
|
||||
const { control, handleSubmit, reset } = useForm<FormValues>({
|
||||
resolver: zodResolver(filterSchema),
|
||||
defaultValues: {
|
||||
account: "",
|
||||
name: "",
|
||||
phone: "",
|
||||
},
|
||||
})
|
||||
|
||||
const fetchUsers = useCallback(async (filters: UserQueryParams = {}) => {
|
||||
setLoading(true)
|
||||
try {
|
||||
const res = await getPageUser(filters)
|
||||
if (res.success) {
|
||||
const req = [{ ...res.data }]
|
||||
setUserList(req)
|
||||
} else {
|
||||
toast.error(res.message || "获取用户失败")
|
||||
setUserList([])
|
||||
}
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : error
|
||||
toast.error(`获取管理员失败: ${message}`)
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}, [])
|
||||
|
||||
const bind = useCallback(
|
||||
async (id: number) => {
|
||||
try {
|
||||
const res = await bindAdmin({ id })
|
||||
if (res.success) {
|
||||
toast.success("用户已认领")
|
||||
fetchUsers()
|
||||
} else {
|
||||
toast.error(res.message || "认领失败")
|
||||
}
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : error
|
||||
toast.error(`认领请求失败: ${message}`)
|
||||
}
|
||||
},
|
||||
[fetchUsers],
|
||||
const fetchUsers = useCallback(
|
||||
(page: number, size: number) => getPageUserPage({ page, size, ...filters }),
|
||||
[filters],
|
||||
)
|
||||
|
||||
const onFilter = handleSubmit((data: FormValues) => {
|
||||
const params: UserQueryParams = {}
|
||||
const table = useDataTable<User>(fetchUsers)
|
||||
const bind = useFetch(table, (id: number) => bindAdmin({ id }), {
|
||||
done: "用户已认领",
|
||||
fail: "用户认领失败",
|
||||
})
|
||||
|
||||
if (data.account?.trim()) params.account = data.account.trim()
|
||||
if (data.name?.trim()) params.name = data.name.trim()
|
||||
const hasValidFilter = Object.keys(params).length > 0
|
||||
if (hasValidFilter) {
|
||||
fetchUsers(params)
|
||||
} else {
|
||||
setUserList([])
|
||||
setLoading(false)
|
||||
toast.info("请输入筛选条件后再查询")
|
||||
}
|
||||
const onFilter = handleSubmit(data => {
|
||||
const result: FilterValues = {}
|
||||
if (data.phone) result.phone = data.phone
|
||||
setFilters(result)
|
||||
table.pagination.onPageChange(1)
|
||||
})
|
||||
|
||||
return (
|
||||
@@ -99,30 +64,15 @@ export default function UserPage() {
|
||||
<form onSubmit={onFilter} className="bg-white p-4">
|
||||
<div className="flex flex-wrap items-end gap-4">
|
||||
<Controller
|
||||
name="account"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-80 flex-none"
|
||||
>
|
||||
<FieldLabel>账号/手机号/邮箱</FieldLabel>
|
||||
<Input {...field} placeholder="请输入账号/手机号/邮箱" />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
/>
|
||||
|
||||
<Controller
|
||||
name="name"
|
||||
name="phone"
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field
|
||||
data-invalid={fieldState.invalid}
|
||||
className="w-40 flex-none"
|
||||
>
|
||||
<FieldLabel>姓名</FieldLabel>
|
||||
<Input {...field} placeholder="请输入姓名" />
|
||||
<FieldLabel>手机号</FieldLabel>
|
||||
<Input {...field} placeholder="请输入手机号" />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
)}
|
||||
@@ -136,7 +86,7 @@ export default function UserPage() {
|
||||
variant="outline"
|
||||
onClick={() => {
|
||||
reset()
|
||||
setUserList([])
|
||||
setFilters({})
|
||||
setLoading(false)
|
||||
}}
|
||||
>
|
||||
@@ -147,7 +97,7 @@ export default function UserPage() {
|
||||
|
||||
<Suspense>
|
||||
<DataTable<User>
|
||||
data={userList || []}
|
||||
{...table}
|
||||
status={loading ? "load" : "done"}
|
||||
columns={[
|
||||
{ header: "账号", accessorKey: "username" },
|
||||
@@ -186,14 +136,6 @@ export default function UserPage() {
|
||||
</Badge>
|
||||
),
|
||||
},
|
||||
{
|
||||
header: "身份证号",
|
||||
accessorKey: "id_no",
|
||||
cell: ({ row }) => {
|
||||
const idNo = row.original.id_no
|
||||
return idNo ? `${idNo.slice(0, 6)}****${idNo.slice(-4)}` : "-"
|
||||
},
|
||||
},
|
||||
{
|
||||
header: "客户来源",
|
||||
accessorKey: "source",
|
||||
@@ -213,10 +155,6 @@ export default function UserPage() {
|
||||
cell: ({ row }) => (row.original.status === 1 ? "正常" : "禁用"),
|
||||
},
|
||||
{ header: "联系方式", accessorKey: "contact_wechat" },
|
||||
{
|
||||
header: "客户经理",
|
||||
cell: ({ row }) => row.original.admin?.name || "-",
|
||||
},
|
||||
{
|
||||
header: "最后登录时间",
|
||||
accessorKey: "last_login",
|
||||
@@ -226,12 +164,12 @@ export default function UserPage() {
|
||||
new Date(row.original.last_login),
|
||||
"yyyy-MM-dd HH:mm",
|
||||
)
|
||||
: "-",
|
||||
: "",
|
||||
},
|
||||
{
|
||||
header: "最后登录IP",
|
||||
accessorKey: "last_login_ip",
|
||||
cell: ({ row }) => row.original.last_login_ip || "-",
|
||||
cell: ({ row }) => row.original.last_login_ip || "",
|
||||
},
|
||||
{
|
||||
header: "创建时间",
|
||||
@@ -244,13 +182,15 @@ export default function UserPage() {
|
||||
meta: { pin: "right" },
|
||||
header: "操作",
|
||||
cell: ctx => (
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => bind(ctx.row.original.id)}
|
||||
disabled={!!ctx.row.original.admin_id}
|
||||
>
|
||||
{ctx.row.original.admin_id ? "已认领" : "认领"}
|
||||
</Button>
|
||||
<Auth scope={ScopeUserWriteBind}>
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => bind(ctx.row.original.id)}
|
||||
disabled={!!ctx.row.original.admin_id}
|
||||
>
|
||||
{ctx.row.original.admin_id ? "已认领" : "认领"}
|
||||
</Button>
|
||||
</Auth>
|
||||
),
|
||||
},
|
||||
]}
|
||||
|
||||
@@ -22,6 +22,7 @@ export const ScopeProductWrite = "product:write" // 写入产品
|
||||
export const ScopeProductSku = "product_sku"
|
||||
export const ScopeProductSkuRead = "product_sku:read" // 读取产品套餐列表
|
||||
export const ScopeProductSkuWrite = "product_sku:write" // 写入产品套餐
|
||||
export const ScopeProductSkuWriteStatus = "product_sku:write:status" // 更改产品套餐状态
|
||||
|
||||
// 折扣
|
||||
export const ScopeDiscount = "discount"
|
||||
@@ -32,14 +33,23 @@ export const ScopeDiscountWrite = "discount:write" // 写入折扣
|
||||
export const ScopeResource = "resource"
|
||||
export const ScopeResourceRead = "resource:read" // 读取用户套餐列表
|
||||
export const ScopeResourceWrite = "resource:write" // 写入用户套餐
|
||||
export const ScopeResourceShort = "resource:short" // 短效动态套餐
|
||||
export const ScopeResourceShortRead = "resource:short:read" // 读取用户短效动态套餐列表
|
||||
export const ScopeResourceShortReadOfUser = "resource:short:read:of_user" // 读取指定用户的短效动态套餐列表
|
||||
export const ScopeResourceLong = "resource:long" // 长效动态套餐
|
||||
export const ScopeResourceLongRead = "resource:long:read" // 读取用户长效动态套餐列表
|
||||
export const ScopeResourceLongReadOfUser = "resource:long:read:of_user" // 读取指定用户的长效动态套餐列表
|
||||
|
||||
// 用户
|
||||
export const ScopeUser = "user"
|
||||
export const ScopeUserRead = "user:read" // 读取用户列表
|
||||
export const ScopeUserReadOne = "user:read:one" // 读取单个用户
|
||||
export const ScopeUserReadNotBind = "user:read:not_bind" // 读取未绑定管理员的用户列表
|
||||
export const ScopeUserWrite = "user:write" // 写入用户
|
||||
export const ScopeUserWriteBalance = "user:write:balance" // 写入用户余额
|
||||
export const ScopeUserReadOne = "user:read:one" // 读取单个用户
|
||||
export const ScopeUserWriteBind = "user:write:bind" // 认领用户
|
||||
export const ScopeUserWriteBalanceInc = "user:write:balance:inc" // 增加用户余额
|
||||
export const ScopeUserWriteBalanceDec = "user:write:balance:dec" // 减少用户余额
|
||||
export const ScopeUserWriteBind = "user:write:bind" // 用户认领
|
||||
|
||||
// 优惠券
|
||||
export const ScopeCoupon = "coupon"
|
||||
@@ -49,19 +59,29 @@ export const ScopeCouponWrite = "coupon:write" // 写入优惠券
|
||||
// 批次
|
||||
export const ScopeBatch = "batch"
|
||||
export const ScopeBatchRead = "batch:read" // 读取批次列表
|
||||
export const ScopeBatchReadOfUser = "batch:read:of_user" // 读取指定用户的批次列表
|
||||
export const ScopeBatchWrite = "batch:write" // 写入批次
|
||||
|
||||
// IP
|
||||
export const ScopeChannel = "channel"
|
||||
export const ScopeChannelRead = "channel:read" // 读取 IP 列表
|
||||
export const ScopeChannelReadOfUser = "channel:read:of_user" // 读取指定用户的 IP 列表
|
||||
export const ScopeChannelWrite = "channel:write" // 写入 IP
|
||||
|
||||
// 交易
|
||||
export const ScopeTrade = "trade"
|
||||
export const ScopeTradeRead = "trade:read" // 读取交易列表
|
||||
export const ScopeTradeReadOfUser = "trade:read:of_user" // 读取指定用户的交易列表
|
||||
export const ScopeTradeWrite = "trade:write" // 写入交易
|
||||
export const ScopeTradeWriteComplete = "trade:write:complete" // 完成交易
|
||||
|
||||
// 账单
|
||||
export const ScopeBill = "bill"
|
||||
export const ScopeBillRead = "bill:read" // 读取账单列表
|
||||
export const ScopeBillReadOfUser = "bill:read:of_user" // 读取指定用户的账单列表
|
||||
export const ScopeBillWrite = "bill:write" // 写入账单
|
||||
|
||||
// 余额变动
|
||||
export const ScopeBalanceActivity = "balance_activity"
|
||||
export const ScopeBalanceActivityRead = "balance_activity:read" // 读取余额变动列表
|
||||
export const ScopeBalanceActivityReadOfUser = "balance_activity:read:of_user" // 读取指定用户的余额变动列表
|
||||
|
||||
@@ -19,7 +19,7 @@ export type Admin = {
|
||||
phone?: string
|
||||
email?: string
|
||||
status: AdminStatus
|
||||
|
||||
lock: boolean
|
||||
lastLogin?: Date
|
||||
lastLoginIp?: string
|
||||
lastLoginUa?: string
|
||||
|
||||
18
src/models/balance.ts
Normal file
18
src/models/balance.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import type { Admin } from "./admin"
|
||||
import type { Billing } from "./billing"
|
||||
import type { User } from "./user"
|
||||
|
||||
export type Balance = {
|
||||
id: number
|
||||
user_id: string
|
||||
bill_id: string
|
||||
admin_id: string
|
||||
amount: number
|
||||
balance_prev: number
|
||||
balance_curr: number
|
||||
remark: string
|
||||
created_at: Date
|
||||
user?: User
|
||||
admin?: Admin
|
||||
bill?: Billing
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
import type { ProductDiscount } from "./product_discount"
|
||||
|
||||
export type Cust = {
|
||||
id: number
|
||||
admin_id?: number
|
||||
phone: string
|
||||
admin?: Admin
|
||||
password: string
|
||||
source: number
|
||||
discount_id: string
|
||||
has_password: boolean
|
||||
username: string
|
||||
email: string
|
||||
name: string
|
||||
avatar: string
|
||||
status: number
|
||||
balance: number
|
||||
id_type: number
|
||||
id_no: string
|
||||
id_token: string
|
||||
contact_qq: string
|
||||
contact_wechat: string
|
||||
last_login: Date
|
||||
last_login_host: string
|
||||
last_login_agent: string
|
||||
last_login_ip: string
|
||||
created_at: Date
|
||||
updated_at: Date
|
||||
discount: ProductDiscount
|
||||
}
|
||||
export type Admin = {
|
||||
name: string
|
||||
password: string
|
||||
}
|
||||
@@ -7,7 +7,8 @@ export type ProductSku = Model & {
|
||||
code: string
|
||||
name: string
|
||||
price: string
|
||||
|
||||
status: number
|
||||
product?: Product
|
||||
price_min?: string
|
||||
discount?: ProductDiscount
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ export type Trade = {
|
||||
type: number
|
||||
subject: string
|
||||
status: number
|
||||
user_id: string
|
||||
created_at: Date
|
||||
canceled_at: Date
|
||||
updated_at: Date
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import type { Admin } from "./admin"
|
||||
import type { ProductDiscount } from "./product_discount"
|
||||
|
||||
export type User = {
|
||||
id: number
|
||||
admin_id?: number
|
||||
admin?: Admin
|
||||
phone: string
|
||||
source: number
|
||||
has_password: boolean
|
||||
@@ -24,4 +24,8 @@ export type User = {
|
||||
last_login_ip: string
|
||||
created_at: Date
|
||||
updated_at: Date
|
||||
password: string
|
||||
discount_id: string
|
||||
discount: ProductDiscount
|
||||
admin?: Admin
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user