page.tsx
9.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
"use client";
import { useCallback, useEffect, useState } from "react";
import { useRouter } from "next/navigation";
import {
ChevronLeft,
ChevronRight,
KeyRound,
Loader2,
Search,
UserRound,
} from "lucide-react";
import ConfirmDialog from "@/components/ConfirmDialog";
import UserProfileDialog from "@/components/UserProfileDialog";
import { apiJson, fetchMe } from "@/lib/auth";
import { cn } from "@/lib/utils";
interface AdminUser {
id: string;
username: string;
displayName: string;
phone: string | null;
email: string | null;
role: string;
status: string;
storeName: string | null;
positionName: string | null;
lastLoginAt: string | null;
}
const PAGE_SIZE = 20;
export default function AdminUsersPage() {
const router = useRouter();
const [users, setUsers] = useState<AdminUser[]>([]);
const [total, setTotal] = useState(0);
const [page, setPage] = useState(0);
const [keyword, setKeyword] = useState("");
const [loading, setLoading] = useState(true);
const [busy, setBusy] = useState(false);
const [resetTarget, setResetTarget] = useState<AdminUser | null>(null);
const [editTarget, setEditTarget] = useState<AdminUser | null>(null);
const [notice, setNotice] = useState("");
const load = useCallback(
async (search = "", pageIndex = 0) => {
setLoading(true);
try {
const data = await apiJson<{ total: number; items: AdminUser[] }>(
`/admin/users?keyword=${encodeURIComponent(search)}&limit=${PAGE_SIZE}&offset=${
pageIndex * PAGE_SIZE
}`
);
setUsers(data.items);
setTotal(data.total);
} catch {
router.push("/login");
} finally {
setLoading(false);
}
},
[router]
);
useEffect(() => {
(async () => {
const me = await fetchMe().catch(() => null);
if (!me) {
router.push("/login");
return;
}
if (me.role !== "admin") {
router.push("/");
return;
}
await load();
})();
}, [router, load]);
const handleSearch = () => {
setPage(0);
load(keyword, 0);
};
const goPage = (next: number) => {
setPage(next);
load(keyword, next);
};
const handleReset = async () => {
if (!resetTarget) return;
setBusy(true);
try {
const data = await apiJson<{ initialPassword: string }>(
`/admin/users/${resetTarget.id}/reset-password`,
{ method: "POST" }
);
setNotice(`已将 ${resetTarget.displayName} 的密码重置为 ${data.initialPassword}`);
setResetTarget(null);
setTimeout(() => setNotice(""), 4000);
} finally {
setBusy(false);
}
};
const totalPages = Math.max(1, Math.ceil(total / PAGE_SIZE));
return (
<div className="flex-1 overflow-y-auto bg-slate-100">
<div className="w-full max-w-none px-8 py-8">
<header className="mb-5 flex items-end justify-between">
<div>
<h1 className="text-lg font-bold text-slate-800">用户管理</h1>
<p className="text-xs text-slate-500 mt-1">共 {total} 名用户</p>
</div>
<div className="relative">
<Search className="w-3.5 h-3.5 absolute left-3 top-1/2 -translate-y-1/2 text-slate-400" />
<input
value={keyword}
onChange={(e) => setKeyword(e.target.value)}
onKeyDown={(e) => e.key === "Enter" && handleSearch()}
placeholder="搜索姓名 / 账号 / 手机号"
className="w-64 pl-8 pr-3 py-2 text-xs bg-white border border-slate-200 rounded-lg outline-none focus:border-blue-500 focus:ring-2 focus:ring-blue-500/20 transition"
/>
</div>
</header>
{notice && (
<div className="mb-4 text-xs text-emerald-700 bg-emerald-50 border border-emerald-100 rounded-lg px-3 py-2.5">
{notice}
</div>
)}
<div className="bg-white rounded-xl border border-slate-200 overflow-hidden">
{loading ? (
<div className="flex items-center justify-center py-16 text-slate-400 text-xs gap-2">
<Loader2 className="w-4 h-4 animate-spin" />
加载中…
</div>
) : (
<table className="w-full text-xs">
<thead className="bg-slate-50 border-b border-slate-200">
<tr className="text-left text-slate-500">
<th className="px-4 py-2.5 font-semibold">姓名</th>
<th className="px-4 py-2.5 font-semibold">账号</th>
<th className="px-4 py-2.5 font-semibold">岗位</th>
<th className="px-4 py-2.5 font-semibold">门店</th>
<th className="px-4 py-2.5 font-semibold">角色</th>
<th className="px-4 py-2.5 font-semibold">状态</th>
<th className="px-4 py-2.5 font-semibold text-right">操作</th>
</tr>
</thead>
<tbody>
{users.map((user) => (
<tr
key={user.id}
onClick={() => setEditTarget(user)}
title="点击查看 / 编辑该用户的画像与长期记忆"
className="group border-b border-slate-100 last:border-0 hover:bg-blue-50/40 cursor-pointer transition"
>
<td className="px-4 py-2.5">
<div className="flex items-center gap-2">
<div className="w-6 h-6 bg-slate-100 rounded-lg flex items-center justify-center text-slate-500 group-hover:bg-blue-100 group-hover:text-blue-600 transition">
<UserRound className="w-3 h-3" />
</div>
<span className="font-medium text-slate-800 group-hover:text-blue-700 transition">
{user.displayName}
</span>
</div>
</td>
<td className="px-4 py-2.5 text-slate-500">{user.username}</td>
<td className="px-4 py-2.5 text-slate-500">{user.positionName || "—"}</td>
<td className="px-4 py-2.5 text-slate-500">{user.storeName || "—"}</td>
<td className="px-4 py-2.5">
<span
className={cn(
"px-2 py-0.5 rounded-md text-[10px] font-semibold",
user.role === "admin"
? "bg-blue-50 text-blue-700"
: "bg-slate-100 text-slate-500"
)}
>
{user.role === "admin" ? "管理员" : "普通用户"}
</span>
</td>
<td className="px-4 py-2.5">
<span
className={cn(
"px-2 py-0.5 rounded-md text-[10px] font-semibold",
user.status === "active"
? "bg-emerald-50 text-emerald-700"
: "bg-slate-100 text-slate-400"
)}
>
{user.status === "active" ? "启用" : "停用"}
</span>
</td>
<td className="px-4 py-2.5 text-right">
<button
onClick={(e) => {
e.stopPropagation();
setResetTarget(user);
}}
className="inline-flex items-center gap-1 px-2.5 py-1 text-[11px] font-semibold text-slate-600 border border-slate-200 rounded-lg hover:bg-white bg-white transition"
>
<KeyRound className="w-3 h-3" />
重置密码
</button>
</td>
</tr>
))}
{users.length === 0 && (
<tr>
<td colSpan={7} className="px-4 py-12 text-center text-slate-400">
没有匹配的用户
</td>
</tr>
)}
</tbody>
</table>
)}
</div>
{/* 分页条 */}
{!loading && total > 0 && (
<div className="mt-4 flex items-center justify-end gap-3 text-xs text-slate-500">
<span>
第 {page + 1} / {totalPages} 页
</span>
<div className="flex items-center gap-1">
<button
onClick={() => goPage(page - 1)}
disabled={page <= 0}
className="flex items-center gap-1 px-2.5 py-1.5 font-semibold bg-white border border-slate-200 rounded-lg hover:bg-slate-50 disabled:opacity-40 disabled:cursor-not-allowed transition"
>
<ChevronLeft className="w-3.5 h-3.5" />
上一页
</button>
<button
onClick={() => goPage(page + 1)}
disabled={page >= totalPages - 1}
className="flex items-center gap-1 px-2.5 py-1.5 font-semibold bg-white border border-slate-200 rounded-lg hover:bg-slate-50 disabled:opacity-40 disabled:cursor-not-allowed transition"
>
下一页
<ChevronRight className="w-3.5 h-3.5" />
</button>
</div>
</div>
)}
</div>
<ConfirmDialog
open={!!resetTarget}
title="重置密码?"
description={`将把 ${resetTarget?.displayName || ""} 的密码重置为初始密码,该用户需用新密码重新登录。`}
confirmText="重置"
loading={busy}
onConfirm={handleReset}
onCancel={() => setResetTarget(null)}
/>
{editTarget && (
<UserProfileDialog
userId={editTarget.id}
displayName={editTarget.displayName}
onClose={() => setEditTarget(null)}
/>
)}
</div>
);
}