UnisKB/ui/src/views/document/index.vue

400 lines
12 KiB
Vue
Raw Normal View History

2023-11-07 11:04:59 +00:00
<template>
<LayoutContainer header="文档">
<div class="main-calc-height">
2023-11-10 03:36:04 +00:00
<div class="p-24">
2023-11-07 11:04:59 +00:00
<div class="flex-between">
2024-01-10 09:44:05 +00:00
<div>
<el-button
v-if="datasetDetail.type === '0'"
type="primary"
@click="router.push({ path: '/dataset/upload', query: { id: id } })"
>上传文档</el-button
>
2024-01-19 08:03:17 +00:00
<el-button v-if="datasetDetail.type === '1'" type="primary" @click="importDoc"
>导入文档</el-button
>
2024-01-25 07:08:25 +00:00
<el-button @click="syncDataset" v-if="datasetDetail.type === '1'"></el-button>
2024-01-19 08:03:17 +00:00
<el-button
@click="syncMulDocument"
:disabled="multipleSelection.length === 0"
v-if="datasetDetail.type === '1'"
2024-01-25 07:08:25 +00:00
>同步文档</el-button
2024-01-19 08:03:17 +00:00
>
<el-button @click="deleteMulDocument" :disabled="multipleSelection.length === 0"
2024-01-19 06:59:51 +00:00
>批量删除</el-button
>
2024-01-10 09:44:05 +00:00
</div>
2023-11-07 11:04:59 +00:00
<el-input
v-model="filterText"
placeholder="按 文档名称 搜索"
prefix-icon="Search"
class="w-240"
2023-11-10 09:18:00 +00:00
@change="getList"
2023-11-07 11:04:59 +00:00
/>
</div>
2023-11-09 10:52:06 +00:00
<app-table
2024-01-11 06:40:00 +00:00
ref="multipleTableRef"
2023-11-09 10:52:06 +00:00
class="mt-16"
2023-12-06 09:30:46 +00:00
:data="documentData"
2023-11-09 10:52:06 +00:00
:pagination-config="paginationConfig"
2024-01-10 09:44:05 +00:00
:quick-create="datasetDetail.type === '0'"
2023-11-09 10:52:06 +00:00
@sizeChange="handleSizeChange"
2023-12-06 09:30:46 +00:00
@changePage="getList"
2023-11-09 10:52:06 +00:00
@cell-mouse-enter="cellMouseEnter"
@cell-mouse-leave="cellMouseLeave"
2023-11-10 09:18:00 +00:00
@creatQuick="creatQuickHandle"
2023-11-10 11:05:52 +00:00
@row-click="rowClickHandle"
2024-01-11 06:40:00 +00:00
@selection-change="handleSelectionChange"
2023-11-10 03:36:04 +00:00
v-loading="loading"
2024-03-22 05:48:08 +00:00
:row-key="(row: any) => row.id"
2023-11-09 10:52:06 +00:00
>
2024-03-22 05:48:08 +00:00
<el-table-column type="selection" width="55" :reserve-selection="true" />
2023-11-09 10:52:06 +00:00
<el-table-column prop="name" label="文件名称" min-width="280">
<template #default="{ row }">
<ReadWrite
@change="editName"
:data="row.name"
:showEditIcon="row.id === currentMouseId"
/>
</template>
</el-table-column>
2023-11-07 11:04:59 +00:00
<el-table-column prop="char_length" label="字符数" align="right">
<template #default="{ row }">
2023-12-05 11:21:13 +00:00
{{ numberFormat(row.char_length) }}
2023-11-07 11:04:59 +00:00
</template>
</el-table-column>
<el-table-column prop="paragraph_count" label="分段" align="right" />
2023-11-09 10:52:06 +00:00
<el-table-column prop="status" label="文件状态" min-width="90">
<template #default="{ row }">
<el-text v-if="row.status === '1'">
<el-icon class="success"><SuccessFilled /></el-icon>
</el-text>
<el-text v-else-if="row.status === '2'">
<el-icon class="danger"><CircleCloseFilled /></el-icon>
</el-text>
<el-text v-else-if="row.status === '0'">
<el-icon class="is-loading primary"><Loading /></el-icon>
</el-text>
</template>
2023-11-07 11:04:59 +00:00
</el-table-column>
2023-12-08 08:55:29 +00:00
<el-table-column label="启用状态">
2023-11-07 11:04:59 +00:00
<template #default="{ row }">
2023-11-10 11:05:52 +00:00
<div @click.stop>
2023-12-05 11:21:13 +00:00
<el-switch
size="small"
v-model="row.is_active"
@change="changeState($event, row)"
/>
2023-11-10 11:05:52 +00:00
</div>
2023-11-07 11:04:59 +00:00
</template>
</el-table-column>
<el-table-column prop="create_time" label="创建时间" width="170">
<template #default="{ row }">
{{ datetimeFormat(row.create_time) }}
</template>
</el-table-column>
<el-table-column prop="update_time" label="更新时间" width="170">
<template #default="{ row }">
{{ datetimeFormat(row.update_time) }}
</template>
</el-table-column>
2024-03-04 06:16:25 +00:00
<el-table-column label="操作" align="left">
2023-11-07 11:04:59 +00:00
<template #default="{ row }">
2024-01-10 09:44:05 +00:00
<div v-if="datasetDetail.type === '0'">
<span v-if="row.status === '2'" class="mr-4">
<el-tooltip effect="dark" content="重试" placement="top">
<el-button type="primary" text @click.stop="refreshDocument(row)">
<el-icon><RefreshRight /></el-icon>
</el-button>
</el-tooltip>
</span>
<span>
<el-tooltip effect="dark" content="删除" placement="top">
<el-button type="primary" text @click.stop="deleteDocument(row)">
<el-icon><Delete /></el-icon>
</el-button>
</el-tooltip>
</span>
</div>
<div v-if="datasetDetail.type === '1'">
<el-tooltip
effect="dark"
content="同步"
placement="top"
v-if="datasetDetail.type === '1'"
>
2023-12-14 02:47:51 +00:00
<el-button type="primary" text @click.stop="refreshDocument(row)">
2024-01-10 09:44:05 +00:00
<el-icon><Refresh /></el-icon>
2023-11-07 11:04:59 +00:00
</el-button>
</el-tooltip>
2024-01-10 09:44:05 +00:00
<span @click.stop>
<el-dropdown trigger="click">
2024-01-19 06:59:51 +00:00
<el-button text>
2024-01-10 09:44:05 +00:00
<el-icon><MoreFilled /></el-icon>
2024-01-19 06:59:51 +00:00
</el-button>
2024-01-10 09:44:05 +00:00
<template #dropdown>
<el-dropdown-menu>
2024-01-19 08:03:17 +00:00
<el-dropdown-item icon="Setting" @click="settingDoc(row)"
>设置</el-dropdown-item
>
2024-01-10 09:44:05 +00:00
<el-dropdown-item icon="Delete" @click.stop="deleteDocument(row)"
>删除</el-dropdown-item
>
</el-dropdown-menu>
</template>
</el-dropdown>
</span>
</div>
2023-11-07 11:04:59 +00:00
</template>
</el-table-column>
2023-11-09 10:52:06 +00:00
</app-table>
2023-11-07 11:04:59 +00:00
</div>
2024-01-19 08:03:17 +00:00
<ImportDocumentDialog ref="ImportDocumentDialogRef" :title="title" @refresh="refresh" />
2024-01-25 07:08:25 +00:00
<SyncWebDialog ref="SyncWebDialogRef" @refresh="refresh" />
2023-11-07 11:04:59 +00:00
</div>
</LayoutContainer>
</template>
<script setup lang="ts">
2023-12-20 02:57:43 +00:00
import { ref, onMounted, reactive, onBeforeUnmount } from 'vue'
2023-11-07 11:04:59 +00:00
import { useRouter, useRoute } from 'vue-router'
2024-01-11 06:40:00 +00:00
import { ElTable } from 'element-plus'
2023-11-22 10:37:08 +00:00
import documentApi from '@/api/document'
2024-01-19 08:03:17 +00:00
import ImportDocumentDialog from './component/ImportDocumentDialog.vue'
2024-01-25 07:08:25 +00:00
import SyncWebDialog from '@/views/dataset/component/SyncWebDialog.vue'
2023-12-05 11:21:13 +00:00
import { numberFormat } from '@/utils/utils'
2023-11-07 11:04:59 +00:00
import { datetimeFormat } from '@/utils/time'
2024-03-01 03:06:09 +00:00
import { MsgSuccess, MsgConfirm, MsgError } from '@/utils/message'
2024-01-10 09:44:05 +00:00
import useStore from '@/stores'
2023-11-07 11:04:59 +00:00
const router = useRouter()
const route = useRoute()
2023-11-10 09:18:00 +00:00
const {
params: { id } // id为datasetID
2023-11-10 09:18:00 +00:00
} = route as any
2023-11-09 10:52:06 +00:00
const { dataset, document } = useStore()
2024-01-25 07:08:25 +00:00
const SyncWebDialogRef = ref()
2023-11-07 11:04:59 +00:00
const loading = ref(false)
2023-12-20 02:57:43 +00:00
let interval: any
2023-11-07 11:04:59 +00:00
const filterText = ref('')
const documentData = ref<any[]>([])
2023-11-09 10:52:06 +00:00
const currentMouseId = ref(null)
2024-01-10 09:44:05 +00:00
const datasetDetail = ref<any>({})
2023-11-09 10:52:06 +00:00
const paginationConfig = reactive({
2023-12-06 09:30:46 +00:00
current_page: 1,
page_size: 10,
2023-11-09 10:52:06 +00:00
total: 0
})
2024-01-19 08:03:17 +00:00
const ImportDocumentDialogRef = ref()
2024-01-11 06:40:00 +00:00
const multipleTableRef = ref<InstanceType<typeof ElTable>>()
const multipleSelection = ref<any[]>([])
2024-01-19 08:03:17 +00:00
const title = ref('')
2024-01-25 07:08:25 +00:00
function syncDataset() {
SyncWebDialogRef.value.open(id)
}
2024-01-19 08:03:17 +00:00
function importDoc() {
title.value = '导入文档'
ImportDocumentDialogRef.value.open()
}
function settingDoc(row: any) {
title.value = '设置'
ImportDocumentDialogRef.value.open(row)
}
2024-01-11 06:40:00 +00:00
const handleSelectionChange = (val: any[]) => {
multipleSelection.value = val
}
2023-12-20 02:57:43 +00:00
/**
* 初始化轮询
*/
const initInterval = () => {
interval = setInterval(() => {
2024-01-24 02:38:48 +00:00
// if (
// documentData.value.length === 0 ||
// documentData.value.some((item) => item.status === '0' || item.status === '2')
// ) {
// getList(true)
// }
getList(true)
2023-12-20 02:57:43 +00:00
}, 6000)
}
/**
* 关闭轮询
*/
const closeInterval = () => {
if (interval) {
2023-12-14 02:47:51 +00:00
clearInterval(interval)
}
2023-12-20 02:57:43 +00:00
}
2023-12-14 02:47:51 +00:00
function refreshDocument(row: any) {
2024-01-19 08:03:17 +00:00
if (row.type === '1') {
MsgConfirm(`确认同步文档?`, `同步将删除已有数据重新获取新数据,请谨慎操作。`, {
confirmButtonText: '同步',
confirmButtonClass: 'danger'
})
.then(() => {
documentApi.putDocumentRefresh(row.dataset_id, row.id).then((res) => {
getList()
})
})
.catch(() => {})
} else {
documentApi.putDocumentRefresh(row.dataset_id, row.id).then((res) => {
getList()
})
}
2023-12-14 02:47:51 +00:00
}
2023-11-10 11:05:52 +00:00
function rowClickHandle(row: any) {
2023-11-24 11:02:52 +00:00
router.push({ path: `/dataset/${id}/${row.id}` })
2023-11-10 11:05:52 +00:00
}
2023-11-22 03:05:06 +00:00
/*
快速创建空白文档
*/
2023-11-10 09:18:00 +00:00
function creatQuickHandle(val: string) {
loading.value = true
2023-12-18 09:58:56 +00:00
const obj = [{ name: val }]
document
.asyncPostDocument(id, obj)
.then(() => {
2023-11-10 09:18:00 +00:00
getList()
MsgSuccess('创建成功')
})
.catch(() => {
loading.value = false
})
}
2024-01-19 08:03:17 +00:00
function syncMulDocument() {
const arr: string[] = []
multipleSelection.value.map((v) => {
if (v) {
arr.push(v.id)
}
})
documentApi.delMulSyncDocument(id, arr, loading).then(() => {
2024-01-25 07:08:25 +00:00
MsgSuccess('同步文档成功')
2024-01-19 08:03:17 +00:00
getList()
})
}
2024-01-19 06:59:51 +00:00
function deleteMulDocument() {
const arr: string[] = []
multipleSelection.value.map((v) => {
if (v) {
arr.push(v.id)
}
})
documentApi.delMulDocument(id, arr, loading).then(() => {
2024-01-19 08:03:17 +00:00
MsgSuccess('批量删除成功')
2024-01-19 06:59:51 +00:00
getList()
})
}
2023-11-10 03:36:04 +00:00
function deleteDocument(row: any) {
MsgConfirm(
`是否删除文档:${row.name} ?`,
`此文档下的 ${row.paragraph_count} 个分段都会被删除,请谨慎操作。`,
{
confirmButtonText: '删除',
confirmButtonClass: 'danger'
}
)
.then(() => {
2024-01-19 06:59:51 +00:00
documentApi.delDocument(id, row.id, loading).then(() => {
MsgSuccess('删除成功')
getList()
})
2023-11-10 03:36:04 +00:00
})
.catch(() => {})
}
2023-11-22 03:05:06 +00:00
/*
更新名称或状态
*/
2023-12-05 11:21:13 +00:00
function updateData(documentId: string, data: any, msg: string) {
2024-01-19 09:51:27 +00:00
documentApi.putDocument(id, documentId, data, loading).then((res) => {
const index = documentData.value.findIndex((v) => v.id === documentId)
documentData.value.splice(index, 1, res.data)
MsgSuccess(msg)
})
2023-11-10 03:36:04 +00:00
}
function changeState(bool: Boolean, row: any) {
const obj = {
is_active: bool
}
2023-12-05 11:21:13 +00:00
const str = bool ? '启用成功' : '禁用成功'
currentMouseId.value && updateData(row.id, obj, str)
2023-11-10 03:36:04 +00:00
}
function editName(val: string) {
2024-03-01 03:06:09 +00:00
if (val) {
const obj = {
name: val
}
currentMouseId.value && updateData(currentMouseId.value, obj, '修改成功')
} else {
MsgError('文件名称不能为空!')
2023-11-10 03:36:04 +00:00
}
}
2023-11-09 10:52:06 +00:00
function cellMouseEnter(row: any) {
currentMouseId.value = row.id
}
function cellMouseLeave() {
currentMouseId.value = null
}
2023-12-05 06:27:50 +00:00
function handleSizeChange() {
2023-12-06 09:30:46 +00:00
paginationConfig.current_page = 1
getList()
2023-11-09 10:52:06 +00:00
}
2023-11-07 11:04:59 +00:00
2023-12-14 02:47:51 +00:00
function getList(bool?: boolean) {
2023-11-22 10:37:08 +00:00
documentApi
2023-12-06 09:30:46 +00:00
.getDocument(
id as string,
paginationConfig,
filterText.value && { name: filterText.value },
2023-12-14 02:47:51 +00:00
bool ? undefined : loading
2023-12-06 09:30:46 +00:00
)
2023-11-07 11:04:59 +00:00
.then((res) => {
2023-12-06 09:30:46 +00:00
documentData.value = res.data.records
paginationConfig.total = res.data.total
2023-11-07 11:04:59 +00:00
})
}
2024-01-10 09:44:05 +00:00
function getDetail() {
dataset.asyncGetDatesetDetail(id, loading).then((res: any) => {
datasetDetail.value = res.data
})
}
2024-01-19 08:03:17 +00:00
function refresh() {
paginationConfig.current_page = 1
getList()
}
2023-11-07 11:04:59 +00:00
onMounted(() => {
2024-01-10 09:44:05 +00:00
getDetail()
2023-11-07 11:04:59 +00:00
getList()
2023-12-20 02:57:43 +00:00
// 初始化定时任务
initInterval()
})
onBeforeUnmount(() => {
// 清除定时任务
closeInterval()
2023-11-07 11:04:59 +00:00
})
</script>
<style lang="scss" scoped></style>