fileViewer.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <template>
  2. <div class="sc-file-viewer">
  3. <el-dialog v-model="visible" fullscreen :show-close="false" @closed="$emit('closed')">
  4. <div class="sc-file-viewer__header">
  5. <div class="sc-file-viewer__title">
  6. {{ fileName }}
  7. <button class="el-button el-button--primary" aria-label="download" type="button" @click="downloadFile">
  8. <sc-iconify icon="ant-design:download-outlined" size="20"></sc-iconify>下载
  9. </button>
  10. </div>
  11. <button class="el-button is-link" aria-label="close" type="button" @click="visible = false">
  12. <sc-iconify icon="ant-design:close-outlined"></sc-iconify>
  13. </button>
  14. </div>
  15. <div v-loading="loading" class="sc-file-viewer__content">
  16. <component :is="`vue_office_${fileTypes[fileType].split('.')[0]}`" :src="'/minio' + filePath" :options="options" @rendered="loading = false" @error="loading = false"></component>
  17. </div>
  18. </el-dialog>
  19. <video-viewer v-if="showVideoViewer" :videoUrl="filePath" hideOnModal @close="showVideoViewer = false"></video-viewer>
  20. </div>
  21. </template>
  22. <script>
  23. import { VxeUI } from "vxe-pc-ui";
  24. import { fileTypes, officeOptions } from "./main";
  25. import vue_office_docx from "@vue-office/docx";
  26. import vue_office_excel from "@vue-office/excel";
  27. import vue_office_pdf from "@vue-office/pdf";
  28. import vue_office_txt from "@/components/scUpload/txtViewer";
  29. import videoViewer from "@/components/scUpload/videoViewer";
  30. import "@vue-office/docx/lib/index.css";
  31. import "@vue-office/excel/lib/index.css";
  32. export default {
  33. emits: ["closed"],
  34. components: {
  35. vue_office_docx,
  36. vue_office_excel,
  37. vue_office_pdf,
  38. vue_office_txt
  39. },
  40. data() {
  41. return {
  42. fileTypes,
  43. visible: false,
  44. loading: false,
  45. fileName: null,
  46. fileType: null,
  47. filePath: null,
  48. options: {},
  49. showVideoViewer: false
  50. }
  51. },
  52. methods: {
  53. init(uploadFile) {
  54. this.fileName = uploadFile.name;
  55. this.filePath = uploadFile.path;
  56. this.fileType = uploadFile.contentType;
  57. if (!fileTypes[this.fileType]) {
  58. ElMessage.warning("当前只支持预览 .png/.jpg/.map4/.avi/.txt/.docx/.pdf/.xlsx/.xls 格式文件, 文件已下载");
  59. this.downloadFile();
  60. } else {
  61. if (fileTypes[this.fileType] == "image") {
  62. VxeUI.previewImage({
  63. showDownloadButton: true,
  64. urlList: ["/minio" + uploadFile.path],
  65. downloadMethod: () => this.downloadFile()
  66. })
  67. } else if (fileTypes[this.fileType] == "video") this.showVideoViewer = true;
  68. else {
  69. this.loading = true;
  70. this.visible = true;
  71. this.options = officeOptions[fileTypes[this.fileType].split(".")[0]] || {};
  72. if (fileTypes[this.fileType].includes(".")) this.options.xls = fileTypes[this.fileType].split(".")[1] == "xls";
  73. }
  74. }
  75. },
  76. downloadFile() {
  77. this.$API.common.minio.download(this.filePath).then(res => {
  78. const a = document.createElement("a");
  79. const blob = new Blob([res], { type: this.fileType });
  80. a.download = this.fileName;
  81. a.href = URL.createObjectURL(blob);
  82. a.click();
  83. });
  84. }
  85. }
  86. }
  87. </script>
  88. <style lang="scss" scoped>
  89. .sc-file-viewer {
  90. --el-overlay-color-lighter: gray;
  91. :deep(.el-dialog) {
  92. --el-dialog-bg-color: transparent;
  93. padding: 0;
  94. .el-dialog__header {padding: 0;}
  95. .el-dialog__body {
  96. height: 100%;
  97. padding: 0;
  98. .sc-file-viewer__header {
  99. display: flex;
  100. justify-content: space-between;
  101. align-items: center;
  102. padding: var(--el-dialog-padding-primary);
  103. background: #fff;
  104. .sc-file-viewer__title {
  105. line-height: var(--el-dialog-font-line-height);
  106. font-size: var(--el-dialog-title-font-size);
  107. color: var(--el-text-color-primary);
  108. button {
  109. margin-left: var(--el-dialog-padding-primary);
  110. svg {
  111. margin-right: 6px;
  112. }
  113. }
  114. }
  115. .el-button.is-link:hover {color: var(--el-color-primary);}
  116. }
  117. .sc-file-viewer__content {
  118. height: calc(100% - 64px - 2 * var(--el-dialog-padding-primary));
  119. padding: var(--el-dialog-padding-primary);
  120. }
  121. }
  122. }
  123. }
  124. </style>