@@ -0,0 +1,609 @@
< script setup lang = "ts" >
/** 独立 C 端用户管理:开户、停用、重置密码及按用户查看持久化聊天记录。 */
import { onMounted , ref } from "vue" ;
import { ElMessage } from "element-plus" ;
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 : "" } ) ;
const editing = ref < DigitalUser | null > ( null ) ,
editForm = ref ( { displayName : "" , status : "ACTIVE" } ) ;
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 : "" } ;
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 } ;
}
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: '' };
creating = true;
"
> 创建 C 端用户 < / e l - b u t t o n
>
< / 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 = "已停用" / > < / e l - s e l e c t
> < 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 > < / t e m p l a t e
> < / e l - t a b l e - c o l u m n
> < el-table-column label = "状态" width = "100"
> < template # default = "{ row }"
> < el-tag : type = "row.status === 'ACTIVE' ? 'success' : 'info'" > { {
row . status === "ACTIVE" ? "正常" : "已停用"
} } < / el-tag > < / t e m p l a t e
> < / e l - t a b l e - c o l u m n
> < 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 > < / e l - t a b l e - c o l u m n
> < el-table-column label = "最近登录" min -width = " 175 "
> < template # default = "{ row }" > { {
time ( row . lastLoginAt )
} } < / template > < / e l - t a b l e - c o l u m n
> < el-table-column label = "操作" min -width = " 235 " fixed = "right"
> < template # default = "{ row }"
> < el-button link type = "primary" @click ="openHistory(row)"
> 聊天记录 < / e l - b u t t o n
> < el-button link type = "primary" @click ="edit(row)" > 编辑 < / el -button
> < el-button
link
type = "warning"
@click ="
resetTarget = row;
newPassword = '';
"
> 重置密码 < / e l - b u t t o n
> < / t e m p l a t e
> < / e l - t a b l e - c o l u m n
> < / e l - t a b l e
>
< 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" / > < / e l - f o r m - i t e m
> < el-form-item label = "用户昵称" required
> < el-input
v-model = "createForm.displayName"
maxlength = "120" / > < / e l - f o r m - i t e m
> < 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 > < / e l - f o r m
> < template # footer
> < el-button @click ="creating = false" > 取消 < / el -button
> < el-button type = "primary" :loading = "saving" @click ="create"
> 创建用户 < / e l - b u t t o n
> < / t e m p l a t e
> < / e l - d i a l o g
>
< 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 } } < / e l - f o r m - i t e m
> < el-form-item label = "用户昵称"
> < el-input
v-model = "editForm.displayName"
maxlength = "120" / > < / e l - f o r m - i t e m
> < el-form-item label = "账号状态"
> < el-radio-group v-model = "editForm.status"
> < el-radio value = "ACTIVE" > 正常 < / e l - r a d i o
> < el-radio value = "INACTIVE" > 停用 < / el-radio > < / e l - r a d i o - g r o u p
> < / e l - f o r m - i t e m
> < / e l - f o r m
> < el-alert
title = "停用后用户无法登录,已有会话记录仍保留。"
:closable = "false"
/ > < template # footer
> < el-button @click ="editing = null" > 取消 < / el -button
> < el-button type = "primary" :loading = "saving" @click ="saveEdit"
> 保存 < / e l - b u t t o n
> < / t e m p l a t e
> < / e l - d i a l o g
>
< 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"
> 确认重置 < / e l - b u t t o n
> < / t e m p l a t e
> < / e l - d i a l o g
>
< 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 } } < / s t r o n g
> < span > { { c . title } } < / s p a n
> < small
> { { c . messages } } 条消息 · { { time ( c . updatedAt ) } } < / s m a l l
> < / b u t t o n
> < 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 } } < / s t r o n g
> < el-button size = "small" @click ="loadMessages"
> 刷新记录 < / e l - b u t t o n
>
< / 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
} } < / s t r o n g
> < time > { { time ( m . createdAt ) } } < / t i m e
> < el-tag
v-if = "m.status !== 'complete'"
size = "small"
type = "info"
> { { states [ m . status ] ? ? m . status } } < / e l - t a g
> < el-tag v-if = "m.pinned" size="small" > 已固定 < / el -tag >
< / div >
< p > { { m . content || "(未生成正文)" } } < / p >
< / article >
< / div >
< el-pagination
v -model :current-page = "messagePage"
:page-size = "30"
:total = "messageTotal"
layout = "total, prev, pager, next"
@ current -change = " loadMessages " / > < / t e m p l a t e
> < el-empty v-else description = "选择左侧会话查看聊天内容" / >
< / section >
< / div >
< / el-dialog >
< / div >
< / template >
< style scoped >
. heading ,
. filters ,
. message - pane header {
display : flex ;
gap : 12 px ;
align - items : center ;
justify - content : space - between ;
margin - bottom : 18 px ;
}
h2 {
margin : 0 0 8 px ;
font - size : 24 px ;
}
. secondary ,
. heading p {
margin : 6 px 0 ;
font - size : 12 px ;
color : var ( -- el - text - color - secondary ) ;
}
. filters . el - input {
max - width : 420 px ;
}
. filters . el - select {
width : 160 px ;
}
. filters {
justify - content : flex - start ;
}
. el - pagination {
justify - content : flex - end ;
margin - top : 16 px ;
}
. history - layout {
display : grid ;
grid - template - columns : 280 px minmax ( 0 , 1 fr ) ;
gap : 20 px ;
height : 72 vh ;
}
. conversation - list ,
. message - pane {
min - height : 0 ;
overflow : auto ;
}
. conversation - list {
padding - right : 12 px ;
border - right : 1 px solid var ( -- el - border - color - light ) ;
}
. conversation {
display : flex ;
flex - direction : column ;
gap : 6 px ;
width : 100 % ;
padding : 14 px 10 px ;
color : var ( -- el - text - color - primary ) ;
text - align : left ;
cursor : pointer ;
background : transparent ;
border : 0 ;
border - bottom : 1 px solid var ( -- el - border - color - lighter ) ;
}
. conversation . active {
color : var ( -- el - color - primary ) ;
background : var ( -- el - color - primary - light - 9 ) ;
}
. conversation small {
font - size : 11 px ;
color : var ( -- el - text - color - secondary ) ;
}
. message - pane {
display : flex ;
flex - direction : column ;
}
. message - pane header {
margin - bottom : 8 px ;
}
. messages {
flex : 1 ;
min - height : 0 ;
padding : 8 px ;
overflow : auto ;
}
. messages article {
margin - bottom : 18 px ;
}
. message - meta {
display : flex ;
flex - wrap : wrap ;
gap : 10 px ;
align - items : center ;
font - size : 12 px ;
}
. message - meta time {
color : var ( -- el - text - color - secondary ) ;
}
. messages article p {
padding : 12 px 14 px ;
margin : 6 px 0 ;
overflow - wrap : anywhere ;
white - space : pre - wrap ;
background : var ( -- el - fill - color - light ) ;
border - radius : 6 px ;
}
. messages article . user p {
background : var ( -- el - color - primary - light - 9 ) ;
}
. conversation : focus - visible {
outline : 2 px solid var ( -- el - color - primary ) ;
outline - offset : - 2 px ;
}
@ media ( width <= 760 px ) {
. heading ,
. filters {
flex - direction : column ;
align - items : stretch ;
}
. filters . el - input ,
. filters . el - select {
width : 100 % ;
max - width : none ;
}
. history - layout {
grid - template - rows : 180 px minmax ( 0 , 1 fr ) ;
grid - template - columns : 1 fr ;
}
. conversation - list {
padding - right : 0 ;
border - right : 0 ;
border - bottom : 1 px solid var ( -- el - border - color - light ) ;
}
}
< / style >