FileView.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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" @click="allDownload">
  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" :title="file_name">{{ file_name }}</span>
  35. </div>
  36. </div>
  37. <hr />
  38. <div class="student-download">
  39. <div class="student-download-title">学生上传:</div>
  40. <template v-if="!isTeacher">
  41. <div class="select-file">
  42. <el-upload action="no" :http-request="uploadHomework" multiple :show-file-list="false">
  43. <el-button size="small">选择文件</el-button>
  44. </el-upload>
  45. <span class="tip">支持批量上传,上传格式支持mp3; mp4; jpg; png文件</span>
  46. </div>
  47. </template>
  48. <div class="preview-file-content">
  49. <div
  50. v-for="{ file_id, file_name } in homeworkList"
  51. v-show="curFileType === fileTypeList[0].type || curFileFormat.includes(getFileType(file_name))"
  52. :key="file_id"
  53. class="file-item"
  54. >
  55. <div class="file-item-image">
  56. <div class="file-operation student">
  57. <svg-icon icon-class="course-preview" @click="showFileVisible(file_name, file_id)" />
  58. <span class="vertical-bar"></span>
  59. <svg-icon icon-class="course-download" @click="downloadFileUrl(file_id, file_name)" />
  60. <span class="vertical-bar"></span>
  61. <svg-icon icon-class="delete-current" @click="deleteHomework(file_id)" />
  62. </div>
  63. <img :src="getFileImage(getFileType(file_name))" />
  64. </div>
  65. <span class="nowrap-ellipsis" :title="file_name">{{ file_name }}</span>
  66. </div>
  67. </div>
  68. </div>
  69. </div>
  70. </template>
  71. <script>
  72. export default {
  73. name: 'PreviewFile'
  74. };
  75. </script>
  76. <script setup>
  77. import { computed, inject, ref } from 'vue';
  78. import { allowFormat, documentFormat, pictureFormat, videoFormat, audioFormat, compressFormat } from '@/utils/file.js';
  79. import { getFileType } from '@/utils/filter';
  80. import { downloadFileUrl } from '@/utils/common';
  81. import { isAllowFileType, fileTypeSizeLimit } from '@/utils/validate';
  82. import { Message } from 'element-ui';
  83. import { fileUpload } from '@/api/app';
  84. const props = defineProps({
  85. accessoryList: {
  86. type: Array,
  87. required: true
  88. },
  89. homeworkList: {
  90. type: Array,
  91. required: true
  92. },
  93. addHomework: {
  94. type: Function,
  95. default: () => {}
  96. },
  97. deleteHomework: {
  98. type: Function,
  99. default: () => {}
  100. }
  101. });
  102. const fileTypeList = [
  103. {
  104. name: '全部',
  105. type: 'all',
  106. format: allowFormat
  107. },
  108. {
  109. name: '文档',
  110. type: 'document',
  111. format: documentFormat
  112. },
  113. {
  114. name: '图片',
  115. type: 'image',
  116. format: pictureFormat
  117. },
  118. {
  119. name: '视频',
  120. type: 'video',
  121. format: videoFormat
  122. },
  123. {
  124. name: '音频',
  125. type: 'audio',
  126. format: audioFormat
  127. },
  128. {
  129. name: '压缩包',
  130. type: 'compress',
  131. format: compressFormat
  132. }
  133. ];
  134. let curFileType = ref(fileTypeList[0].type);
  135. let curFileFormat = computed((newVal) => {
  136. return fileTypeList.find(({ type }) => curFileType.value === type).format;
  137. });
  138. function changeFileType(type) {
  139. curFileType.value = type;
  140. }
  141. function getFileImage(type) {
  142. if (type === 'txt') {
  143. return require('@/assets/file/txt.png');
  144. }
  145. if (type === 'pdf') {
  146. return require('@/assets/file/pdf.png');
  147. }
  148. if (/^xlsx?$/.test(type)) {
  149. return require('@/assets/file/execl.png');
  150. }
  151. if (/^pptx?$/.test(type)) {
  152. return require('@/assets/file/ppt.png');
  153. }
  154. if (/^docx?$/.test(type)) {
  155. return require('@/assets/file/word.png');
  156. }
  157. return require('@/assets/file/base.png');
  158. }
  159. const showFileVisible = inject('showFileVisible');
  160. const isTeacher = inject('isTeacher');
  161. const $t = inject('$t');
  162. // 全部下载
  163. function allDownload() {
  164. props.accessoryList.forEach(({ file_id, file_name }) => {
  165. downloadFileUrl(file_id, file_name);
  166. });
  167. }
  168. /**
  169. * 提交作业
  170. * @param {Object} file
  171. */
  172. function uploadHomework(file) {
  173. const fileName = file.file.name;
  174. if (!isAllowFileType(fileName)) {
  175. Message.error(`【${fileName}】${$t('Key335')}`);
  176. return;
  177. }
  178. if (fileTypeSizeLimit(file.file.name, file.file.size)) {
  179. return Message.error(`${fileName}文件大小超出限制`);
  180. }
  181. fileUpload('Mid', file).then(({ file_info_list }) => {
  182. if (file_info_list.length > 0) {
  183. const { file_id, file_name, file_url } = file_info_list[0];
  184. props.addHomework({ file_id, file_name, file_url });
  185. }
  186. });
  187. }
  188. </script>
  189. <style lang="scss" scoped>
  190. .preview-file {
  191. &-header {
  192. display: flex;
  193. justify-content: space-between;
  194. padding-bottom: 16px;
  195. border-bottom: 1px dashed $border-color;
  196. .type-list {
  197. display: flex;
  198. column-gap: 40px;
  199. .type-item {
  200. font-size: 16px;
  201. font-weight: 500;
  202. cursor: pointer;
  203. &.active {
  204. color: #1890ff;
  205. }
  206. }
  207. }
  208. .file-download {
  209. color: #979797;
  210. cursor: pointer;
  211. }
  212. }
  213. &-content {
  214. display: flex;
  215. flex-wrap: wrap;
  216. row-gap: 16px;
  217. column-gap: 8px;
  218. padding-top: 16px;
  219. .file-item {
  220. display: flex;
  221. flex-direction: column;
  222. row-gap: 8px;
  223. width: 112px;
  224. height: 142px;
  225. &-image {
  226. position: relative;
  227. width: 112px;
  228. height: 112px;
  229. cursor: pointer;
  230. border: 1px solid #ccc;
  231. .file-operation {
  232. position: absolute;
  233. top: 0;
  234. left: 0;
  235. display: none;
  236. width: 100%;
  237. height: 100%;
  238. color: #fff;
  239. background-color: rgba(0, 0, 0, 60%);
  240. border-radius: 3px;
  241. .svg-icon {
  242. width: 16px;
  243. height: 16px;
  244. }
  245. .vertical-bar {
  246. width: 1px;
  247. height: 16px;
  248. background-color: #fff;
  249. }
  250. }
  251. &:hover .file-operation {
  252. display: flex;
  253. column-gap: 16px;
  254. align-items: center;
  255. justify-content: center;
  256. &.student {
  257. column-gap: 8px;
  258. }
  259. }
  260. }
  261. }
  262. }
  263. .student-download {
  264. &-title {
  265. padding-bottom: 8px;
  266. font-weight: bold;
  267. }
  268. .select-file {
  269. display: flex;
  270. column-gap: 24px;
  271. align-items: center;
  272. }
  273. }
  274. }
  275. </style>