index.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <template>
  2. <el-dialog class="show-file" :visible="dialogVisibleShowFile" width="900px" @close="dialogShowFileClose">
  3. <div slot="title">
  4. {{ $t('Key322') }}【{{ fileName }}】
  5. </div>
  6. <template v-if="fileType === 'pdf'">
  7. <pdf v-for="i in numPages" :key="i" :src="pdfSrc" :page="i" />
  8. </template>
  9. <template v-else-if="isImage(fileType)">
  10. <div class="image-parent">
  11. <el-image fit="contain" :src="fileUrl" />
  12. </div>
  13. </template>
  14. <div v-else-if="fileType === 'mp3'" class="audio-file">
  15. <audio :src="fileUrl" controls />
  16. </div>
  17. <div v-else-if="fileType === 'mp4'" class="video-file">
  18. <video :src="fileUrl" controls />
  19. </div>
  20. <div v-else-if="fileType === 'txt'" class="text-file">
  21. <el-input v-model="text" type="textarea" :readonly="true" resize="none" />
  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">
  33. {{ $t('Key246') }}
  34. </el-button>
  35. </div>
  36. </el-dialog>
  37. </template>
  38. <script>
  39. import pdf from 'vue-pdf';
  40. import { GetFileStoreInfo } from '@/api/app';
  41. import { getFileType } from '@/utils/filter';
  42. export default {
  43. name: 'ShowFile',
  44. components: { pdf },
  45. props: {
  46. fileName: {
  47. default: '',
  48. type: String
  49. },
  50. fileId: {
  51. default: '',
  52. type: String
  53. }
  54. },
  55. data() {
  56. return {
  57. dialogVisibleShowFile: false,
  58. pdfSrc: '',
  59. numPages: 1,
  60. fileUrl: '',
  61. text: ''
  62. };
  63. },
  64. computed: {
  65. fileType() {
  66. return getFileType(this.fileName);
  67. }
  68. },
  69. watch: {
  70. fileId(newVal) {
  71. if (newVal.length > 0) {
  72. this.getFileStoreInfo();
  73. }
  74. },
  75. dialogVisibleShowFile(newVal) {
  76. if (!newVal) {
  77. this.pdfSrc = '';
  78. this.numPages = 0;
  79. this.fileUrl = '';
  80. this.text = '';
  81. }
  82. }
  83. },
  84. created() {
  85. this.updateWordPack({
  86. word_key_list: ['Key322', 'Key246', 'Key323']
  87. });
  88. },
  89. methods: {
  90. getFileStoreInfo() {
  91. GetFileStoreInfo({ file_id: this.fileId }).then(({ file_url_https, file_relative_path }) => {
  92. this.fileUrl = file_url_https;
  93. if (this.fileType === 'pdf') {
  94. this.getNumPages(file_relative_path);
  95. }
  96. if (this.fileType === 'txt') {
  97. fetch(`${process.env.VUE_APP_PDF}${file_relative_path}`).then(async res => {
  98. if (!res.ok) return;
  99. this.text = await res.text();
  100. });
  101. }
  102. });
  103. },
  104. getNumPages(url) {
  105. let loadingTask = pdf.createLoadingTask(`${process.env.VUE_APP_PDF}${url}`);
  106. loadingTask.promise
  107. .then(pdf => {
  108. this.pdfSrc = loadingTask;
  109. this.numPages = pdf.numPages;
  110. })
  111. .catch(err => {
  112. console.error('pdf加载失败', err);
  113. this.$message.error(this.$i18n.t('Key323'));
  114. });
  115. },
  116. showDialog() {
  117. this.dialogVisibleShowFile = true;
  118. },
  119. dialogShowFileClose() {
  120. this.dialogVisibleShowFile = false;
  121. this.$emit('dialogShowFileClose');
  122. },
  123. isImage(type) {
  124. return ['jpeg', 'gif', 'jpg', 'png', 'bmp', 'pic', 'svg'].includes(type);
  125. }
  126. }
  127. };
  128. </script>
  129. <style lang="scss">
  130. @import '~@/styles/mixin';
  131. .show-file {
  132. @include dialog;
  133. .el-dialog__header {
  134. font-weight: bold;
  135. }
  136. %image-parent,
  137. .image-parent {
  138. display: flex;
  139. justify-content: center;
  140. }
  141. .audio-file {
  142. @extend %image-parent;
  143. }
  144. .video-file {
  145. @extend %image-parent;
  146. }
  147. .text-file {
  148. height: 60vh;
  149. .el-textarea,
  150. textarea {
  151. height: 100%;
  152. }
  153. }
  154. }
  155. </style>