Files
gao-customer-h5/src/pages/ai/components/AiConversationList.vue
T
2026-09-22 15:39:50 +08:00

368 lines
8.3 KiB
Vue

<template>
<t-popup
v-model="popupVisible"
placement="right"
destroy-on-close
:z-index="popupZIndex"
:overlay-props="{ zIndex: popupZIndex - 1 }"
>
<section class="ai-conversation-list" @scroll="handleScroll">
<header class="header glass-white">
<h3>历史会话</h3>
<t-button
theme="danger"
shape="circle"
variant="text"
@click="popupVisible = false"
>
<template #icon>
<t-icon name="close" />
</template>
</t-button>
</header>
<t-cell-group v-if="conversationList.length" class="items fix-last-cell-line">
<t-swipe-cell
v-for="(item, index) in conversationList"
:key="item.id"
class="item"
v-entrance-transition="index * 30"
>
<t-cell
class="cell"
:class="{ active: item.id === props.activeConversationId }"
:disabled="props.disabled"
@click="selectConversation(item)"
>
<template #leftIcon>
<div class="icon">
<t-icon name="chat" size="1.25rem" />
</div>
</template>
<template #title>
<span class="title" v-text="getConversationName(item)" />
</template>
<template #description>
<pass-time-label :timestamp="formatConversationTime(item)" underline />
</template>
</t-cell>
<template #right>
<t-button
class="swipe-cell-button"
theme="danger"
:loading="deletingId === item.id"
:disabled="props.disabled"
@click.stop="confirmDelete(item)"
>
删除
</t-button>
</template>
</t-swipe-cell>
</t-cell-group>
<t-empty v-else-if="!isLoading" class="center-tips" description="暂无历史会话" />
<t-loading
v-if="isLoading && !conversationList.length"
class="center-tips"
size="1.5rem"
text="正在加载会话"
/>
<div class="footer glass-white">
<span class="llm gray">Deepseek v4.1-flash</span>
<t-button
class="agreement-button"
variant="text"
size="small"
type="button"
content="服务协议"
/>
</div>
</section>
</t-popup>
</template>
<script setup lang="ts">
import { DialogPlugin } from "tdesign-mobile-vue";
import { deleteConversation as removeConversation, type GaoAiConversation, listConversations } from "@/api/GaoAiAPI";
import { getErrorMessage } from "@/utils/error";
import { Message } from "@/utils/message";
import { PassTimeLabel } from "timi-web";
defineOptions({
name: "AiConversationList"
});
interface Props {
visible: boolean;
activeConversationId: string;
disabled: boolean;
sessionKey: string;
}
const props = defineProps<Props>();
const emit = defineEmits<{
"update:visible": [value: boolean];
select: [conversation: GaoAiConversation];
delete: [conversationId: string];
}>();
const popupZIndex = 4000;
const deleteDialogZIndex = 12000;
const pageSize = 20;
const popupVisible = computed({
get() {
return props.visible;
},
set(value: boolean) {
emit("update:visible", value);
}
});
const conversationList = ref<GaoAiConversation[]>([]);
const isLoading = ref(false);
const isNoMore = ref(false);
const lastId = ref("");
const deletingId = ref("");
let requestToken = 0;
let loadingPromise: Promise<void> | undefined;
async function loadMore(): Promise<void> {
if (loadingPromise) {
await loadingPromise;
return;
}
if (isNoMore.value) {
return;
}
const currentToken = requestToken;
const request = (async () => {
isLoading.value = true;
try {
const result = await listConversations({
lastId: lastId.value || undefined,
limit: pageSize,
sortBy: "-updated_at"
});
if (currentToken !== requestToken) {
return;
}
const nextList = (result.data || []).filter(item => item.id);
const idSet = new Set(conversationList.value.map(item => item.id));
conversationList.value = conversationList.value.concat(nextList.filter(item => !idSet.has(item.id)));
lastId.value = nextList[nextList.length - 1]?.id || lastId.value;
isNoMore.value = result["has_more"] !== true || !nextList.length;
} catch (error) {
if (currentToken === requestToken) {
Message.error(getErrorMessage(error, "历史会话加载失败"));
}
} finally {
if (currentToken === requestToken) {
isLoading.value = false;
}
}
})();
loadingPromise = request;
try {
await request;
} finally {
if (loadingPromise === request) {
loadingPromise = undefined;
}
}
}
async function reloadList(): Promise<void> {
const currentToken = requestToken + 1;
requestToken = currentToken;
conversationList.value = [];
lastId.value = "";
isNoMore.value = false;
if (loadingPromise) {
await loadingPromise;
}
if (currentToken === requestToken) {
await loadMore();
}
}
async function handleScroll(event: Event): Promise<void> {
const target = event.currentTarget;
if (!(target instanceof HTMLElement)) {
return;
}
if (target.scrollHeight - target.scrollTop - target.clientHeight < 40) {
await loadMore();
}
}
function selectConversation(conversation: GaoAiConversation): void {
if (props.disabled || !conversation.id) {
return;
}
emit("select", conversation);
emit("update:visible", false);
}
async function confirmDelete(conversation: GaoAiConversation): Promise<void> {
if (props.disabled || deletingId.value || !conversation.id) {
return;
}
const conversationId = conversation.id;
const dialog = DialogPlugin.confirm({
title: "删除历史会话",
content: "确定删除这段历史会话吗?删除后无法恢复。",
cancelBtn: "取消",
confirmBtn: "删除",
zIndex: deleteDialogZIndex,
overlayProps: { zIndex: deleteDialogZIndex },
onCancel() {
dialog.destroy();
},
async onConfirm() {
dialog.destroy();
await deleteItem(conversationId);
}
});
}
async function deleteItem(conversationId: string): Promise<void> {
deletingId.value = conversationId;
try {
await removeConversation(conversationId);
emit("delete", conversationId);
await reloadList();
Message.success("历史会话已删除");
} catch (error) {
Message.error(getErrorMessage(error, "历史会话删除失败"));
} finally {
deletingId.value = "";
}
}
function getConversationName(conversation: GaoAiConversation): string {
return conversation.name?.trim() || conversation.introduction?.trim() || "未命名会话";
}
function formatConversationTime(conversation: GaoAiConversation): number {
const value = conversation["updated_at"] || conversation["created_at"];
const timestamp = typeof value === "number" ? value : Number(value);
if (!Number.isFinite(timestamp) || timestamp <= 0) {
return -1;
}
return timestamp < 100000000000 ? timestamp * 1000 : timestamp;
}
watch(() => props.visible, async visible => {
if (visible) {
await reloadList();
}
});
watch(() => props.sessionKey, async () => {
requestToken += 1;
conversationList.value = [];
lastId.value = "";
isNoMore.value = false;
if (props.visible) {
await reloadList();
}
});
</script>
<style scoped lang="less">
.ai-conversation-list {
width: min(70vw, 24rem);
height: 100vh;
padding: calc(var(--app-nav-height) + .5rem) 0 calc(1rem + env(safe-area-inset-bottom, 0px));
display: flex;
overflow: auto;
position: relative;
flex-direction: column;
.header {
top: 0;
z-index: 1;
display: flex;
position: sticky;
box-shadow: var(--td-shadow-1);
align-items: center;
justify-content: space-between;
h3 {
margin: .5rem 0 .5rem 1rem;
}
.t-button {
gap: .25rem;
display: inline-flex;
align-items: center;
}
}
.items {
flex: 1;
.item {
.cell {
&.active {
background: var(--td-bg-color-secondarycontainer);
}
:deep(.t-cell__title) {
min-width: 0;
}
.icon {
color: #0f766e;
width: 2rem;
height: 2rem;
border-radius: .625rem;
display: flex;
background: #ccfbf1;
align-items: center;
justify-content: center;
}
.title {
overflow: hidden;
display: block;
white-space: nowrap;
text-overflow: ellipsis;
}
}
.delete {
height: 100%;
}
}
}
.footer {
bottom: 0;
display: flex;
padding: 0 .5rem;
position: sticky;
box-shadow: 0 -2px 10px rgba(0, 0, 0, .03);
align-items: center;
justify-content: space-between;
.llm {
font-size: .875rem;
}
.agreement-button {
}
}
}
:global(html.theme-dark) {
.conversation-drawer {
background: #0f172a;
.icon {
color: #5eead4;
background: rgba(20, 184, 166, .15);
}
}
}
</style>