refactor: 智能助手页对齐 ChatGPT 式侧栏分组与居中空态布局

This commit is contained in:
2026-09-25 23:38:13 +08:00
parent 0b460e22b0
commit a72a43cffc
2 changed files with 234 additions and 122 deletions
@@ -292,7 +292,7 @@ onBeforeUnmount(() => {
v-model="draft"
class="agent-td-sender"
:loading="loading"
placeholder="输入问题,@ 引用店铺、商品或 SKU"
placeholder="问点什么,或用 @ 引用店铺 / 商品 / SKU"
:textarea-props="{ autosize: { minRows: 2, maxRows: 6 } }"
@send="emit('send', $event)"
@stop="emit('stop')"
@@ -1,10 +1,17 @@
<script lang="ts" setup>
import { computed, nextTick, onBeforeUnmount, ref, watch } from 'vue';
import { Page } from '@vben/common-ui';
import { PlusOutlined } from '@ant-design/icons-vue';
import { EllipsisText, Page } from '@vben/common-ui';
import {
BarChartOutlined,
CalendarOutlined,
FundOutlined,
PlusOutlined,
RiseOutlined,
} from '@ant-design/icons-vue';
import dayjs from 'dayjs';
import { Button, Empty, Select, Spin, message } from 'ant-design-vue';
import { Empty, Select, Spin, Tooltip, message } from 'ant-design-vue';
import { ChatItem } from '@tdesign-vue-next/chat';
import 'tdesign-vue-next/es/style/index.css';
import '@tdesign-vue-next/chat/es/style/index.css';
@@ -28,14 +35,32 @@ import {
defineOptions({ name: 'AgentChat' });
/** 新对话空态快捷提示:点击只填入输入框,不直接发送 */
/** 空态快捷 chips:短标签展示,点击填入完整提问 */
const starterPrompts = [
'近7天的销售额、单量、净利和费比分别是多少',
'昨天的经营情况怎么样',
'本周销售额和净利帮我汇总一下',
'近30天单量和费比有什么变化',
{
label: '近7天经营概览',
prompt: '近7天的销售额、单量、净利和费比分别是多少',
icon: BarChartOutlined,
},
{
label: '昨天经营情况',
prompt: '昨天的经营情况怎么样',
icon: CalendarOutlined,
},
{
label: '本周销售与净利',
prompt: '本周销售额和净利帮我汇总一下',
icon: RiseOutlined,
},
{
label: '近30天费比变化',
prompt: '近30天单量和费比有什么变化',
icon: FundOutlined,
},
];
type SessionGroup = { key: string; label: string; items: AgentVO.Session[] };
/** 消息气泡视图模型(走 Vue ChatItem,避免 ChatList→WebComponent 丢正文) */
type BubbleView = {
id: string;
@@ -110,7 +135,7 @@ const showMessageLoading = computed(
() => loadingMessages.value && !sending.value && messages.value.length === 0,
);
/** 新对话草稿,或还没有任何会话时,展示能力引导 */
/** 新对话草稿,或还没有任何会话时,展示居中空态 */
const showStarter = computed(
() =>
messages.value.length === 0 &&
@@ -119,6 +144,53 @@ const showStarter = computed(
(!loadingSessions.value && !activeThreadId.value)),
);
const greeting = computed(() => {
const h = dayjs().hour();
if (h < 12) return '早上好,今天想看点什么?';
if (h < 18) return '下午好,今天想看点什么?';
return '晚上好,今天想看点什么?';
});
function sessionTime(s: AgentVO.Session) {
const raw = s.lastMessageAt || s.updatedAt;
const d = raw ? dayjs(raw) : null;
return d?.isValid() ? d : null;
}
/** 侧栏按「今天 / 昨天 / 近7天 / 更早」分组,对齐常见助手布局 */
const sessionGroups = computed<SessionGroup[]>(() => {
const buckets: Record<string, AgentVO.Session[]> = {
today: [],
yesterday: [],
week: [],
earlier: [],
};
const startToday = dayjs().startOf('day');
const startYesterday = startToday.subtract(1, 'day');
const startWeek = startToday.subtract(6, 'day');
for (const s of sessions.value) {
const t = sessionTime(s);
if (!t || !t.isBefore(startToday)) {
buckets.today.push(s);
} else if (!t.isBefore(startYesterday)) {
buckets.yesterday.push(s);
} else if (!t.isBefore(startWeek)) {
buckets.week.push(s);
} else {
buckets.earlier.push(s);
}
}
const labels: Array<[string, string]> = [
['today', '今天'],
['yesterday', '昨天'],
['week', '近 7 天'],
['earlier', '更早'],
];
return labels
.map(([key, label]) => ({ key, label, items: buckets[key]! }))
.filter((g) => g.items.length > 0);
});
const bubbleViews = computed<BubbleView[]>(() =>
messages.value.map((m, idx) => {
const isStreamingAssistant =
@@ -469,15 +541,18 @@ void loadEnabledModels().then(() => refreshSessions());
<template>
<Page auto-content-height content-class="!p-0">
<div class="flex h-full min-h-0 w-full overflow-hidden">
<aside
class="flex w-64 shrink-0 flex-col border-r border-border bg-card"
>
<div class="border-b border-border p-3">
<Button type="primary" block @click="beginNewChat">
<template #icon><PlusOutlined /></template>
<div class="agent-shell flex h-full min-h-0 w-full overflow-hidden">
<!-- 左轨:新对话 + 按日分组的历史 -->
<aside class="agent-sider flex w-[260px] shrink-0 flex-col border-r border-border">
<div class="flex items-center gap-2 px-3 pb-2 pt-3">
<button
type="button"
class="hover:bg-muted flex h-9 flex-1 items-center gap-2 rounded-lg px-3 text-sm font-medium transition-colors"
@click="beginNewChat"
>
<PlusOutlined />
新对话
</Button>
</button>
</div>
<div class="relative min-h-0 flex-1">
<div
@@ -488,131 +563,82 @@ void loadEnabledModels().then(() => refreshSessions());
</div>
<Empty
v-if="!loadingSessions && sessions.length === 0 && !composingNew"
class="mt-10 px-2"
class="mt-12 px-3"
description="暂无会话"
/>
<div v-else class="h-full space-y-1 overflow-y-auto px-3 py-2">
<div v-else class="h-full overflow-y-auto px-2 pb-4">
<button
v-if="composingNew"
type="button"
class="bg-primary/10 text-primary mb-1 w-full rounded-lg px-3 py-2 text-left text-sm"
class="bg-muted mb-3 w-full rounded-lg px-3 py-2 text-left text-sm font-medium"
>
<div class="truncate font-medium">新对话</div>
<div class="text-muted-foreground mt-0.5 truncate text-xs">
尚未发送
</div>
新对话
</button>
<button
v-for="s in sessions"
:key="s.threadId"
type="button"
class="w-full rounded-lg px-3 py-2 text-left text-sm transition-colors"
:class="
s.threadId === activeThreadId
? 'bg-primary/10 text-primary'
: 'hover:bg-muted text-foreground'
"
@click="onSessionClick(s.threadId)"
<div
v-for="group in sessionGroups"
:key="group.key"
class="mb-3"
>
<div class="truncate font-medium">{{ s.title || '新对话' }}</div>
<div
v-if="s.updatedAt || s.lastMessageAt"
class="text-muted-foreground mt-0.5 truncate text-xs"
class="text-muted-foreground px-3 pb-1 pt-2 text-xs font-medium"
>
{{ s.updatedAt || s.lastMessageAt }}
{{ group.label }}
</div>
</button>
<button
v-for="s in group.items"
:key="s.threadId"
type="button"
class="mb-0.5 w-full rounded-lg px-3 py-2 text-left text-sm transition-colors"
:class="
s.threadId === activeThreadId
? 'bg-muted font-medium'
: 'hover:bg-muted/70 text-foreground'
"
@click="onSessionClick(s.threadId)"
>
<EllipsisText :tooltip-when-ellipsis="true" class="w-full">
{{ s.title || '新对话' }}
</EllipsisText>
</button>
</div>
</div>
</div>
</aside>
<section class="agent-chat-main flex min-h-0 min-w-0 flex-1 flex-col">
<!-- 主区:空态居中 / 对话时消息 + 底栏输入 -->
<section class="agent-main relative flex min-h-0 min-w-0 flex-1 flex-col">
<header
class="flex h-12 shrink-0 items-center gap-3 border-b border-border px-4"
class="absolute right-4 top-3 z-20 flex items-center gap-2"
>
<span class="min-w-0 flex-1 truncate font-medium">
{{
composingNew
? '新对话'
: activeSession?.title || '智能助手'
}}
</span>
<Select
:value="selectedModelKey"
:options="modelOptions"
:loading="patchingModel"
:disabled="sending || enabledModels.length === 0"
placeholder="选择模型"
class="w-64 shrink-0"
placeholder="模型"
size="small"
class="agent-model-select w-48"
:allow-clear="false"
@change="(v) => onModelChange(String(v ?? ''))"
/>
</header>
<div class="relative flex min-h-0 flex-1 flex-col">
<div
v-if="showMessageLoading"
class="text-muted-foreground absolute inset-0 z-10 flex items-center justify-center bg-background/60 text-sm"
>
加载消息…
</div>
<Empty
v-if="!showStarter && !showMessageLoading && messages.length === 0"
class="absolute inset-0 flex items-center justify-center"
description="开始提问吧(需先在「模型管理」配置 API)"
/>
<div
ref="messagePaneRef"
class="agent-td-chat t-chat t-chat--normal min-h-0 flex-1 overflow-y-auto py-4"
>
<div
v-if="showStarter"
class="mx-auto flex h-full max-w-xl flex-col justify-center px-2 py-6"
>
<div class="text-base font-medium">智能助手可以帮你看经营数据</div>
<p class="text-muted-foreground mt-2 text-sm leading-6">
直接问销售额、单量、净利、费比。说「近7天」「昨天」「本周」「本月」即可,不必自己换算日期。查某家店或某个商品时,带上店铺名或商品名。
</p>
<ul
class="text-muted-foreground mt-3 list-disc space-y-1 pl-5 text-sm leading-6"
>
<li>工作台概览:销售额、单量、净利、费比</li>
<li>店铺 / 商品:销量、ROI、目标达成</li>
</ul>
<div class="mt-4 flex flex-col gap-2">
<button
v-for="item in starterPrompts"
:key="item"
type="button"
class="hover:border-primary hover:bg-primary/5 rounded-lg border border-border bg-card px-3 py-2 text-left text-sm transition-colors"
@click="applyStarterPrompt(item)"
>
{{ item }}
</button>
</div>
</div>
<ChatItem
v-for="item in bubbleViews"
:key="item.id"
class="agent-td-item"
:role="item.role"
:name="item.name"
:content="item.content"
:text-loading="item.textLoading"
variant="base"
animation="gradient"
/>
<div
v-if="toolStatusActive && toolStatusLabel"
class="text-muted-foreground mx-auto flex max-w-3xl items-center gap-2 px-4 py-1 text-xs"
>
<span
class="border-primary/40 inline-block size-3 animate-spin rounded-full border-2 border-t-transparent"
/>
<span>{{ toolStatusLabel }}…</span>
</div>
</div>
<div class="shrink-0 border-t border-border p-3">
<div
v-if="showMessageLoading"
class="text-muted-foreground absolute inset-0 z-10 flex items-center justify-center bg-background/60 text-sm"
>
加载消息…
</div>
<!-- 空态:问候 + 居中输入 + chips -->
<div
v-if="showStarter"
class="flex min-h-0 flex-1 flex-col items-center justify-center px-4 pb-16 pt-8"
>
<h1 class="mb-10 text-center text-3xl font-semibold tracking-tight">
{{ greeting }}
</h1>
<div class="agent-composer w-full max-w-[720px]">
<CatalogCiteSender
v-model="draft"
v-model:cites="citations"
@@ -620,31 +646,117 @@ void loadEnabledModels().then(() => refreshSessions());
@send="onSenderSend"
@stop="onSenderStop"
/>
<div class="mt-5 flex flex-wrap justify-center gap-2">
<Tooltip
v-for="item in starterPrompts"
:key="item.label"
:title="item.prompt"
>
<button
type="button"
class="hover:bg-muted border-border text-foreground/80 hover:text-foreground inline-flex items-center gap-2 rounded-full border bg-card px-3.5 py-1.5 text-sm transition-colors"
@click="applyStarterPrompt(item.prompt)"
>
<component :is="item.icon" class="text-muted-foreground" />
{{ item.label }}
</button>
</Tooltip>
</div>
</div>
</div>
<!-- 对话态:可滚动消息 + 底部输入 -->
<template v-else>
<div
ref="messagePaneRef"
class="agent-td-chat t-chat t-chat--normal min-h-0 flex-1 overflow-y-auto px-4 pb-4 pt-14"
>
<Empty
v-if="!showMessageLoading && messages.length === 0"
class="mt-24"
description="开始提问吧(需先在「模型管理」配置 API)"
/>
<div class="mx-auto max-w-[760px]">
<ChatItem
v-for="item in bubbleViews"
:key="item.id"
class="agent-td-item"
:role="item.role"
:name="item.name"
:content="item.content"
:text-loading="item.textLoading"
variant="base"
animation="gradient"
/>
<div
v-if="toolStatusActive && toolStatusLabel"
class="text-muted-foreground flex items-center gap-2 px-2 py-1 text-xs"
>
<span
class="border-primary/40 inline-block size-3 animate-spin rounded-full border-2 border-t-transparent"
/>
<span>{{ toolStatusLabel }}…</span>
</div>
</div>
</div>
<div class="agent-composer-dock shrink-0 px-4 pb-5 pt-2">
<div class="agent-composer mx-auto max-w-[720px]">
<CatalogCiteSender
v-model="draft"
v-model:cites="citations"
:loading="sending"
@send="onSenderSend"
@stop="onSenderStop"
/>
</div>
</div>
</template>
</section>
</div>
</Page>
</template>
<style scoped>
.agent-td-chat {
--td-bg-color-container: transparent;
padding-inline: 28px;
.agent-sider {
background: hsl(var(--background-deep, var(--background)));
}
.agent-composer :deep(.t-chat-sender) {
border-radius: 24px;
box-shadow: 0 0 0 1px hsl(var(--border) / 0.8);
}
.agent-composer :deep(.t-chat-sender__textarea__wrapper) {
border-radius: 24px;
padding-block: 10px;
padding-inline: 14px;
}
.agent-model-select :deep(.ant-select-selector) {
border-radius: 999px !important;
}
.agent-td-item {
margin-bottom: 12px;
margin-bottom: 16px;
}
/* 气泡按内容收缩,左右再留一截,避免贴边铺满 */
/* 气泡按内容收缩,避免贴边铺满 */
.agent-td-item :deep(.t-chat__inner) {
box-sizing: border-box;
padding-inline: 4px;
}
.agent-td-item :deep(.t-chat__content) {
width: auto;
max-width: min(720px, calc(100% - 32px));
max-width: min(680px, calc(100% - 24px));
}
.agent-td-item :deep(.t-chat__inner.user) {
justify-content: flex-end;
}
.agent-td-item :deep(.t-chat__text__name) {
opacity: 0.55;
font-size: 12px;
}
</style>