FileView.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <template>
  2. <div class="preview-file">
  3. <div class="preview-file-header">
  4. <div class="type-list">
  5. <div
  6. v-for="{ type, name } in fileTypeList"
  7. :key="type"
  8. :class="['type-item', { active: type === curFileType }]"
  9. @click="changeFileType(type)"
  10. >
  11. {{ name }}
  12. </div>
  13. </div>
  14. <div class="file-download">
  15. <svg-icon icon-class="course-download" />
  16. 全部下载
  17. </div>
  18. </div>
  19. <div class="preview-file-content">
  20. <div
  21. v-for="{ file_id, file_name } in accessoryList"
  22. v-show="curFileType === fileTypeList[0].type || curFileFormat.includes(getFileType(file_name))"
  23. :key="file_id"
  24. class="file-item"
  25. >
  26. <div class="file-item-image">
  27. <div class="file-operation">
  28. <svg-icon icon-class="course-preview" @click="showFileVisible(file_name, file_id)" />
  29. <span class="vertical-bar"></span>
  30. <svg-icon icon-class="course-download" @click="downloadFileUrl(file_id, file_name)" />
  31. </div>
  32. <img :src="getFileImage(getFileType(file_name))" />
  33. </div>
  34. <span class="nowrap-ellipsis">{{ file_name }}</span>
  35. </div>
  36. </div>
  37. <hr />
  38. <div class="student-download">
  39. <div class="student-download-title">学生上传:</div>
  40. <div class="select-file">
  41. <el-button size="small">选择文件</el-button>
  42. <span class="tip">支持批量上传,上传格式支持mp3; mp4; jpg; png文件</span>
  43. </div>
  44. </div>
  45. </div>
  46. </template>
  47. <script>
  48. export default {
  49. name: 'PreviewFile'
  50. };
  51. </script>
  52. <script setup>
  53. import { computed, inject, ref } from 'vue';
  54. import { allowFormat, documentFormat, pictureFormat, videoFormat, audioFormat, compressFormat } from '@/utils/file.js';
  55. import { getFileType } from '@/utils/filter';
  56. import { downloadFileUrl } from '@/utils/common';
  57. defineProps({
  58. accessoryList: {
  59. type: Array,
  60. required: true
  61. }
  62. });
  63. const fileTypeList = [
  64. {
  65. name: '全部',
  66. type: 'all',
  67. format: allowFormat
  68. },
  69. {
  70. name: '文档',
  71. type: 'document',
  72. format: documentFormat
  73. },
  74. {
  75. name: '图片',
  76. type: 'image',
  77. format: pictureFormat
  78. },
  79. {
  80. name: '视频',
  81. type: 'video',
  82. format: videoFormat
  83. },
  84. {
  85. name: '音频',
  86. type: 'audio',
  87. format: audioFormat
  88. },
  89. {
  90. name: '压缩包',
  91. type: 'compress',
  92. format: compressFormat
  93. }
  94. ];
  95. let curFileType = ref(fileTypeList[0].type);
  96. let curFileFormat = computed((newVal) => {
  97. return fileTypeList.find(({ type }) => curFileType.value === type).format;
  98. });
  99. function changeFileType(type) {
  100. curFileType.value = type;
  101. }
  102. function getFileImage(type) {
  103. if (type === 'txt') {
  104. return require('@/assets/file/txt.png');
  105. }
  106. if (type === 'pdf') {
  107. return require('@/assets/file/pdf.png');
  108. }
  109. if (/^xlsx?$/.test(type)) {
  110. return require('@/assets/file/execl.png');
  111. }
  112. if (/^pptx?$/.test(type)) {
  113. return require('@/assets/file/ppt.png');
  114. }
  115. if (/^docx?$/.test(type)) {
  116. return require('@/assets/file/word.png');
  117. }
  118. }
  119. const showFileVisible = inject('showFileVisible');
  120. </script>
  121. <style lang="scss" scoped>
  122. .preview-file {
  123. &-header {
  124. display: flex;
  125. justify-content: space-between;
  126. padding-bottom: 16px;
  127. border-bottom: 1px dashed $border-color;
  128. .type-list {
  129. display: flex;
  130. column-gap: 40px;
  131. .type-item {
  132. font-size: 16px;
  133. font-weight: 500;
  134. cursor: pointer;
  135. &.active {
  136. color: #1890ff;
  137. }
  138. }
  139. }
  140. .file-download {
  141. color: #979797;
  142. cursor: pointer;
  143. }
  144. }
  145. &-content {
  146. display: flex;
  147. flex-wrap: wrap;
  148. row-gap: 16px;
  149. column-gap: 8px;
  150. padding-top: 16px;
  151. .file-item {
  152. display: flex;
  153. flex-direction: column;
  154. row-gap: 8px;
  155. width: 112px;
  156. height: 142px;
  157. &-image {
  158. position: relative;
  159. width: 112px;
  160. height: 112px;
  161. cursor: pointer;
  162. .file-operation {
  163. position: absolute;
  164. top: 0;
  165. left: 0;
  166. display: none;
  167. width: 100%;
  168. height: 100%;
  169. color: #fff;
  170. background-color: rgba(0, 0, 0, 60%);
  171. border-radius: 3px;
  172. .svg-icon {
  173. width: 16px;
  174. height: 16px;
  175. }
  176. .vertical-bar {
  177. width: 1px;
  178. height: 16px;
  179. background-color: #fff;
  180. }
  181. }
  182. &:hover .file-operation {
  183. display: flex;
  184. column-gap: 16px;
  185. align-items: center;
  186. justify-content: center;
  187. }
  188. }
  189. }
  190. }
  191. .student-download {
  192. &-title {
  193. padding-bottom: 8px;
  194. font-weight: bold;
  195. }
  196. .select-file {
  197. display: flex;
  198. column-gap: 24px;
  199. align-items: center;
  200. }
  201. }
  202. }
  203. </style>