UnisKB/ui/src/views/log/component/ChatRecordDrawer.vue

167 lines
3.6 KiB
Vue
Raw Normal View History

2023-12-07 07:22:07 +00:00
<template>
2024-04-15 10:46:03 +00:00
<el-drawer v-model="visible" size="60%" @close="closeHandle" class="chat-record-drawer">
2023-12-07 07:22:07 +00:00
<template #header>
<h4 class="single-line">{{ currentAbstract }}</h4>
2023-12-07 07:22:07 +00:00
</template>
2023-12-08 04:42:40 +00:00
<div
v-loading="paginationConfig.current_page === 1 && loading"
class="h-full"
style="padding: 24px 0"
>
<AiChat
ref="AiChatRef"
:application-details="application"
type="log"
:record="recordList"
@scroll="handleScroll"
2023-12-11 02:35:26 +00:00
>
</AiChat>
2023-12-07 11:28:38 +00:00
</div>
2023-12-07 07:22:07 +00:00
<template #footer>
<div>
2025-01-15 11:10:29 +00:00
<el-button @click="pre" :disabled="pre_disable || loading">{{
$t('views.log.buttons.prev')
}}</el-button>
<el-button @click="next" :disabled="next_disable || loading">{{
$t('views.log.buttons.next')
}}</el-button>
2023-12-07 07:22:07 +00:00
</div>
</template>
</el-drawer>
</template>
<script setup lang="ts">
import { ref, reactive, watch, nextTick } from 'vue'
2023-12-07 07:22:07 +00:00
import { useRoute } from 'vue-router'
import { type ApplicationFormType, type chatType } from '@/api/type/application'
import useStore from '@/stores'
const AiChatRef = ref()
const { log } = useStore()
2023-12-08 10:10:47 +00:00
const props = withDefaults(
defineProps<{
/**
* 应用信息
*/
application?: ApplicationFormType
/**
* 对话 记录id
*/
2024-05-20 09:50:14 +00:00
chatId: string
2024-01-22 02:43:51 +00:00
currentAbstract: string
2023-12-08 10:10:47 +00:00
/**
* 下一条
*/
next: () => void
/**
* 上一条
*/
pre: () => void
2023-12-07 11:28:38 +00:00
2023-12-08 10:10:47 +00:00
pre_disable: boolean
next_disable: boolean
}>(),
{}
)
2023-12-07 11:28:38 +00:00
2024-05-20 09:50:14 +00:00
const emit = defineEmits(['update:chatId', 'update:currentAbstract', 'refresh'])
2023-12-08 04:42:40 +00:00
2023-12-07 07:22:07 +00:00
const route = useRoute()
const {
params: { id }
} = route
const loading = ref(false)
const visible = ref(false)
2023-12-07 11:28:38 +00:00
const recordList = ref<chatType[]>([])
2023-12-07 07:22:07 +00:00
const paginationConfig = reactive({
current_page: 1,
2023-12-15 03:04:10 +00:00
page_size: 20,
2023-12-07 07:22:07 +00:00
total: 0
})
2024-04-15 10:46:03 +00:00
function closeHandle() {
2023-12-07 11:28:38 +00:00
recordList.value = []
paginationConfig.total = 0
paginationConfig.current_page = 1
}
function getChatRecord() {
return log
.asyncChatRecordLog(id as string, props.chatId, paginationConfig, loading)
.then((res: any) => {
2023-12-07 11:28:38 +00:00
paginationConfig.total = res.data.total
const list = res.data.records
recordList.value = [...list, ...recordList.value].sort((a, b) =>
a.create_time.localeCompare(b.create_time)
)
if (paginationConfig.current_page === 1) {
nextTick(() => {
// 将滚动条滚动到最下面
AiChatRef.value.setScrollBottom()
})
}
2023-12-07 11:28:38 +00:00
})
2023-12-07 07:22:07 +00:00
}
2023-12-08 10:10:47 +00:00
watch(
2024-05-20 09:50:14 +00:00
() => props.chatId,
2023-12-08 10:10:47 +00:00
() => {
recordList.value = []
paginationConfig.total = 0
paginationConfig.current_page = 1
if (props.chatId) {
getChatRecord()
}
2023-12-08 10:10:47 +00:00
}
)
2023-12-08 04:42:40 +00:00
2023-12-13 07:13:01 +00:00
watch(visible, (bool) => {
if (!bool) {
2024-05-20 09:50:14 +00:00
emit('update:chatId', '')
2024-01-22 02:43:51 +00:00
emit('update:currentAbstract', '')
2024-01-25 07:08:25 +00:00
emit('refresh')
2023-12-13 07:13:01 +00:00
}
})
function handleScroll(event: any) {
if (
props.chatId !== 'new' &&
event.scrollTop === 0 &&
paginationConfig.total > recordList.value.length
) {
const history_height = event.dialogScrollbar.offsetHeight
paginationConfig.current_page += 1
getChatRecord().then(() => {
event.scrollDiv.setScrollTop(event.dialogScrollbar.offsetHeight - history_height)
})
}
}
2023-12-08 10:10:47 +00:00
const open = () => {
2023-12-07 07:22:07 +00:00
visible.value = true
}
2023-12-08 04:42:40 +00:00
2023-12-07 07:22:07 +00:00
defineExpose({
open
})
</script>
<style lang="scss">
.single-line {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
2023-12-07 07:22:07 +00:00
.chat-record-drawer {
.el-drawer__body {
background: var(--app-layout-bg-color);
2023-12-08 04:42:40 +00:00
padding: 0;
2023-12-07 07:22:07 +00:00
}
2023-12-29 07:13:30 +00:00
:deep(.el-divider__text) {
2023-12-11 02:35:26 +00:00
background: var(--app-layout-bg-color);
}
2023-12-07 07:22:07 +00:00
}
</style>