smart_archives_web/src/views/viewFile/viewFile.vue

129 lines
3.4 KiB
Vue
Raw Normal View History

2025-09-17 17:44:11 +08:00
<template>
2025-09-18 11:16:04 +08:00
<!-- 预览文件 -->
2025-09-17 17:44:11 +08:00
<el-dialog class="l-dialog" :class="lDialog" :title="title" :visible.sync="dialogVisible" :showClose="true"
2025-09-18 11:16:04 +08:00
:closeOnClickModal="false" @close="handleClose" :append-to-body="true">
<div style="text-align:center">
<!-- 图片预览 -->
<template v-if="isImage">
<el-image :src="processedFileUrl" :preview-src-list="previewList" fit="contain" style="max-width:100%;max-height:70vh">
<div slot="error" class="image-slot">
<i class="el-icon-picture-outline"></i>
</div>
</el-image>
</template>
<!-- PDF 预览 -->
<template v-else>
<pdf :src="processedFileUrl" style="width:100%;height:70vh" />
</template>
</div>
2025-09-17 17:44:11 +08:00
</el-dialog>
2025-09-18 11:16:04 +08:00
</template>
2025-09-17 17:44:11 +08:00
<script>
import pdf from 'vue-pdf'
2025-09-18 11:16:04 +08:00
import { getFileAsBase64Api } from '@/api/archivesManagement/fileManager/fileManager'
2025-09-17 17:44:11 +08:00
export default {
2025-09-18 11:16:04 +08:00
name: 'ViewFile',
props: ['width', 'hight', 'dataForm', 'title', 'disabled', 'isAdd', 'rowData', 'projectId'],
components: { pdf },
data() {
return {
2025-09-17 17:44:11 +08:00
dialogVisible: true,
2025-09-18 11:16:04 +08:00
lDialog: this.width > 500 ? "w700" : "w500",
2025-09-17 17:44:11 +08:00
fileUrl: '', // 实际文件地址
fileName: '', // 用于从文件名判断类型
previewList: [] // 预览组(可只放当前图片)
}
},
computed: {
isImage() {
// 允许的图片类型
2025-09-18 11:16:04 +08:00
const exts = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp']
const url = (this.fileUrl || '')
const name = (this.fileName || '').toLowerCase()
return exts.some(ext => url.endsWith(ext) || name.endsWith(ext))
},
isPdf() {
// 判断是否为PDF文件
const exts = ['.pdf']
const url = (this.fileUrl || '')
2025-09-17 17:44:11 +08:00
const name = (this.fileName || '').toLowerCase()
return exts.some(ext => url.endsWith(ext) || name.endsWith(ext))
2025-09-18 11:16:04 +08:00
},
processedFileUrl() {
// 处理文件URL为PDF添加data前缀
if (this.isPdf && this.fileUrl && !this.fileUrl.startsWith('data:')) {
return `data:application/pdf;base64,${this.fileUrl}`
}
return this.fileUrl
2025-09-17 17:44:11 +08:00
}
},
created() {
2025-09-18 11:16:04 +08:00
this.getFileAsBase64();
2025-09-17 17:44:11 +08:00
},
2025-09-18 11:16:04 +08:00
methods: {
handleClose() {
2025-09-17 17:44:11 +08:00
this.dialogVisible = false
this.$emit('closeDialog')
2025-09-18 11:16:04 +08:00
},
/* 获取文件的base64 */
getFileAsBase64() {
getFileAsBase64Api({ id: this.rowData.fileId }).then(res => {
const obj =res.data;
this.fileUrl = obj?.fileBase64 || ''
this.fileName = obj?.fileName || ''
// 预览组(如果你有多张,可放数组;没有就放当前一张)
if (this.isImage && this.fileUrl) {
this.previewList = [this.processedFileUrl]
}
})
2025-09-17 17:44:11 +08:00
}
},
}
</script>
<style lang="scss">
.w700 .el-dialog {
width: 700px;
}
.w500 .el-dialog {
width: 500px;
}
.w500 .el-dialog__header,
.w700 .el-dialog__header {
// background: #eeeeee;
.el-dialog__title {
font-size: 16px;
}
}
.yxq .el-range-separator {
margin-right: 7px !important;
}
.el-date-editor--daterange.el-input__inner {
width: 260px;
}
.form-item {
width: 100%;
}
.select-style {
display: flex;
justify-content: space-between;
}
2025-09-18 11:16:04 +08:00
2025-09-17 17:44:11 +08:00
.image-slot {
width: 100%;
height: 240px;
display: flex;
align-items: center;
justify-content: center;
color: #909399;
font-size: 24px;
}
</style>