- 新增许可证服务器 (license-server/) - 新增 Agent 部署模块 (flnxi-agent/) - 添加许可证验证和激活页面 - 添加 Harbor 镜像同步脚本 - 更新素材系统支持许可证验证
277 lines
8.7 KiB
TypeScript
277 lines
8.7 KiB
TypeScript
'use client';
|
|
|
|
import { useEffect, useState } from 'react';
|
|
import { useRouter } from 'next/navigation';
|
|
import Link from 'next/link';
|
|
|
|
interface Customer {
|
|
id: string;
|
|
companyName: string;
|
|
contactName: string | null;
|
|
contactEmail: string | null;
|
|
contactPhone: string | null;
|
|
createdAt: string;
|
|
_count: { licenses: number };
|
|
}
|
|
|
|
export default function CustomersPage() {
|
|
const router = useRouter();
|
|
const [customers, setCustomers] = useState<Customer[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [search, setSearch] = useState('');
|
|
const [showModal, setShowModal] = useState(false);
|
|
|
|
useEffect(() => {
|
|
fetchCustomers();
|
|
}, []);
|
|
|
|
const fetchCustomers = async () => {
|
|
try {
|
|
const res = await fetch('/api/admin/customers');
|
|
if (res.status === 401) {
|
|
router.push('/admin/login');
|
|
return;
|
|
}
|
|
const data = await res.json();
|
|
setCustomers(data.customers || []);
|
|
} catch (error) {
|
|
console.error('获取客户列表失败:', error);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
const filteredCustomers = customers.filter(
|
|
(c) =>
|
|
c.companyName.toLowerCase().includes(search.toLowerCase()) ||
|
|
c.contactName?.toLowerCase().includes(search.toLowerCase()) ||
|
|
c.contactEmail?.toLowerCase().includes(search.toLowerCase())
|
|
);
|
|
|
|
return (
|
|
<div className="min-h-screen bg-gray-100">
|
|
<nav className="bg-white shadow">
|
|
<div className="max-w-7xl mx-auto px-4 py-4 flex justify-between items-center">
|
|
<div className="flex items-center gap-4">
|
|
<Link href="/admin" className="text-gray-600 hover:text-gray-900">
|
|
← 返回
|
|
</Link>
|
|
<h1 className="text-xl font-bold">客户管理</h1>
|
|
</div>
|
|
</div>
|
|
</nav>
|
|
|
|
<main className="max-w-7xl mx-auto px-4 py-8">
|
|
<div className="bg-white rounded-lg shadow">
|
|
<div className="p-6 border-b flex justify-between items-center">
|
|
<input
|
|
type="text"
|
|
placeholder="搜索客户..."
|
|
value={search}
|
|
onChange={(e) => setSearch(e.target.value)}
|
|
className="px-4 py-2 border rounded-lg w-64"
|
|
/>
|
|
<button
|
|
onClick={() => setShowModal(true)}
|
|
className="px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700"
|
|
>
|
|
添加客户
|
|
</button>
|
|
</div>
|
|
|
|
{loading ? (
|
|
<div className="p-8 text-center text-gray-500">加载中...</div>
|
|
) : (
|
|
<table className="w-full">
|
|
<thead className="bg-gray-50">
|
|
<tr>
|
|
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">
|
|
公司名称
|
|
</th>
|
|
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">
|
|
联系人
|
|
</th>
|
|
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">
|
|
邮箱
|
|
</th>
|
|
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">
|
|
许可证数
|
|
</th>
|
|
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">
|
|
创建时间
|
|
</th>
|
|
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">
|
|
操作
|
|
</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody className="divide-y divide-gray-200">
|
|
{filteredCustomers.map((customer) => (
|
|
<tr key={customer.id}>
|
|
<td className="px-6 py-4 font-medium">{customer.companyName}</td>
|
|
<td className="px-6 py-4 text-gray-600">{customer.contactName || '-'}</td>
|
|
<td className="px-6 py-4 text-gray-600">{customer.contactEmail || '-'}</td>
|
|
<td className="px-6 py-4">{customer._count.licenses}</td>
|
|
<td className="px-6 py-4 text-gray-600">
|
|
{new Date(customer.createdAt).toLocaleDateString()}
|
|
</td>
|
|
<td className="px-6 py-4">
|
|
<Link
|
|
href={`/admin/customers/${customer.id}`}
|
|
className="text-blue-600 hover:underline"
|
|
>
|
|
查看
|
|
</Link>
|
|
</td>
|
|
</tr>
|
|
))}
|
|
{!filteredCustomers.length && (
|
|
<tr>
|
|
<td colSpan={6} className="px-6 py-8 text-center text-gray-500">
|
|
暂无客户数据
|
|
</td>
|
|
</tr>
|
|
)}
|
|
</tbody>
|
|
</table>
|
|
)}
|
|
</div>
|
|
</main>
|
|
|
|
{showModal && (
|
|
<CustomerModal
|
|
onClose={() => setShowModal(false)}
|
|
onSuccess={() => {
|
|
setShowModal(false);
|
|
fetchCustomers();
|
|
}}
|
|
/>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function CustomerModal({
|
|
onClose,
|
|
onSuccess,
|
|
}: {
|
|
onClose: () => void;
|
|
onSuccess: () => void;
|
|
}) {
|
|
const [form, setForm] = useState({
|
|
companyName: '',
|
|
contactName: '',
|
|
contactEmail: '',
|
|
contactPhone: '',
|
|
});
|
|
const [loading, setLoading] = useState(false);
|
|
const [error, setError] = useState('');
|
|
|
|
const handleSubmit = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
setLoading(true);
|
|
setError('');
|
|
|
|
try {
|
|
const res = await fetch('/api/admin/customers', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(form),
|
|
});
|
|
|
|
const data = await res.json();
|
|
if (data.success) {
|
|
onSuccess();
|
|
} else {
|
|
setError(data.error || '创建失败');
|
|
}
|
|
} catch {
|
|
setError('网络错误');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50">
|
|
<div className="bg-white rounded-lg shadow-xl w-full max-w-md">
|
|
<div className="p-6 border-b">
|
|
<h2 className="text-lg font-medium">添加客户</h2>
|
|
</div>
|
|
|
|
<form onSubmit={handleSubmit} className="p-6 space-y-4">
|
|
<div>
|
|
<label className="block text-sm font-medium text-gray-700 mb-1">
|
|
公司名称 *
|
|
</label>
|
|
<input
|
|
type="text"
|
|
value={form.companyName}
|
|
onChange={(e) => setForm({ ...form, companyName: e.target.value })}
|
|
className="w-full px-3 py-2 border rounded-lg"
|
|
required
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="block text-sm font-medium text-gray-700 mb-1">
|
|
联系人
|
|
</label>
|
|
<input
|
|
type="text"
|
|
value={form.contactName}
|
|
onChange={(e) => setForm({ ...form, contactName: e.target.value })}
|
|
className="w-full px-3 py-2 border rounded-lg"
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="block text-sm font-medium text-gray-700 mb-1">
|
|
邮箱
|
|
</label>
|
|
<input
|
|
type="email"
|
|
value={form.contactEmail}
|
|
onChange={(e) => setForm({ ...form, contactEmail: e.target.value })}
|
|
className="w-full px-3 py-2 border rounded-lg"
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="block text-sm font-medium text-gray-700 mb-1">
|
|
电话
|
|
</label>
|
|
<input
|
|
type="tel"
|
|
value={form.contactPhone}
|
|
onChange={(e) => setForm({ ...form, contactPhone: e.target.value })}
|
|
className="w-full px-3 py-2 border rounded-lg"
|
|
/>
|
|
</div>
|
|
|
|
{error && (
|
|
<div className="text-red-600 text-sm bg-red-50 p-3 rounded-lg">{error}</div>
|
|
)}
|
|
|
|
<div className="flex gap-3 pt-4">
|
|
<button
|
|
type="button"
|
|
onClick={onClose}
|
|
className="flex-1 py-2 border rounded-lg hover:bg-gray-50"
|
|
>
|
|
取消
|
|
</button>
|
|
<button
|
|
type="submit"
|
|
disabled={loading}
|
|
className="flex-1 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 disabled:opacity-50"
|
|
>
|
|
{loading ? '创建中...' : '创建'}
|
|
</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|