index.vue
2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<template>
<div class="attachment-preview">
<el-card>
<template v-if="loading">
正在加载...
</template>
<template v-else>
<!-- PDF 预览 -->
<div v-if="isPdf">
<vue-office-pdf
:src="docx"
style="height: 60vh"
@rendered="rendered"
:key="fileId"
/>
</div>
<!-- 图片预览 -->
<div v-else-if="isImage">
<el-image :src="previewUrl" style="height: 60vh"/>
</div>
<!-- 不支持的类型 -->
<div v-else>
<p>当前附件类型不支持预览。</p>
</div>
</template>
</el-card>
</div>
</template>
<script>
//import pdf from 'vue-pdf';
import VueOfficePdf from '@vue-office/pdf';
export default {
name: "AttachmentPreview",
components: {
//pdf,
VueOfficePdf
},
props: {
fileId: {
type: String,
required: false,
},
fileType: {
type: String,
required: true,
},
filePath: {
type: String,
required: false,
}
},
data() {
return {
docx: "",
loading: true,
};
},
computed: {
isPdf() {
return this.fileType === "pdf";
},
isImage() {
return ["jpg", "jpeg", "png", "gif", "bmp"].includes(this.fileType);
},
},
mounted() {
this.loadPreview();
},
watch: {
fileId(newVal) {
this.loadPreview();
}
},
methods: {
/**
* 加载附件预览 URL
*/
loadPreview() {
this.previewUrl = process.env.VUE_APP_BASE_API+"/view/attachement/preview?id=" + this.fileId;
if(this.filePath != '' && this.filePath != null) {
this.previewUrl = process.env.VUE_APP_BASE_API+"/view/attachement/preview?path=" + this.filePath;
}
console.log(this.previewUrl);
fetch(this.previewUrl)
.then(response => response.blob())
.then(blob => {
this.docx = blob;
});
this.loading = false;
},
/**
* 根据文件路径获取文件类型
*/
getFileType() {
return this.fileType;
},
rendered(){
//console.log("渲染完成")
},
},
};
</script>
<style>
.attachment-preview {
padding: 0px;
}
</style>