<template>
|
<div>
|
<div class="boxDiv">
|
<el-scrollbar>
|
<div v-if="state.fileSuffix == '.docx'" ref="file" class="files"></div>
|
<div v-if="state.fileSuffix == '.doc'" ref="fileT" class="files"></div>
|
<div v-if="state.fileSuffix == '.pdf'" style="height: 81vh">
|
<iframe
|
:src="`/manage/pdfjs/web/viewer.html?file=${encodeURIComponent(pdfurl)}`"
|
width="100%"
|
height="100%"
|
/>
|
</div>
|
</el-scrollbar>
|
</div>
|
</div>
|
</template>
|
|
<script setup lang="ts">
|
import { ref, nextTick, onMounted, getCurrentInstance, reactive } from 'vue'
|
import * as docx from 'docx-preview'
|
import commonApi from '@/api/commonApi'
|
|
// 获取dome
|
const { proxy } = getCurrentInstance()
|
//
|
const pdfurl = ref('')
|
|
interface BasicInfoType {
|
id?: string // 客户id
|
parentId: string
|
}
|
|
interface State {
|
visible: boolean // 控制弹窗显示
|
title?: string // 弹窗标题
|
fileId?: string | number //文件id
|
fileSuffix: string
|
}
|
|
const state = reactive<any>({
|
visible: false,
|
title: '文件预览',
|
fileId: '',
|
fileType: '',
|
file: '',
|
})
|
|
// 控制弹窗的显示
|
const visibleShow = (data?: any) => {
|
state.fileId = data?.fileId ?? ''
|
state.fileSuffix = data?.fileSuffix.toLowerCase() ?? ''
|
if (state.fileSuffix == '.docx') {
|
previewDocxFun()
|
} else if (state.fileSuffix == '.doc') {
|
previewDocFun()
|
} else if (state.fileSuffix == '.pdf') {
|
previewPdfFun()
|
}
|
}
|
|
// 调用预览接口
|
const previewDocxFun = () => {
|
commonApi
|
.getFileFlowById({ fileId: state.fileId })
|
.then((res) => {
|
if (res) {
|
docx.renderAsync(res.data, proxy.$refs.file)
|
} else {
|
// ElMessage.warning(res.msg)
|
}
|
})
|
.finally(() => {})
|
}
|
|
const fileT = ref()
|
|
const previewDocFun = () => {
|
commonApi
|
.getFileFlowById({ fileId: state.fileId })
|
.then((res: any) => {
|
if (res) {
|
// let docx = require("docx-preview");
|
docx.renderAsync(res.data, fileT.value)
|
} else {
|
// ElMessage.warning(res.msg)
|
}
|
})
|
.finally(() => {})
|
}
|
|
const previewPdfFun = () => {
|
commonApi
|
.getFileFlowById({ fileId: state.fileId })
|
.then((res) => {
|
if (res) {
|
let blob = new Blob([res.data], { type: 'application/pdf' })
|
const url = URL.createObjectURL(blob)
|
pdfurl.value = url
|
}
|
})
|
.finally(() => {})
|
}
|
|
// 将方法暴露给父组件
|
defineExpose({
|
visibleShow,
|
})
|
|
// 将子组件的事件触发暴露给父组件
|
const emit = defineEmits(['closePreview'])
|
// 关闭弹窗
|
const closeDialog = () => {
|
pdfurl.value = ''
|
if (state.fileType == '.docx') {
|
docx?.renderAsync('', proxy.$refs.file)
|
} else if (state.fileType == '.doc') {
|
} else if (state.fileType == '.pdf') {
|
pdfurl.value = ''
|
}
|
emit('closePreview')
|
}
|
</script>
|
|
<style scoped lang="scss">
|
.boxDiv {
|
// z-index: 999;
|
// position: absolute;
|
// top: 0;
|
// right: 0;
|
// left: 0;
|
// bottom: 0;
|
height: 100%;
|
}
|
.close-icon {
|
position: absolute;
|
right: 0;
|
top: 0;
|
width: 40px;
|
height: 40px;
|
background-color: #666;
|
border-bottom-left-radius: 60px;
|
padding-left: 10px;
|
padding-top: 5px;
|
cursor: pointer;
|
z-index: 99999;
|
}
|
</style>
|