index.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <template>
  2. <el-dialog
  3. class="show-file"
  4. :visible="dialogVisibleShowFile"
  5. width="900px"
  6. @close="dialogShowFileClose"
  7. >
  8. <div slot="title">查看文件【{{ fileName }}】</div>
  9. <template v-if="fileType === 'pdf'">
  10. <pdf v-for="i in numPages" :key="i" :src="pdfSrc" :page="i" />
  11. </template>
  12. <template v-else-if="isImage(fileType)">
  13. <div class="image-parent">
  14. <el-image fit="contain" :src="fileUrl" />
  15. </div>
  16. </template>
  17. <div v-else-if="fileType === 'mp3'" class="audio-file">
  18. <audio :src="fileUrl" controls></audio>
  19. </div>
  20. <div v-else-if="fileType === 'mp4'" class="video-file">
  21. <video :src="fileUrl" controls></video>
  22. </div>
  23. <template v-else>
  24. <iframe
  25. :src="'https://view.officeapps.live.com/op/view.aspx?src=' + `${fileUrl}`"
  26. width="100%"
  27. height="490px"
  28. scrolling="no"
  29. />
  30. </template>
  31. <div slot="footer">
  32. <el-button @click="dialogShowFileClose">关闭</el-button>
  33. </div>
  34. </el-dialog>
  35. </template>
  36. <script>
  37. import pdf from 'vue-pdf';
  38. import { GetFileStoreInfo } from '@/api/app';
  39. import { getFileType } from '@/utils/filter';
  40. export default {
  41. components: { pdf },
  42. props: {
  43. fileName: {
  44. default: '',
  45. type: String
  46. },
  47. fileId: {
  48. default: '',
  49. type: String
  50. }
  51. },
  52. data() {
  53. return {
  54. dialogVisibleShowFile: false,
  55. pdfSrc: '',
  56. numPages: 1,
  57. fileUrl: ''
  58. };
  59. },
  60. computed: {
  61. fileType() {
  62. return getFileType(this.fileName);
  63. }
  64. },
  65. watch: {
  66. fileId(newVal) {
  67. if (newVal.length > 0) {
  68. this.getFileStoreInfo();
  69. }
  70. }
  71. },
  72. methods: {
  73. getFileStoreInfo() {
  74. GetFileStoreInfo({ file_id: this.fileId }).then(({ file_url_https }) => {
  75. this.fileUrl = file_url_https;
  76. if (this.fileType === 'pdf') {
  77. this.getNumPages(file_url_https);
  78. }
  79. if (this.fileType === 'txt') {
  80. this.dialogVisibleShowFile = false;
  81. window.open(file_url_https, '_blank');
  82. }
  83. });
  84. },
  85. getNumPages(url) {
  86. let loadingTask = pdf.createLoadingTask(url);
  87. loadingTask.promise
  88. .then(pdf => {
  89. this.pdfSrc = loadingTask;
  90. this.numPages = pdf.numPages;
  91. })
  92. .catch(err => {
  93. console.error('pdf加载失败', err);
  94. this.$message.error('pdf加载失败');
  95. });
  96. },
  97. showDialog() {
  98. this.dialogVisibleShowFile = true;
  99. },
  100. dialogShowFileClose() {
  101. this.dialogVisibleShowFile = false;
  102. this.$emit('dialogShowFileClose');
  103. },
  104. isImage(type) {
  105. return ['jpeg', 'gif', 'jpg', 'png', 'bmp', 'pic', 'svg'].includes(type);
  106. }
  107. }
  108. };
  109. </script>
  110. <style lang="scss">
  111. @import '~@/styles/mixin.scss';
  112. .show-file {
  113. @include dialog;
  114. .el-dialog__header {
  115. font-weight: bold;
  116. }
  117. .image-parent {
  118. display: flex;
  119. justify-content: center;
  120. }
  121. .audio-file {
  122. @extend .image-parent;
  123. }
  124. .video-file {
  125. @extend .image-parent;
  126. }
  127. }
  128. </style>