MpUser.vue
10.6 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<template>
<el-card shadow="never" class="box">
<div class="toolbar">
<el-input
v-model="keyword"
clearable
placeholder="昵称 / 手机号 / OpenID"
class="search"
@keyup.enter="reload"
/>
<el-button type="primary" @click="reload">搜索</el-button>
</div>
<el-table v-loading="loading" :data="list" stripe>
<el-table-column prop="id" label="ID" width="70" />
<el-table-column label="头像" width="72" align="center">
<template #default="{ row }">
<el-image
v-if="resolveAdminMediaUrl(row.avatar)"
:src="resolveAdminMediaUrl(row.avatar)"
fit="cover"
class="table-avatar"
preview-teleported
:preview-src-list="[resolveAdminMediaUrl(row.avatar)]"
>
<template #error>
<div class="table-avatar-fail" title="图片加载失败">—</div>
</template>
</el-image>
<span v-else class="table-avatar-ph">—</span>
</template>
</el-table-column>
<el-table-column label="手机号" min-width="130" show-overflow-tooltip>
<template #default="{ row }">
{{ row.phone || "—" }}
</template>
</el-table-column>
<el-table-column prop="openid" label="OpenID(脱敏)" width="140" />
<el-table-column prop="nickname" label="昵称" width="110" />
<el-table-column label="性别" width="72">
<template #default="{ row }">
{{ genderLabel(row.gender) }}
</template>
</el-table-column>
<el-table-column prop="work_count" label="作品数" width="88" />
<el-table-column label="状态" width="88">
<template #default="{ row }">
<el-tag :type="row.status === 1 ? 'success' : 'danger'">
{{ row.status === 1 ? "正常" : "禁用" }}
</el-tag>
</template>
</el-table-column>
<el-table-column prop="create_time" label="注册时间" min-width="160" />
<el-table-column label="操作" width="200" fixed="right">
<template #default="{ row }">
<el-button type="primary" link @click="openEdit(row)">编辑</el-button>
<el-button type="danger" link @click="onDeleteUser(row)">删除</el-button>
</template>
</el-table-column>
</el-table>
<div class="pager">
<el-pagination
v-model:current-page="page"
v-model:page-size="pageSize"
:total="total"
:page-sizes="[10, 20, 50]"
layout="total, sizes, prev, pager, next"
@current-change="load"
@size-change="reload"
/>
</div>
<el-dialog
v-model="editVisible"
title="编辑用户"
width="560px"
align-center
destroy-on-close
>
<template v-if="currentRow">
<h4 class="section-title">账号信息</h4>
<el-descriptions :column="1" border size="small" class="section-block">
<el-descriptions-item label="用户 ID">{{ currentRow.id }}</el-descriptions-item>
<el-descriptions-item label="手机号">{{ currentRow.phone || "—" }}</el-descriptions-item>
<el-descriptions-item label="OpenID">{{ currentRow.openid }}</el-descriptions-item>
<el-descriptions-item label="UnionID">{{ currentRow.unionid || "—" }}</el-descriptions-item>
</el-descriptions>
<el-form :model="form" label-width="88px" class="profile-form">
<el-form-item label="头像">
<div class="avatar-upload">
<el-image
v-if="avatarEditDisplaySrc"
:src="avatarEditDisplaySrc"
fit="cover"
class="avatar-preview"
preview-teleported
:preview-src-list="[avatarEditDisplaySrc]"
>
<template #error>
<div class="avatar-preview-fail" title="图片加载失败">加载失败</div>
</template>
</el-image>
<div
v-else-if="form.avatar.trim()"
class="avatar-preview avatar-preview--empty"
>
地址无效,请重新上传
</div>
<div v-else class="avatar-preview avatar-preview--empty">未设置</div>
<el-upload
:show-file-list="false"
accept="image/jpeg,image/jpg,image/png,image/webp,image/gif"
:http-request="handleAvatarUpload"
>
<el-button type="primary" :loading="uploading">上传图片</el-button>
</el-upload>
</div>
</el-form-item>
<el-form-item label="名字" required>
<el-input
v-model="form.nickname"
maxlength="16"
show-word-limit
placeholder="必填,与小程序填写上限一致"
/>
</el-form-item>
<el-form-item label="简介">
<el-input
v-model="form.bio"
type="textarea"
:rows="3"
maxlength="120"
show-word-limit
placeholder="一句话介绍自己"
/>
</el-form-item>
<el-form-item label="性别">
<el-radio-group v-model="form.gender">
<el-radio :value="0">保密</el-radio>
<el-radio :value="1">男</el-radio>
<el-radio :value="2">女</el-radio>
</el-radio-group>
</el-form-item>
<el-form-item label="账号状态">
<el-radio-group v-model="form.status">
<el-radio :value="1">正常</el-radio>
<el-radio :value="0">禁用</el-radio>
</el-radio-group>
</el-form-item>
</el-form>
</template>
<template #footer>
<el-button @click="editVisible = false">取消</el-button>
<el-button type="primary" :loading="saving" @click="submitEdit">保存</el-button>
</template>
</el-dialog>
</el-card>
</template>
<script setup lang="ts">
import { computed, onMounted, reactive, ref } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import type { UploadRequestOptions } from "element-plus";
import {
deleteMpUser,
fetchMpUsers,
saveMpUser,
uploadAdminImage,
type MpUserRow,
} from "@/api/admin";
import { resolveAdminMediaUrl, sanitizeAvatarForSave } from "@/utils/mediaUrl";
const loading = ref(false);
const list = ref<MpUserRow[]>([]);
const keyword = ref("");
const page = ref(1);
const pageSize = ref(20);
const total = ref(0);
const editVisible = ref(false);
const saving = ref(false);
const uploading = ref(false);
const currentRow = ref<MpUserRow | null>(null);
const form = reactive({
id: 0,
nickname: "",
avatar: "",
bio: "",
gender: 0,
status: 1,
});
const avatarEditDisplaySrc = computed(() => resolveAdminMediaUrl(form.avatar));
/** 与小程序「我的资料」性别选项一致:0 保密 1 男 2 女 */
function genderLabel(g: number): string {
if (g === 1) return "男";
if (g === 2) return "女";
return "保密";
}
async function handleAvatarUpload(options: UploadRequestOptions) {
uploading.value = true;
try {
const res = await uploadAdminImage(options.file);
form.avatar = res.data.url;
options.onSuccess(res);
} catch (e) {
options.onError(e as Error);
} finally {
uploading.value = false;
}
}
async function load() {
loading.value = true;
try {
const res = await fetchMpUsers({
keyword: keyword.value.trim() || undefined,
page: page.value,
page_size: pageSize.value,
});
list.value = res.data.list;
total.value = res.data.total;
} finally {
loading.value = false;
}
}
function reload() {
page.value = 1;
void load();
}
function openEdit(row: MpUserRow) {
currentRow.value = row;
form.id = row.id;
form.nickname = row.nickname ?? "";
form.avatar = sanitizeAvatarForSave(row.avatar ?? "");
form.bio = row.bio ?? "";
form.gender = row.gender;
form.status = row.status;
editVisible.value = true;
}
async function submitEdit() {
const nick = form.nickname.trim();
if (!nick) {
ElMessage.warning("请填写名字");
return;
}
saving.value = true;
try {
await saveMpUser({
id: form.id,
nickname: nick,
avatar: sanitizeAvatarForSave(form.avatar),
bio: form.bio.trim(),
gender: form.gender,
status: form.status,
});
ElMessage.success("已保存");
editVisible.value = false;
currentRow.value = null;
await load();
} finally {
saving.value = false;
}
}
async function onDeleteUser(row: MpUserRow) {
const name = row.nickname?.trim() || row.phone || `ID ${row.id}`;
try {
await ElMessageBox.confirm(
`确定删除用户「${name}」?将一并删除该用户的全部作品、点赞及相关数据,且不可恢复。`,
"删除用户",
{ type: "warning", confirmButtonText: "删除", cancelButtonText: "取消" }
);
} catch {
return;
}
loading.value = true;
try {
await deleteMpUser(row.id);
ElMessage.success("已删除");
if (currentRow.value?.id === row.id) {
editVisible.value = false;
currentRow.value = null;
}
await load();
} finally {
loading.value = false;
}
}
onMounted(() => {
void load();
});
</script>
<style scoped>
.box {
border-radius: 12px;
}
.toolbar {
display: flex;
gap: 12px;
margin-bottom: 16px;
}
.search {
max-width: 280px;
}
.pager {
margin-top: 16px;
display: flex;
justify-content: flex-end;
}
.section-title {
margin: 0 0 12px;
font-size: 15px;
font-weight: 600;
color: #0f172a;
}
.section-title:not(:first-child) {
margin-top: 20px;
}
.section-block {
margin-bottom: 0;
}
.profile-form {
margin-top: 20px;
}
.avatar-upload {
display: flex;
flex-direction: column;
align-items: flex-start;
gap: 10px;
}
.avatar-preview {
width: 72px;
height: 72px;
border-radius: 8px;
}
.avatar-preview--empty {
display: flex;
align-items: center;
justify-content: center;
font-size: 13px;
color: #9ca3af;
background: #f3f4f6;
}
.table-avatar {
width: 40px;
height: 40px;
border-radius: 8px;
}
.table-avatar-ph {
font-size: 13px;
color: #9ca3af;
}
.table-avatar-fail {
display: flex;
width: 40px;
height: 40px;
align-items: center;
justify-content: center;
border-radius: 8px;
font-size: 14px;
color: #9ca3af;
background: #f3f4f6;
}
.avatar-preview-fail {
display: flex;
width: 72px;
height: 72px;
align-items: center;
justify-content: center;
border-radius: 8px;
font-size: 12px;
color: #9ca3af;
background: #f3f4f6;
}
</style>