Files
role-admin/src/views/digital/users.vue
T
湛兮 6d3f6a0a8e fix(users): 标记前台已删除的历史会话
- 会话摘要增加软删除时间字段
- 后台聊天列表显示前台已删除状态并保留消息查看入口
2026-10-28 16:08:40 +08:00

664 lines
19 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
/** 独立 C 端用户管理:开户、内容偏好、停用、改密与区分对白/叙述的持久化聊天记录。 */
import { onMounted, ref } from "vue";
import { ElMessage } from "element-plus";
import DigitalMessageText from "@/components/DigitalMessageText.vue";
import {
digital,
type DigitalUser,
type UserConversation,
type UserChatMessage
} from "@/api/access";
const q = ref(""),
status = ref(""),
page = ref(1),
total = ref(0),
loading = ref(false),
users = ref<DigitalUser[]>([]);
const creating = ref(false),
saving = ref(false),
createForm = ref({
username: "",
displayName: "",
password: "",
nsfwEnabled: true
});
const editing = ref<DigitalUser | null>(null),
editForm = ref({ displayName: "", status: "ACTIVE", nsfwEnabled: true });
const resetTarget = ref<DigitalUser | null>(null),
newPassword = ref("");
const selected = ref<DigitalUser | null>(null),
history = ref(false),
conversations = ref<UserConversation[]>([]),
conversationPage = ref(1),
conversationTotal = ref(0),
conversationLoading = ref(false);
const active = ref<UserConversation | null>(null),
messages = ref<UserChatMessage[]>([]),
messagePage = ref(1),
messageTotal = ref(0),
messageLoading = ref(false);
let listVersion = 0,
chatVersion = 0,
messageVersion = 0;
const time = (value: string | null) =>
value ? new Date(value).toLocaleString() : "—";
const states: Record<string, string> = {
complete: "已完成",
stopped: "已停止",
failed: "生成失败",
generating: "生成中",
superseded: "旧版本"
};
/** 请求编号避免快速切换用户时把旧请求结果写进新用户的聊天面板。 */
async function load() {
const version = ++listVersion;
loading.value = true;
try {
const params = new URLSearchParams({
q: q.value,
page: String(page.value),
size: "20"
});
if (status.value) params.set("status", status.value);
const r = await digital<{ items: DigitalUser[]; total: number }>(
`users?${params}`
);
if (version === listVersion) {
users.value = r.items;
total.value = r.total;
}
} catch (e) {
ElMessage.error(e instanceof Error ? e.message : "加载失败");
} finally {
if (version === listVersion) loading.value = false;
}
}
function search() {
page.value = 1;
load();
}
async function create() {
if (
!createForm.value.username.trim() ||
!createForm.value.displayName.trim() ||
createForm.value.password.length < 8
)
return ElMessage.warning("请填写账号、昵称及至少8位密码");
saving.value = true;
try {
await digital("users", "post", createForm.value);
creating.value = false;
createForm.value = {
username: "",
displayName: "",
password: "",
nsfwEnabled: true
};
search();
ElMessage.success("C 端账号已创建,可在星港前台登录");
} catch (e) {
ElMessage.error(e instanceof Error ? e.message : "创建失败");
} finally {
saving.value = false;
}
}
function edit(user: DigitalUser) {
editing.value = user;
editForm.value = {
displayName: user.displayName,
status: user.status,
nsfwEnabled: user.nsfwEnabled ?? true
};
}
async function saveEdit() {
if (!editing.value) return;
saving.value = true;
try {
await digital(`users/${editing.value.id}`, "patch", editForm.value);
editing.value = null;
await load();
ElMessage.success("用户资料已保存");
} catch (e) {
ElMessage.error(e instanceof Error ? e.message : "保存失败");
} finally {
saving.value = false;
}
}
async function resetPassword() {
if (!resetTarget.value) return;
if (newPassword.value.length < 8) return ElMessage.warning("密码至少8位");
saving.value = true;
try {
await digital(`users/${resetTarget.value.id}/password`, "post", {
password: newPassword.value
});
newPassword.value = "";
resetTarget.value = null;
ElMessage.success("密码已重置,该用户需要重新登录");
} catch (e) {
ElMessage.error(e instanceof Error ? e.message : "重置失败");
} finally {
saving.value = false;
}
}
async function openHistory(user: DigitalUser) {
selected.value = user;
history.value = true;
conversationPage.value = 1;
active.value = null;
messages.value = [];
++messageVersion;
await loadConversations();
}
async function loadConversations() {
if (!selected.value) return;
const version = ++chatVersion;
conversationLoading.value = true;
conversations.value = [];
try {
const r = await digital<{ items: UserConversation[]; total: number }>(
`users/${selected.value.id}/conversations?page=${conversationPage.value}&size=20`
);
if (version === chatVersion) {
conversations.value = r.items;
conversationTotal.value = r.total;
}
} catch (e) {
if (version === chatVersion)
ElMessage.error(e instanceof Error ? e.message : "会话加载失败");
} finally {
if (version === chatVersion) conversationLoading.value = false;
}
}
async function chooseConversation(c: UserConversation) {
active.value = c;
messagePage.value = 1;
await loadMessages();
}
async function loadMessages() {
if (!selected.value || !active.value) return;
const version = ++messageVersion;
messageLoading.value = true;
messages.value = [];
try {
const r = await digital<{ items: UserChatMessage[]; total: number }>(
`users/${selected.value.id}/conversations/${active.value.id}/messages?page=${messagePage.value}&size=30`
);
if (version === messageVersion) {
messages.value = r.items;
messageTotal.value = r.total;
}
} catch (e) {
if (version === messageVersion)
ElMessage.error(e instanceof Error ? e.message : "聊天记录加载失败");
} finally {
if (version === messageVersion) messageLoading.value = false;
}
}
function closeHistory() {
++chatVersion;
++messageVersion;
selected.value = null;
active.value = null;
messages.value = [];
}
onMounted(load);
</script>
<template>
<div class="customer-page">
<div class="heading">
<div>
<h2>用户管理</h2>
<p>C 端账号独立于后台管理员,由后台开通。聊天记录按用户和会话查看。</p>
</div>
<el-button
type="primary"
@click="
createForm = {
username: '',
displayName: '',
password: '',
nsfwEnabled: true
};
creating = true;
"
>创建 C 端用户</el-button
>
</div>
<el-card shadow="never"
><div class="filters">
<el-input
v-model="q"
clearable
placeholder="搜索账号、昵称或用户 ID"
@keyup.enter="search"
@clear="search"
/><el-select
v-model="status"
clearable
placeholder="全部状态"
@change="search"
><el-option value="ACTIVE" label="正常" /><el-option
value="INACTIVE"
label="已停用" /></el-select
><el-button :loading="loading" @click="search">查询</el-button>
</div>
<el-table
v-loading="loading"
:data="users"
empty-text="暂无 C 端用户,可点击上方创建"
><el-table-column label="用户" min-width="220"
><template #default="{ row }"
><strong>{{ row.displayName }}</strong>
<p class="secondary">
{{ row.username }} · UID {{ row.id }}
</p></template
></el-table-column
><el-table-column label="状态" width="100"
><template #default="{ row }"
><el-tag :type="row.status === 'ACTIVE' ? 'success' : 'info'">{{
row.status === "ACTIVE" ? "正常" : "已停用"
}}</el-tag></template
></el-table-column
><el-table-column label="NSFW" width="100"
><template #default="{ row }"
><el-tag :type="row.nsfwEnabled === false ? 'info' : 'success'">{{
row.nsfwEnabled === false ? "已关闭" : "已开启"
}}</el-tag></template
></el-table-column
><el-table-column
prop="conversations"
label="会话数"
width="90"
/><el-table-column
prop="messages"
label="消息数"
width="90"
/><el-table-column label="创建时间" min-width="175"
><template #default="{ row }">{{
time(row.createdAt)
}}</template></el-table-column
><el-table-column label="最近登录" min-width="175"
><template #default="{ row }">{{
time(row.lastLoginAt)
}}</template></el-table-column
><el-table-column label="操作" min-width="235" fixed="right"
><template #default="{ row }"
><el-button link type="primary" @click="openHistory(row)"
>聊天记录</el-button
><el-button link type="primary" @click="edit(row)">编辑</el-button
><el-button
link
type="warning"
@click="
resetTarget = row;
newPassword = '';
"
>重置密码</el-button
></template
></el-table-column
></el-table
>
<el-pagination
v-model:current-page="page"
:page-size="20"
:total="total"
layout="total, prev, pager, next"
@current-change="load"
/>
</el-card>
<el-dialog
v-model="creating"
title="创建 C 端用户"
width="min(520px,95vw)"
:close-on-click-modal="false"
><el-form label-position="top"
><el-form-item label="登录账号" required
><el-input
v-model="createForm.username"
maxlength="120"
placeholder="字母、数字、手机号或邮箱"
autocomplete="off" /></el-form-item
><el-form-item label="用户昵称" required
><el-input
v-model="createForm.displayName"
maxlength="120" /></el-form-item
><el-form-item label="初始密码" required
><el-input
v-model="createForm.password"
type="password"
show-password
maxlength="128"
placeholder="至少8位"
autocomplete="new-password" /></el-form-item
><el-form-item label="NSFW 成人内容"
><el-switch
v-model="createForm.nsfwEnabled"
active-text="开启"
inactive-text="关闭"
/>
<p class="secondary">
登录账号默认开启,用户可在前台保存自己的选择。
</p>
</el-form-item></el-form
><template #footer
><el-button @click="creating = false">取消</el-button
><el-button type="primary" :loading="saving" @click="create"
>创建用户</el-button
></template
></el-dialog
>
<el-dialog
:model-value="!!editing"
title="编辑 C 端用户"
width="min(520px,95vw)"
@close="editing = null"
><el-form label-position="top"
><el-form-item label="登录账号">{{ editing?.username }}</el-form-item
><el-form-item label="用户昵称"
><el-input
v-model="editForm.displayName"
maxlength="120" /></el-form-item
><el-form-item label="账号状态"
><el-radio-group v-model="editForm.status"
><el-radio value="ACTIVE">正常</el-radio
><el-radio value="INACTIVE">停用</el-radio></el-radio-group
></el-form-item
><el-form-item label="NSFW 成人内容"
><el-switch
v-model="editForm.nsfwEnabled"
active-text="开启"
inactive-text="关闭"
/>
<p class="secondary">保存后用于该账号的后续回复。</p>
</el-form-item></el-form
><el-alert
title="停用后用户无法登录,已有会话记录仍保留。"
:closable="false"
/><template #footer
><el-button @click="editing = null">取消</el-button
><el-button type="primary" :loading="saving" @click="saveEdit"
>保存</el-button
></template
></el-dialog
>
<el-dialog
:model-value="!!resetTarget"
title="重置 C 端用户密码"
width="min(520px,95vw)"
@close="
resetTarget = null;
newPassword = '';
"
><p>{{ resetTarget?.displayName }} · {{ resetTarget?.username }}</p>
<el-input
v-model="newPassword"
type="password"
show-password
maxlength="128"
autocomplete="new-password"
placeholder="设置至少8位新密码"
/>
<p class="secondary">
重置后该 C 端账号的旧登录状态失效,不影响同名后台账号。
</p>
<template #footer
><el-button @click="resetTarget = null">取消</el-button
><el-button type="primary" :loading="saving" @click="resetPassword"
>确认重置</el-button
></template
></el-dialog
>
<el-dialog
v-model="history"
:title="`${selected?.displayName ?? ''} · 聊天记录`"
width="min(1280px,96vw)"
top="4vh"
@closed="closeHistory"
>
<div class="history-layout">
<aside v-loading="conversationLoading" class="conversation-list">
<p class="secondary">
{{ selected?.username }} · {{ conversationTotal }} 个会话
</p>
<el-empty
v-if="!conversations.length && !conversationLoading"
description="暂无聊天记录"
/><button
v-for="c in conversations"
:key="c.id"
class="conversation"
:class="{ active: active?.id === c.id }"
@click="chooseConversation(c)"
>
<strong>{{ c.characterName }}</strong
><span
>{{ c.title
}}<el-tag v-if="c.deletedAt" size="small" type="info"
>前台已删除</el-tag
></span
><small
>{{ c.messages }} 条消息 · {{ time(c.updatedAt) }}</small
></button
><el-pagination
v-model:current-page="conversationPage"
small
:page-size="20"
:total="conversationTotal"
layout="prev, pager, next"
@current-change="loadConversations"
/>
</aside>
<section v-loading="messageLoading" class="message-pane">
<template v-if="active"
><header>
<strong>{{ active.characterName }} · {{ active.title }}</strong
><el-button size="small" @click="loadMessages"
>刷新记录</el-button
>
</header>
<p class="secondary">
第1页为最新记录,每页按发送顺序展示;较早内容可翻页查看。共
{{ messageTotal }} 条。
</p>
<div class="messages">
<el-empty
v-if="!messages.length && !messageLoading"
description="此会话尚无消息"
/>
<article v-for="m in messages" :key="m.id" :class="m.role">
<div class="message-meta">
<strong>{{
m.role === "user"
? selected?.displayName
: active.characterName
}}</strong
><time>{{ time(m.createdAt) }}</time
><el-tag
v-if="m.status !== 'complete'"
size="small"
type="info"
>{{ states[m.status] ?? m.status }}</el-tag
><el-tag v-if="m.pinned" size="small">已固定</el-tag>
</div>
<DigitalMessageText
class="history-message-body"
:text="m.content"
:role="m.role"
:incomplete="
['generating', 'stopped', 'failed'].includes(m.status)
"
/>
</article>
</div>
<el-pagination
v-model:current-page="messagePage"
:page-size="30"
:total="messageTotal"
layout="total, prev, pager, next"
@current-change="loadMessages" /></template
><el-empty v-else description="选择左侧会话查看聊天内容" />
</section>
</div>
</el-dialog>
</div>
</template>
<style scoped>
.heading,
.filters,
.message-pane header {
display: flex;
gap: 12px;
align-items: center;
justify-content: space-between;
margin-bottom: 18px;
}
h2 {
margin: 0 0 8px;
font-size: 24px;
}
.secondary,
.heading p {
margin: 6px 0;
font-size: 12px;
color: var(--el-text-color-secondary);
}
.filters .el-input {
max-width: 420px;
}
.filters .el-select {
width: 160px;
}
.filters {
justify-content: flex-start;
}
.el-pagination {
justify-content: flex-end;
margin-top: 16px;
}
.history-layout {
display: grid;
grid-template-columns: 280px minmax(0, 1fr);
gap: 20px;
height: 72vh;
}
.conversation-list,
.message-pane {
min-height: 0;
overflow: auto;
}
.conversation-list {
padding-right: 12px;
border-right: 1px solid var(--el-border-color-light);
}
.conversation {
display: flex;
flex-direction: column;
gap: 6px;
width: 100%;
padding: 14px 10px;
color: var(--el-text-color-primary);
text-align: left;
cursor: pointer;
background: transparent;
border: 0;
border-bottom: 1px solid var(--el-border-color-lighter);
}
.conversation.active {
color: var(--el-color-primary);
background: var(--el-color-primary-light-9);
}
.conversation small {
font-size: 11px;
color: var(--el-text-color-secondary);
}
.message-pane {
display: flex;
flex-direction: column;
}
.message-pane header {
margin-bottom: 8px;
}
.messages {
flex: 1;
min-height: 0;
padding: 8px;
overflow: auto;
}
.messages article {
margin-bottom: 18px;
}
.message-meta {
display: flex;
flex-wrap: wrap;
gap: 10px;
align-items: center;
font-size: 12px;
}
.message-meta time {
color: var(--el-text-color-secondary);
}
.messages article .history-message-body {
padding: 12px 14px;
margin: 6px 0;
overflow-wrap: anywhere;
white-space: pre-wrap;
background: var(--el-fill-color-light);
border-radius: 6px;
}
.messages article.user .history-message-body {
background: var(--el-color-primary-light-9);
}
.conversation:focus-visible {
outline: 2px solid var(--el-color-primary);
outline-offset: -2px;
}
@media (width<=760px) {
.heading,
.filters {
flex-direction: column;
align-items: stretch;
}
.filters .el-input,
.filters .el-select {
width: 100%;
max-width: none;
}
.history-layout {
grid-template-rows: 180px minmax(0, 1fr);
grid-template-columns: 1fr;
}
.conversation-list {
padding-right: 0;
border-right: 0;
border-bottom: 1px solid var(--el-border-color-light);
}
}
</style>