UnisKB/ui/src/views/chat/pc/index.vue

636 lines
18 KiB
Vue
Raw Normal View History

2025-06-03 08:08:49 +00:00
<template>
<div
2025-06-15 06:53:18 +00:00
class="chat-pc"
2025-06-03 08:08:49 +00:00
:class="classObj"
v-loading="loading"
:style="{
2025-06-24 10:15:26 +00:00
'--el-color-primary': applicationDetail?.custom_theme?.theme_color,
'--el-color-primary-light-9': hexToRgba(applicationDetail?.custom_theme?.theme_color, 0.1),
2025-06-03 08:08:49 +00:00
}"
>
2025-06-24 10:15:26 +00:00
<div class="flex h-full w-full">
<div class="chat-pc__left">
<el-menu class="w-full h-full" :default-active="currentChatId" :collapse="isPcCollapse" collapse-transition popper-class="chat-pc-popper">
<div style="padding: 16px 18px 0 18px;">
<div class="flex align-center mb-16">
<div class="flex">
2025-06-13 13:56:01 +00:00
<el-avatar
v-if="isAppIcon(applicationDetail?.icon)"
shape="square"
:size="32"
style="background: none"
>
<img :src="applicationDetail?.icon" alt="" />
</el-avatar>
<LogoIcon v-else height="28px" style="width: 28px; height: 28px; display: block" />
</div>
2025-06-24 10:15:26 +00:00
<h4 v-show="!isPcCollapse">{{ applicationDetail?.name }}</h4>
2025-06-03 08:08:49 +00:00
</div>
2025-06-24 10:15:26 +00:00
<el-button v-show="!isPcCollapse" class="add-button w-full primary" @click="newChat">
2025-06-15 07:21:36 +00:00
<AppIcon iconName="app-create-chat"></AppIcon>
2025-06-13 13:56:01 +00:00
<span class="ml-4">{{ $t('chat.createChat') }}</span>
</el-button>
2025-06-24 10:15:26 +00:00
<p v-show="!isPcCollapse" class="mt-20 mb-8">{{ $t('chat.history') }}</p>
</div>
<div v-show="!isPcCollapse" class="left-height pt-0">
<el-scrollbar>
<div class="p-8 pt-0">
<common-list
:style="{
'--el-color-primary': applicationDetail?.custom_theme?.theme_color,
'--el-color-primary-light-9': hexToRgba(
applicationDetail?.custom_theme?.theme_color,
0.1,
),
}"
:data="chatLogData"
class="mt-8"
v-loading="left_loading"
:defaultActive="currentChatId"
@click="clickListHandle"
@mouseenter="mouseenter"
@mouseleave="mouseId = ''"
>
<template #default="{ row }">
<div class="flex-between">
<span :title="row.abstract">
{{ row.abstract }}
</span>
<div @click.stop v-show="mouseId === row.id && row.id !== 'new'">
<el-dropdown trigger="click" :teleported="false">
<el-icon class="rotate-90 mt-4"><MoreFilled /></el-icon>
<template #dropdown>
<el-dropdown-menu>
<el-dropdown-item @click.stop="editLogTitle(row)">
<el-icon><EditPen /></el-icon>
{{ $t('common.edit') }}
</el-dropdown-item>
<el-dropdown-item @click.stop="deleteLog(row)">
<el-icon><Delete /></el-icon>
{{ $t('common.delete') }}
</el-dropdown-item>
</el-dropdown-menu>
</template>
</el-dropdown>
</div>
2025-06-03 08:08:49 +00:00
</div>
2025-06-24 10:15:26 +00:00
</template>
2025-06-03 08:08:49 +00:00
2025-06-24 10:15:26 +00:00
<template #empty>
<div class="text-center">
<el-text type="info">{{ $t('chat.noHistory') }}</el-text>
</div>
</template>
</common-list>
</div>
<div v-if="chatLogData?.length" class="gradient-divider lighter mt-8">
<span>{{ $t('chat.only20history') }}</span>
</div>
</el-scrollbar>
</div>
<el-menu-item index="1" v-show="isPcCollapse" @click="newChat">
<AppIcon iconName="app-create-chat"></AppIcon>
<template #title>{{ $t('chat.createChat') }}</template>
</el-menu-item>
<el-sub-menu v-show="isPcCollapse" index="2">
<template #title>
<el-icon>
<location />
</el-icon>
</template>
<el-menu-item-group v-loading="left_loading">
<template #title><span>{{ $t('chat.history') }}</span></template>
<el-menu-item v-for="row in chatLogData" :index="row.id" :key="row.id" @click="clickListHandle(row)">
<div class="flex-between w-full lighter">
<span :title="row.abstract">
{{ row.abstract }}
</span>
<div @click.stop class="flex" v-show="mouseId === row.id && row.id !== 'new'">
<el-dropdown trigger="click" :teleported="false">
<el-icon class="rotate-90 mt-4">
<MoreFilled />
</el-icon>
<template #dropdown>
<el-dropdown-menu>
<el-dropdown-item @click.stop="editLogTitle(row)">
<el-icon>
<EditPen />
</el-icon>
{{ $t('common.edit') }}
</el-dropdown-item>
<el-dropdown-item @click.stop="deleteLog(row)">
<el-icon>
<Delete />
</el-icon>
{{ $t('common.delete') }}
</el-dropdown-item>
</el-dropdown-menu>
</template>
</el-dropdown>
2025-06-13 13:56:01 +00:00
</div>
2025-06-24 10:15:26 +00:00
</div>
</el-menu-item>
</el-menu-item-group>
<div v-if="!chatLogData?.length" class="text-center">
<el-text type="info">{{ $t('chat.noHistory') }}</el-text>
2025-06-13 13:56:01 +00:00
</div>
2025-06-24 10:15:26 +00:00
</el-sub-menu>
</el-menu>
<el-button v-if="!common.isMobile()" class="pc-collapse" circle size="small" @click="isPcCollapse = !isPcCollapse">
<el-icon>
<component :is=" isPcCollapse ? 'Fold' : 'Expand'" />
</el-icon>
</el-button>
2025-06-13 13:56:01 +00:00
</div>
<div class="chat-pc__right">
2025-06-15 06:53:18 +00:00
<div class="mb-24 p-16-24 flex-between">
2025-06-13 13:56:01 +00:00
<h4 class="ellipsis-1" style="width: 66%">
{{ currentChatName }}
</h4>
2025-06-03 08:08:49 +00:00
2025-06-13 13:56:01 +00:00
<span class="flex align-center" v-if="currentRecordList.length">
<AppIcon
v-if="paginationConfig.total"
iconName="app-chat-record"
class="info mr-8"
style="font-size: 16px"
></AppIcon>
<span v-if="paginationConfig.total" class="lighter">
{{ paginationConfig.total }} {{ $t('chat.question_count') }}
</span>
<el-dropdown class="ml-8">
2025-06-03 08:08:49 +00:00
<AppIcon
2025-06-13 13:56:01 +00:00
iconName="app-export"
class="cursor"
:title="$t('chat.exportRecords')"
2025-06-03 08:08:49 +00:00
></AppIcon>
2025-06-13 13:56:01 +00:00
<template #dropdown>
<el-dropdown-menu>
<el-dropdown-item @click="exportMarkdown"
>{{ $t('common.export') }} Markdown</el-dropdown-item
>
<el-dropdown-item @click="exportHTML"
>{{ $t('common.export') }} HTML</el-dropdown-item
>
</el-dropdown-menu>
</template>
</el-dropdown>
</span>
</div>
<div class="right-height chat-width">
<AiChat
ref="AiChatRef"
v-model:applicationDetails="applicationDetail"
:available="applicationAvailable"
type="ai-chat"
:appId="applicationDetail?.id"
:record="currentRecordList"
:chatId="currentChatId"
@refresh="refresh"
@scroll="handleScroll"
>
</AiChat>
2025-06-03 08:08:49 +00:00
</div>
</div>
</div>
2025-06-13 13:56:01 +00:00
<div class="collapse">
<el-button @click="isCollapse = !isCollapse">
<el-icon> <component :is="isCollapse ? 'Fold' : 'Expand'" /></el-icon>
</el-button>
</div>
2025-06-03 08:08:49 +00:00
<EditTitleDialog ref="EditTitleDialogRef" @refresh="refreshFieldTitle" />
</div>
</template>
<script setup lang="ts">
2025-06-24 10:15:26 +00:00
import { ref, onMounted, nextTick, computed, watch } from 'vue'
2025-06-03 08:08:49 +00:00
import { marked } from 'marked'
import { saveAs } from 'file-saver'
2025-06-23 13:28:10 +00:00
import chatAPI from '@/api/chat/chat'
2025-06-03 08:08:49 +00:00
import { isAppIcon } from '@/utils/common'
import useStore from '@/stores'
import useResize from '@/layout/hooks/useResize'
import { hexToRgba } from '@/utils/theme'
import EditTitleDialog from './EditTitleDialog.vue'
import { t } from '@/locales'
useResize()
2025-06-11 11:15:17 +00:00
const { user, chatLog, common } = useStore()
2025-06-03 08:08:49 +00:00
const EditTitleDialogRef = ref()
const isCollapse = ref(false)
2025-06-24 10:15:26 +00:00
const isPcCollapse = ref(false)
watch(()=> common.device, () => {
if(common.isMobile()) {
isPcCollapse.value = false
}
})
2025-06-03 08:08:49 +00:00
const customStyle = computed(() => {
return {
background: applicationDetail.value?.custom_theme?.theme_color,
2025-06-13 13:56:01 +00:00
color: applicationDetail.value?.custom_theme?.header_font_color,
2025-06-03 08:08:49 +00:00
}
})
const classObj = computed(() => {
return {
mobile: common.isMobile(),
hideLeft: !isCollapse.value,
2025-06-13 13:56:01 +00:00
openLeft: isCollapse.value,
2025-06-03 08:08:49 +00:00
}
})
const newObj = {
id: 'new',
2025-06-13 13:56:01 +00:00
abstract: t('chat.createChat'),
2025-06-03 08:08:49 +00:00
}
const props = defineProps<{
application_profile: any
applicationAvailable: boolean
}>()
const AiChatRef = ref()
const loading = ref(false)
const left_loading = ref(false)
const applicationDetail = computed({
get: () => {
return props.application_profile
},
2025-06-13 13:56:01 +00:00
set: (v) => {},
2025-06-03 08:08:49 +00:00
})
const chatLogData = ref<any[]>([])
const paginationConfig = ref({
current_page: 1,
page_size: 20,
2025-06-13 13:56:01 +00:00
total: 0,
2025-06-03 08:08:49 +00:00
})
const currentRecordList = ref<any>([])
const currentChatId = ref('new') // 当前历史记录Id 默认为'new'
const currentChatName = ref(t('chat.createChat'))
const mouseId = ref('')
function mouseenter(row: any) {
mouseId.value = row.id
}
function editLogTitle(row: any) {
EditTitleDialogRef.value.open(row, applicationDetail.value.id)
}
function refreshFieldTitle(chatId: string, abstract: string) {
const find = chatLogData.value.find((item: any) => item.id == chatId)
if (find) {
find.abstract = abstract
}
}
function deleteLog(row: any) {
2025-06-11 11:15:17 +00:00
chatLog.asyncDelChatClientLog(applicationDetail.value.id, row.id, left_loading).then(() => {
2025-06-03 08:08:49 +00:00
if (currentChatId.value === row.id) {
currentChatId.value = 'new'
currentChatName.value = t('chat.createChat')
paginationConfig.value.current_page = 1
paginationConfig.value.total = 0
currentRecordList.value = []
}
getChatLog(applicationDetail.value.id)
})
}
function handleScroll(event: any) {
if (
currentChatId.value !== 'new' &&
event.scrollTop === 0 &&
paginationConfig.value.total > currentRecordList.value.length
) {
const history_height = event.dialogScrollbar.offsetHeight
paginationConfig.value.current_page += 1
getChatRecord().then(() => {
event.scrollDiv.setScrollTop(event.dialogScrollbar.offsetHeight - history_height)
})
}
}
function newChat() {
if (!chatLogData.value.some((v) => v.id === 'new')) {
paginationConfig.value.current_page = 1
paginationConfig.value.total = 0
currentRecordList.value = []
chatLogData.value.unshift(newObj)
} else {
paginationConfig.value.current_page = 1
paginationConfig.value.total = 0
currentRecordList.value = []
}
currentChatId.value = 'new'
currentChatName.value = t('chat.createChat')
if (common.isMobile()) {
isCollapse.value = false
}
}
function getChatLog(id: string, refresh?: boolean) {
const page = {
current_page: 1,
2025-06-13 13:56:01 +00:00
page_size: 20,
2025-06-03 08:08:49 +00:00
}
2025-06-23 13:28:10 +00:00
chatAPI.pageChat(page.current_page, page.page_size, left_loading).then((res: any) => {
2025-06-03 08:08:49 +00:00
chatLogData.value = res.data.records
if (refresh) {
currentChatName.value = chatLogData.value?.[0]?.abstract
} else {
paginationConfig.value.current_page = 1
paginationConfig.value.total = 0
currentRecordList.value = []
currentChatId.value = chatLogData.value?.[0]?.id || 'new'
currentChatName.value = chatLogData.value?.[0]?.abstract || t('chat.createChat')
if (currentChatId.value !== 'new') {
getChatRecord()
}
}
})
}
function getChatRecord() {
2025-06-24 09:44:59 +00:00
return chatAPI
.pageChatRecord(
2025-06-03 08:08:49 +00:00
currentChatId.value,
2025-06-24 09:44:59 +00:00
paginationConfig.value.current_page,
paginationConfig.value.page_size,
2025-06-03 08:08:49 +00:00
loading,
)
.then((res: any) => {
paginationConfig.value.total = res.data.total
const list = res.data.records
list.map((v: any) => {
v['write_ed'] = true
v['record_id'] = v.id
})
currentRecordList.value = [...list, ...currentRecordList.value].sort((a, b) =>
2025-06-13 13:56:01 +00:00
a.create_time.localeCompare(b.create_time),
2025-06-03 08:08:49 +00:00
)
if (paginationConfig.value.current_page === 1) {
nextTick(() => {
// 将滚动条滚动到最下面
AiChatRef.value.setScrollBottom()
})
}
})
}
const clickListHandle = (item: any) => {
if (item.id !== currentChatId.value) {
paginationConfig.value.current_page = 1
paginationConfig.value.total = 0
currentRecordList.value = []
currentChatId.value = item.id
currentChatName.value = item.abstract
if (currentChatId.value !== 'new') {
getChatRecord()
// 切换对话后,取消暂停的浏览器播放
if (window.speechSynthesis.paused && window.speechSynthesis.speaking) {
window.speechSynthesis.resume()
nextTick(() => {
window.speechSynthesis.cancel()
})
}
}
}
if (common.isMobile()) {
isCollapse.value = false
}
}
function refresh(id: string) {
getChatLog(applicationDetail.value.id, true)
currentChatId.value = id
}
async function exportMarkdown(): Promise<void> {
const suggestedName: string = `${currentChatId.value}.md`
const markdownContent: string = currentRecordList.value
.map((record: any) => `# ${record.problem_text}\n\n${record.answer_text}\n\n`)
.join('\n')
const blob: Blob = new Blob([markdownContent], { type: 'text/markdown;charset=utf-8' })
saveAs(blob, suggestedName)
}
async function exportHTML(): Promise<void> {
const suggestedName: string = `${currentChatId.value}.html`
const markdownContent: string = currentRecordList.value
.map((record: any) => `# ${record.problem_text}\n\n${record.answer_text}\n\n`)
.join('\n')
const htmlContent: any = marked(markdownContent)
const blob: Blob = new Blob([htmlContent], { type: 'text/html;charset=utf-8' })
saveAs(blob, suggestedName)
}
/**
*初始化历史对话记录
*/
const init = () => {
2025-06-21 03:22:40 +00:00
// if (
// (applicationDetail.value.show_history || !user.isEnterprise()) &&
// props.applicationAvailable
// ) {
// getChatLog(applicationDetail.value.id)
// }
2025-06-24 10:15:26 +00:00
getChatLog(applicationDetail.value?.id)
2025-06-03 08:08:49 +00:00
}
onMounted(() => {
init()
})
</script>
<style lang="scss">
.chat-pc {
2025-06-24 10:15:26 +00:00
height: 100%;
2025-06-03 08:08:49 +00:00
overflow: hidden;
2025-06-15 06:53:18 +00:00
background: #eef1f4;
2025-06-03 08:08:49 +00:00
&__header {
background: var(--app-header-bg-color);
position: fixed;
width: 100%;
left: 0;
top: 0;
z-index: 100;
height: var(--app-header-height);
line-height: var(--app-header-height);
box-sizing: border-box;
border-bottom: 1px solid var(--el-border-color);
}
&__left {
2025-06-24 10:15:26 +00:00
position: relative;
.el-menu {
background:
linear-gradient(187.61deg, rgba(235, 241, 255, 0.5) 39.6%, rgba(231, 249, 255, 0.5) 94.3%),
#eef1f4;
&:not(.el-menu--collapse) {
width: 280px;
}
2025-06-15 06:53:18 +00:00
2025-06-24 10:15:26 +00:00
.el-menu-item:hover {
background: transparent;
}
&.el-menu--collapse {
.el-menu-item,.el-menu-tooltip__trigger,.el-sub-menu__title {
padding: 0;
}
.el-menu-item .el-menu-tooltip__trigger,.el-sub-menu__title {
position: static;
width: 40px;
height: 40px;
border-radius: 6px;
align-items: center;
justify-content: center;
margin: 0 auto;
}
.el-menu-item:hover .el-menu-tooltip__trigger,.el-sub-menu__title:hover {
background-color: #1F23291A;
}
}
}
2025-06-03 08:08:49 +00:00
.add-button {
border: 1px solid var(--el-color-primary);
}
.left-height {
2025-06-15 07:21:36 +00:00
height: calc(100vh - 140px);
2025-06-03 08:08:49 +00:00
}
2025-06-24 10:15:26 +00:00
.pc-collapse {
position: absolute;
top: 20px;
right: -12px;
box-shadow: 0px 5px 10px 0px #1F23291A;
}
2025-06-03 08:08:49 +00:00
}
&__right {
2025-06-24 10:15:26 +00:00
flex: 1;
2025-06-03 08:08:49 +00:00
overflow: hidden;
position: relative;
box-sizing: border-box;
.right-height {
2025-06-15 07:21:36 +00:00
height: calc(100vh - 85px);
2025-06-03 08:08:49 +00:00
}
}
.gradient-divider {
position: relative;
text-align: center;
color: var(--el-color-info);
::before {
content: '';
width: 17%;
height: 1px;
background: linear-gradient(90deg, rgba(222, 224, 227, 0) 0%, #dee0e3 100%);
position: absolute;
left: 16px;
top: 50%;
}
::after {
content: '';
width: 17%;
height: 1px;
background: linear-gradient(90deg, #dee0e3 0%, rgba(222, 224, 227, 0) 100%);
position: absolute;
right: 16px;
top: 50%;
}
}
.collapse {
display: none;
}
}
2025-06-24 10:15:26 +00:00
.chat-pc-popper {
.el-menu-item-group__title {
padding-bottom: 16px;
font-weight: 500;
color: var(--app-text-color-secondary);
}
.el-menu-item {
border-radius: 6px;
height: 40px;
margin: 0 8px;
padding-left: 8px;
padding-right: 8px;
&:hover {
background-color: #1F23291A;
}
&.is-active {
background-color: #3370FF1A;
}
}
}
2025-06-03 08:08:49 +00:00
// 适配移动端
.mobile {
.chat-pc {
&__right {
width: 100%;
}
&__left {
display: none;
width: 0;
}
}
.collapse {
display: block;
position: fixed;
bottom: 90px;
z-index: 99;
}
&.openLeft {
.chat-pc {
&__left {
display: block;
position: fixed;
width: 100%;
z-index: 99;
2025-06-15 07:21:36 +00:00
height: calc(100vh);
2025-06-24 10:15:26 +00:00
.el-menu {
width: 100%;
}
2025-06-03 08:08:49 +00:00
}
}
.collapse {
display: block;
position: absolute;
bottom: 90px;
right: 0;
z-index: 99;
}
}
}
.chat-width {
max-width: 80%;
margin: 0 auto;
}
@media only screen and (max-width: 1000px) {
.chat-width {
max-width: 100% !important;
margin: 0 auto;
}
}
</style>