FilePreview.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. <template>
  2. <div class="page_dialog">
  3. <!-- v-model="previewVisible" -->
  4. <el-dialog
  5. @close="HandleDialogClose"
  6. class="preview_dialog el_button"
  7. :model-value="props.previewVisible"
  8. style="max-height: 90vh;overflow: auto;max-width: 900px;"
  9. :width="dialogWidth ? dialogWidth : '80%'"
  10. :title="props.title || '附件预览'"
  11. append-to-body
  12. :align-center="true"
  13. >
  14. <div class="custom">
  15. <!-- 内部 class="preview_introduce" -->
  16. <slot></slot>
  17. </div>
  18. <div v-if="filePreviewList.includes(previewType)" class="preview_item">
  19. <VuePdfEmbed
  20. class="preview_main"
  21. v-if="previewType == 'pdf'"
  22. annotation-layer
  23. :scale="1"
  24. text-layer
  25. :source="fileUrl"
  26. />
  27. <vue-office-excel
  28. v-else-if="previewType == 'xlsx'"
  29. :src="fileUrl"
  30. :options="excelOptions"
  31. style="height: 100vh;"
  32. @rendered="renderedHandler"
  33. @error="errorHandler"
  34. />
  35. <vue-office-docx
  36. class="preview_main"
  37. v-else-if="previewType == 'docx'"
  38. :src="fileUrl"
  39. @rendered="renderedHandler"
  40. @error="errorHandler"
  41. />
  42. <video class="preview_main" controls width="100%" v-else-if="['mp4','avi','mpg','mpeg','wmv','mkv','mov','flv','mpg'].includes(previewType)" :src="fileUrl" />
  43. <!-- <iframe
  44. v-else-if="['ppt','pptx'].includes(previewType)"
  45. :src="`https://view.officeapps.live.com/op/embed.aspx?src=${encodeURIComponent(fileUrl)}`"
  46. width="100%"
  47. height="600px"
  48. frameborder="0"
  49. ></iframe> -->
  50. <div
  51. v-else-if="['pptx'].includes(previewType)"
  52. class="vue_ppt" id="vue_ppt" ref="vuePpt"
  53. >
  54. <div v-if="pptLoading" class="loading_bg">
  55. <el-icon class="is-loading">
  56. <Loading />
  57. </el-icon>
  58. 加载中
  59. </div>
  60. <!-- ppt加载中 -->
  61. </div>
  62. <img @click="HandleImgPreview" style="text-align: center;" v-else-if="['jpg','png','jpeg','gif','svg','webp'].includes(previewType)" :src="fileUrl"/>
  63. </div>
  64. <div class="preview_item" v-else-if="fileName && fileUrl">
  65. <span>不支持预览,请点击下载: <a :href="fileUrl">{{ fileName }}</a></span>
  66. </div>
  67. <template v-if="props.customFooter" #footer>
  68. <el-button @click="HandleDialogClose" class="button_border_gray cancel" >取 消</el-button>
  69. <slot name="footer"></slot>
  70. <slot name="confirm">
  71. <el-button :disabled="props.disabledConfirm" :loading="props.submitLoading" @click="props.disabledConfirm ? '' : HandleConfirmCheck()" class="button_background" >确 定</el-button>
  72. </slot>
  73. </template>
  74. </el-dialog>
  75. <el-image-viewer
  76. v-if="imgVisible"
  77. :url-list="[fileUrl]"
  78. show-progress
  79. @close="imgVisible = false"
  80. />
  81. </div>
  82. </template>
  83. <script setup lang="ts">
  84. import {init} from 'pptx-preview'
  85. import { watch ,ref } from 'vue'
  86. import VuePdfEmbed from 'vue-pdf-embed'
  87. // optional styles
  88. import 'vue-pdf-embed/dist/styles/annotationLayer.css'
  89. import 'vue-pdf-embed/dist/styles/textLayer.css'
  90. //引入VueOfficeExcel组件
  91. import VueOfficeExcel from '@vue-office/excel'
  92. //引入相关样式
  93. import '@vue-office/excel/lib/index.css'
  94. //引入VueOfficeDocx组件
  95. import VueOfficeDocx from '@vue-office/docx'
  96. //引入相关样式
  97. import '@vue-office/docx/lib/index.css'
  98. interface FileMsg {
  99. name:String,
  100. url:String
  101. }
  102. const props = defineProps({
  103. fileMessage:{
  104. required:true,
  105. type:Object
  106. },
  107. title:{
  108. type:String
  109. },
  110. customFooter:{
  111. type:Boolean,
  112. required:false
  113. },
  114. previewVisible:{
  115. type:Boolean,
  116. required:true
  117. },
  118. submitLoading:{
  119. type:Boolean,
  120. default:false,
  121. },
  122. disabledConfirm:{
  123. type:Boolean,
  124. default:false
  125. },
  126. dialogWidth:{
  127. type:String,
  128. }
  129. })
  130. const emit = defineEmits(['close','confirm'])
  131. const pptxPrviewer = ref()
  132. const vuePpt = ref()
  133. const pptLoading = ref(false)
  134. const fileUrl = ref()
  135. const fileName = ref('')
  136. // const previewVisible = ref(false)
  137. const previewType = ref('')
  138. const imgVisible = ref(false)
  139. const excelOptions = {
  140. xls: false, //预览xlsx文件设为false;预览xls文件设为true
  141. minColLength: 0, // excel最少渲染多少列,如果想实现xlsx文件内容有几列,就渲染几列,可以将此值设置为0.
  142. minRowLength: 0, // excel最少渲染多少行,如果想实现根据xlsx实际函数渲染,可以将此值设置为0.
  143. widthOffset: 10, //如果渲染出来的结果感觉单元格宽度不够,可以在默认渲染的列表宽度上再加 Npx宽
  144. heightOffset: 10, //在默认渲染的列表高度上再加 Npx高
  145. beforeTransformData: (workbookData) => {return workbookData}, //底层通过exceljs获取excel文件内容,通过该钩子函数,可以对获取的excel文件内容进行修改,比如某个单元格的数据显示不正确,可以在此自行修改每个单元格的value值。
  146. transformData: (workbookData) => {return workbookData}, //将获取到的excel数据进行处理之后且渲染到页面之前,可通过transformData对即将渲染的数据及样式进行修改,此时每个单元格的text值就是即将渲染到页面上的内容
  147. }
  148. const renderedHandler = ()=> {
  149. console.log("渲染完成")
  150. }
  151. const errorHandler = () => {
  152. console.log("渲染失败")
  153. }
  154. const HandleImgPreview = ()=>{
  155. imgVisible.value = true
  156. }
  157. const HandleDialogClose = ()=>{
  158. emit('close')
  159. console.log('弹窗关闭===')
  160. // previewVisible.value = false
  161. fileUrl.value = ''
  162. fileName.value = ''
  163. previewType.value = ''
  164. }
  165. const HandleConfirmCheck = async () => {
  166. emit('confirm')
  167. }
  168. const HandlePptPreview =(url)=>{
  169. console.log('初始化ppt中')
  170. if(url && vuePpt.value){
  171. pptLoading.value = true
  172. //调用库的init方法生成一个预览器
  173. pptxPrviewer.value = init(vuePpt.value, {
  174. // width: (vuePpt.value?.clientWidth * 0.8),
  175. width: (vuePpt.value?.clientWidth),
  176. // height: 540
  177. })
  178. //获取文件或者读取文件,获取文件的 ArrayBuffer格式数据,传给组件进行预览
  179. fetch(props?.fileMessage?.url).then(response=>{
  180. return response.arrayBuffer()
  181. }).then(res =>{
  182. console.log('获取文件完成',res)
  183. // 在这里监听文档的滚动
  184. let pptDom = document.querySelectorAll('.vue_ppt')
  185. console.log('dom',pptDom)
  186. //调用预览器的preview方法
  187. pptxPrviewer.value.preview(res).then(previewRes=>{
  188. // 此处数据加载成功
  189. console.log('转换结果',previewRes)
  190. pptLoading.value = false
  191. }).catch(err=>{
  192. // 解析失败
  193. pptLoading.value = false
  194. })
  195. }).catch(err=>{
  196. // 获取文件失败
  197. pptLoading.value = false
  198. })
  199. }
  200. }
  201. const filePreviewList = ref(['pptx','pdf','gif','jpeg','jpg','png','svg','webp','xlsx','docx','mp4','avi','mpg','mpeg','wmv','mkv','mov','flv','mpg'])
  202. watch(()=>props.fileMessage,(newVal)=>{
  203. // previewVisible.value = true
  204. let suffixName = newVal?.name?.split('.').pop()
  205. if(suffixName){
  206. fileName.value = newVal.name
  207. fileUrl.value = newVal.url
  208. if(filePreviewList.value.includes(suffixName)){
  209. previewType.value = suffixName
  210. }
  211. /*
  212. else{
  213. let link = document.createElement('a')
  214. link.setAttribute('href',newVal.url)
  215. document.body.appendChild(link)
  216. link.click()
  217. console.log('link',link)
  218. document.body.removeChild(link)
  219. }
  220. */
  221. }
  222. })
  223. watch(()=>vuePpt.value,(newVal)=>{
  224. if(newVal){
  225. HandlePptPreview(props.fileMessage?.url)
  226. }
  227. })
  228. </script>
  229. <style lang="scss" scoped>
  230. .preview_item{
  231. background-color: #f0f4f8;
  232. border-radius: 8px;
  233. padding:10px;
  234. overflow: auto;
  235. display: flex;
  236. flex-direction: column;
  237. .preview_main{
  238. height: 100%;
  239. overflow: auto;
  240. border-radius: 8px;
  241. width: 100% !important;
  242. }
  243. }
  244. .vue_ppt{
  245. display: flex;
  246. position: relative;
  247. flex-direction: column;
  248. align-items: center;
  249. justify-content: flex-start;
  250. min-height: 270px;
  251. // min-height: 100%;
  252. overflow: auto;
  253. background-color: #e8eaee;
  254. .loading_bg{
  255. width: 100%;
  256. flex: 1 1;
  257. display: flex;
  258. justify-content: center;
  259. align-items: center;
  260. gap:3px;
  261. color:#2e64fa;
  262. }
  263. }
  264. </style>
  265. <style lang="scss">
  266. .preview_dialog{
  267. overflow: auto;
  268. border-radius: 8px;
  269. padding: 0;
  270. .el-dialog__headerbtn:focus .el-dialog__close,
  271. .el-dialog__headerbtn:hover .el-dialog__close {
  272. color: #999;
  273. }
  274. .el-dialog__header {
  275. width: 100%;
  276. height: 48px;
  277. line-height: 48px;
  278. text-indent: 20px;
  279. background-color: #f5f7fa;
  280. border-bottom: 1px solid #f5f7fa;
  281. border-top-right-radius: 8px;
  282. border-top-left-radius: 8px;
  283. padding: 0 !important;
  284. text-align: left;
  285. .el-dialog__title {
  286. font-weight: 600;
  287. font-size: 16px;
  288. color: #303133;
  289. }
  290. button:focus, button:focus-visible {
  291. outline: none;
  292. }
  293. }
  294. //饿了么内容样式覆盖
  295. .el-dialog__body {
  296. font-weight: 400;
  297. font-size: 16px;
  298. color: #333;
  299. padding: 20px;
  300. width: 100%;
  301. margin: auto;
  302. display: flex;
  303. flex-direction: column;
  304. justify-content: flex-start;
  305. max-height: calc(90vh - 50px - 66px);
  306. img{
  307. border-radius: 8px;
  308. align-self: center;
  309. }
  310. .custom{
  311. .preview_introduce{
  312. background-color: #fafafa;
  313. border: solid #e9e9e9 1px !important;
  314. padding:10px;
  315. margin-bottom: 10px;
  316. border-radius: 8px;
  317. display: flex;
  318. flex-direction: row;
  319. justify-content: flex-start;
  320. align-items: flex-start;
  321. }
  322. }
  323. }
  324. .el-dialog__footer{
  325. padding:15px 20px;
  326. border-top:solid #EBEEF5 1px;
  327. }
  328. }
  329. .preview_main{
  330. .docx-wrapper{
  331. // width: 100% !important;
  332. background-color: #f0f4f8 !important;
  333. overflow: auto;
  334. box-sizing: border-box;
  335. padding-top: 20px;
  336. padding-bottom: 20px;
  337. .docx{
  338. width: 100% !important;
  339. overflow: auto;
  340. }
  341. }
  342. .vue-pdf-embed__page{
  343. width: 100% !important;
  344. overflow: auto;
  345. padding:20px;
  346. background-color: #f0f4f8 !important;
  347. canvas{
  348. width: 100% !important;
  349. background-color: #f0f4f8 !important;
  350. }
  351. .textLayer{
  352. width: 100% !important;
  353. height: 100% !important;
  354. }
  355. .annotationLayer{
  356. width: 100% !important;
  357. height: 100% !important;
  358. }
  359. }
  360. }
  361. </style>