feat: admin back-office system + AI assistant with action commands

- Admin analytics (recharts charts, overview stats, trend analysis, time range selector)
- Admin permissions (AdminRole/AdminUser models, role CRUD, permission catalog)
- Admin system config (site/AI/member category tabs, per-key save)
- Admin operations (Banner CRUD, push notification send/delete)
- Admin user management (table, search, create/edit, ban/delete)
- Admin layout (custom top bar with branding + sidebar, admin AI assistant)
- Admin login (adminToken localStorage, adminInfo display)
- Admin AI assistant (purple '运营助手', context-aware prompts, action commands)
- Public AI assistant Phase B (action commands: navigate, setModel, startChat, openSkill, setParameter)
- Assistant context mapping + action executor
- Skills API fix: tags parsing fallback (JSON.parse → split)
- Sandbox crash fix: curScene fallback system prompt
- JWT token expiration: access 2h→7d, refresh 7d→30d, admin 8h→7d
- i18n: assistant-related translations (zh/en)
- PM2 ecosystem config for process management
- AI assistant login prompt fix: admin uses getAdminToken(), public uses apiFetch with token refresh
This commit is contained in:
yuzhiran-dev
2026-05-20 10:38:58 +08:00
parent dd9240e5cf
commit 728edc59ef
33 changed files with 3014 additions and 153 deletions
+160 -104
View File
@@ -2,33 +2,34 @@
import { useEffect, useState } from 'react';
import { Skeleton } from '@/components/ui/skeleton';
interface User {
id: number;
phone: string;
nickname: string;
email?: string;
phone?: string;
email: string;
status: string;
memberPlan: string;
createdAt: string;
}
export default function AdminUsers() {
export default function UsersPage() {
const [users, setUsers] = useState<User[]>([]);
const [loading, setLoading] = useState(true);
const [search, setSearch] = useState('');
const [showForm, setShowForm] = useState(false);
const [form, setForm] = useState({ nickname: '', phone: '', email: '', password: '' });
const [editId, setEditId] = useState<number | null>(null);
useEffect(() => {
loadUsers();
}, []);
useEffect(() => { loadUsers(); }, [search]);
async function loadUsers() {
setLoading(true);
try {
const token = localStorage.getItem('adminToken');
const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3000'}/api/v1/admin/users`, {
const params = search ? `?search=${encodeURIComponent(search)}` : '';
const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3000'}/api/v1/admin/users${params}`, {
headers: { Authorization: `Bearer ${token}` },
});
if (res.ok) {
const data = await res.json();
setUsers(data.items || []);
@@ -37,104 +38,159 @@ export default function AdminUsers() {
setLoading(false);
}
async function toggleStatus(userId: number, currentStatus: string) {
try {
const token = localStorage.getItem('adminToken');
await fetch(`${process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3000'}/api/v1/admin/users/${userId}/status`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({ status: currentStatus === 'ACTIVE' ? 'INACTIVE' : 'ACTIVE' }),
});
loadUsers();
} catch {}
async function createUser() {
if (!form.phone || !form.password) return alert('手机号和密码必填');
const token = localStorage.getItem('adminToken');
await fetch(`${process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3000'}/api/v1/admin/users`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` },
body: JSON.stringify(form),
});
setShowForm(false);
setForm({ nickname: '', phone: '', email: '', password: '' });
loadUsers();
}
if (loading) return (
<div className="space-y-4 p-6">
<Skeleton className="h-8 w-48" />
<Skeleton className="h-10 w-full" />
<Skeleton className="h-10 w-full" />
<Skeleton className="h-10 w-3/4" />
</div>
);
async function updateUser() {
if (!editId) return;
const token = localStorage.getItem('adminToken');
await fetch(`${process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3000'}/api/v1/admin/users/${editId}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` },
body: JSON.stringify({ nickname: form.nickname, email: form.email }),
});
setEditId(null);
setShowForm(false);
setForm({ nickname: '', phone: '', email: '', password: '' });
loadUsers();
}
async function deleteUser(id: number) {
if (!confirm('确定删除该用户?')) return;
const token = localStorage.getItem('adminToken');
await fetch(`${process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3000'}/api/v1/admin/users/${id}`, {
method: 'DELETE',
headers: { Authorization: `Bearer ${token}` },
});
loadUsers();
}
async function toggleStatus(id: number, currentStatus: string) {
const token = localStorage.getItem('adminToken');
const newStatus = currentStatus === 'ACTIVE' ? 'BANNED' : 'ACTIVE';
await fetch(`${process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3000'}/api/v1/admin/users/${id}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` },
body: JSON.stringify({ status: newStatus }),
});
loadUsers();
}
function openEdit(user: User) {
setEditId(user.id);
setForm({ nickname: user.nickname || '', phone: user.phone || '', email: user.email || '', password: '' });
setShowForm(true);
}
if (loading) return <div className="p-6">...</div>;
return (
<>
<div className="border-b border-border bg-card px-4 py-4">
<h1 className="text-2xl font-bold text-foreground"></h1>
</div>
<div className="p-6">
<div className="bg-card rounded-xl border border-border overflow-hidden">
<table className="w-full">
<thead className="bg-muted/50 border-b border-border">
<tr>
<th className="px-6 py-3 text-left text-xs font-medium text-muted-foreground uppercase">ID</th>
<th className="px-6 py-3 text-left text-xs font-medium text-muted-foreground uppercase"></th>
<th className="px-6 py-3 text-left text-xs font-medium text-muted-foreground uppercase"></th>
<th className="px-6 py-3 text-left text-xs font-medium text-muted-foreground uppercase"></th>
<th className="px-6 py-3 text-left text-xs font-medium text-muted-foreground uppercase"></th>
<th className="px-6 py-3 text-left text-xs font-medium text-muted-foreground uppercase"></th>
<th className="px-6 py-3 text-left text-xs font-medium text-muted-foreground uppercase"></th>
</tr>
</thead>
<tbody className="divide-y divide-border">
{users.map(user => (
<tr key={user.id} className="hover:bg-accent/50">
<td className="px-6 py-4 text-sm text-foreground">{user.id}</td>
<td className="px-6 py-4">
<div className="text-sm font-medium text-foreground">{user.nickname || '未设置'}</div>
</td>
<td className="px-6 py-4">
<div className="text-sm text-muted-foreground">{user.email || user.phone || '-'}</div>
</td>
<td className="px-6 py-4">
<span className={`px-2 py-1 text-xs rounded-full ${
user.status === 'ACTIVE' ? 'bg-green-100 dark:bg-green-900/30 text-green-700 dark:text-green-400' : 'bg-red-100 dark:bg-red-900/30 text-red-700 dark:text-red-400'
}`}>
{user.status}
</span>
</td>
<td className="px-6 py-4">
<span className={`px-2 py-1 text-xs rounded-full ${
user.memberPlan === 'YEARLY' ? 'bg-purple-100 dark:bg-purple-900/30 text-purple-700 dark:text-purple-400' :
user.memberPlan === 'MONTHLY' ? 'bg-blue-100 dark:bg-blue-900/30 text-blue-700 dark:text-blue-400' :
'bg-muted text-muted-foreground'
}`}>
{user.memberPlan || 'NONE'}
</span>
</td>
<td className="px-6 py-4 text-sm text-muted-foreground">
{new Date(user.createdAt).toLocaleDateString()}
</td>
<td className="px-6 py-4">
<button
onClick={() => toggleStatus(user.id, user.status)}
className={`text-xs px-3 py-1 rounded transition-colors ${
user.status === 'ACTIVE'
? 'text-red-600 hover:bg-red-50 dark:hover:bg-red-900/20'
: 'text-green-600 hover:bg-green-50 dark:hover:bg-green-900/20'
}`}
>
{user.status === 'ACTIVE' ? '禁用' : '启用'}
</button>
</td>
</tr>
))}
</tbody>
</table>
{users.length === 0 && (
<div className="text-center py-20 text-muted-foreground">
</div>
)}
<div className="p-6">
<div className="flex items-center justify-between mb-6">
<div>
<h1 className="text-2xl font-bold text-foreground"></h1>
<p className="text-sm text-muted-foreground"></p>
</div>
<button onClick={() => { setEditId(null); setForm({ nickname: '', phone: '', email: '', password: '' }); setShowForm(true); }}
className="px-4 py-2 bg-brand-600 text-white rounded-lg">
</button>
</div>
</>
);
}
<div className="mb-4">
<input type="text" value={search} onChange={e => setSearch(e.target.value)}
placeholder="搜索用户名或手机号..."
className="px-4 py-2 border border-border rounded-lg bg-background text-foreground w-64" />
</div>
{showForm && (
<div className="bg-card border border-border rounded-xl p-4 mb-6 space-y-4">
<div className="grid grid-cols-2 gap-4">
<div>
<label className="text-sm text-muted-foreground"> {editId ? '(不可修改)' : '*'}</label>
<input type="text" value={form.phone} onChange={e => setForm({...form, phone: e.target.value})}
disabled={!!editId}
className="w-full px-3 py-2 border border-border rounded-lg bg-background text-foreground disabled:opacity-50" />
</div>
<div>
<label className="text-sm text-muted-foreground"></label>
<input type="text" value={form.nickname} onChange={e => setForm({...form, nickname: e.target.value})}
className="w-full px-3 py-2 border border-border rounded-lg bg-background text-foreground" />
</div>
<div>
<label className="text-sm text-muted-foreground"></label>
<input type="email" value={form.email} onChange={e => setForm({...form, email: e.target.value})}
className="w-full px-3 py-2 border border-border rounded-lg bg-background text-foreground" />
</div>
{!editId && (
<div>
<label className="text-sm text-muted-foreground"> *</label>
<input type="password" value={form.password} onChange={e => setForm({...form, password: e.target.value})}
className="w-full px-3 py-2 border border-border rounded-lg bg-background text-foreground" />
</div>
)}
</div>
<div className="flex gap-2">
<button onClick={editId ? updateUser : createUser} className="px-4 py-2 bg-brand-600 text-white rounded-lg">
{editId ? '保存修改' : '创建用户'}
</button>
<button onClick={() => { setShowForm(false); setEditId(null); }} className="px-4 py-2 bg-muted text-muted-foreground rounded-lg">
</button>
</div>
</div>
)}
<div className="bg-card border border-border rounded-xl overflow-hidden">
<table className="w-full">
<thead className="bg-muted/50">
<tr>
<th className="px-4 py-3 text-left text-sm text-muted-foreground">ID</th>
<th className="px-4 py-3 text-left text-sm text-muted-foreground"></th>
<th className="px-4 py-3 text-left text-sm text-muted-foreground"></th>
<th className="px-4 py-3 text-left text-sm text-muted-foreground"></th>
<th className="px-4 py-3 text-left text-sm text-muted-foreground"></th>
<th className="px-4 py-3 text-left text-sm text-muted-foreground"></th>
<th className="px-4 py-3 text-left text-sm text-muted-foreground"></th>
<th className="px-4 py-3 text-left text-sm text-muted-foreground"></th>
</tr>
</thead>
<tbody>
{users.map(user => (
<tr key={user.id} className="border-t border-border">
<td className="px-4 py-3 text-sm">{user.id}</td>
<td className="px-4 py-3 text-sm">{user.phone || '-'}</td>
<td className="px-4 py-3 text-sm">{user.nickname || '-'}</td>
<td className="px-4 py-3 text-sm">{user.email || '-'}</td>
<td className="px-4 py-3">
<span className={`text-xs px-2 py-1 rounded-full ${user.status === 'ACTIVE' ? 'bg-green-100 text-green-700' : 'bg-red-100 text-red-700'}`}>
{user.status === 'ACTIVE' ? '正常' : '禁用'}
</span>
</td>
<td className="px-4 py-3 text-sm">{user.memberPlan || 'FREE'}</td>
<td className="px-4 py-3 text-sm text-muted-foreground">{user.createdAt?.slice(0, 10)}</td>
<td className="px-4 py-3">
<button onClick={() => openEdit(user)} className="text-brand-600 hover:underline text-sm mr-3"></button>
<button onClick={() => toggleStatus(user.id, user.status)} className="text-orange-600 hover:underline text-sm mr-3">
{user.status === 'ACTIVE' ? '禁用' : '启用'}
</button>
<button onClick={() => deleteUser(user.id)} className="text-red-500 hover:underline text-sm"></button>
</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
);
}