UploadFile.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <template>
  2. <div>
  3. <div class="file-area">
  4. <span class="label-text">{{ labelText }}</span>
  5. <div class="upload-box">
  6. <el-upload
  7. ref="upload"
  8. class="file-uploader"
  9. action="no"
  10. :accept="acceptFileType"
  11. :multiple="limit === null || limit > 1"
  12. :show-file-list="false"
  13. :auto-upload="false"
  14. :file-list="fileList"
  15. :on-change="onFileChange"
  16. :on-exceed="handleExceed"
  17. :limit="limit"
  18. >
  19. <el-button>选取{{ labelText }}文件</el-button>
  20. </el-upload>
  21. <el-button size="small" type="primary" @click="uploadFiles">上传</el-button>
  22. </div>
  23. </div>
  24. <el-divider />
  25. <div class="upload-tip">{{ uploadTip }}</div>
  26. <ul v-if="fileList.length > 0" slot="file-list" class="file-list">
  27. <li v-for="(file, i) in fileList" :key="i">
  28. <div class="file-name">
  29. <span>
  30. <SvgIcon :icon-class="iconClass" size="12" />
  31. <span>{{ file.file_name ?? file.name }}</span>
  32. <!-- <span>({{ file.size }})</span> -->
  33. </span>
  34. <span v-if="file.progress > 0 && file.progress < 100"> {{ file.progress }}% </span>
  35. <span v-else-if="file.file_id"> 完成 </span>
  36. </div>
  37. <SvgIcon icon-class="delete-black" size="12" @click="removeFile(file, i)" />
  38. <SvgIcon
  39. v-show="type === 'picture' && file.file_id"
  40. icon-class="mark"
  41. size="12"
  42. @click="viewDialog(file.file_id)"
  43. />
  44. </li>
  45. </ul>
  46. <FillDescribe :file-data="curFile" :visible.sync="visible" @fillDescribeToFile="fillDescribeToFile" />
  47. </div>
  48. </template>
  49. <script>
  50. import { fileUpload } from '@/api/app';
  51. import { conversionSize } from '@/utils/common';
  52. import FillDescribe from '../../common/FillDescribe.vue';
  53. export default {
  54. name: 'UploadFile',
  55. components: {
  56. FillDescribe,
  57. },
  58. inject: ['property'],
  59. props: {
  60. // 课件id
  61. coursewareId: {
  62. type: String,
  63. default: '',
  64. },
  65. // 组件id
  66. componentId: {
  67. type: String,
  68. default: '',
  69. },
  70. // 组件标签
  71. labelText: {
  72. type: String,
  73. default: '',
  74. },
  75. // 上传支持的文件格式
  76. acceptFileType: {
  77. type: String,
  78. default: '',
  79. },
  80. // 提示语
  81. uploadTip: {
  82. type: String,
  83. default: '',
  84. },
  85. // 图标
  86. iconClass: {
  87. type: String,
  88. default: '',
  89. },
  90. fileList: {
  91. type: Array,
  92. default: () => [],
  93. },
  94. fileIdList: {
  95. type: Array,
  96. default: () => [],
  97. },
  98. fileInfoList: {
  99. type: Array,
  100. default: () => [],
  101. },
  102. type: {
  103. type: String,
  104. default: '',
  105. },
  106. singleSize: {
  107. type: Number,
  108. default: 100,
  109. },
  110. totalSize: {
  111. type: Number,
  112. default: 1024,
  113. },
  114. limit: {
  115. type: Number,
  116. default: null,
  117. },
  118. },
  119. data() {
  120. return {
  121. curFile: null,
  122. conversionSize,
  123. visible: false,
  124. content: {
  125. file_list: this.fileList,
  126. file_id_list: this.fileIdList,
  127. file_info_list: this.fileInfoList,
  128. },
  129. };
  130. },
  131. watch: {
  132. content: {
  133. handler(val) {
  134. this.$emit('updateFileList', val);
  135. },
  136. deep: true,
  137. },
  138. property: {
  139. handler(val) {
  140. if (val.isGetContent) {
  141. this.content = {
  142. file_list: this.fileList,
  143. file_id_list: this.fileIdList,
  144. file_info_list: this.fileInfoList,
  145. };
  146. }
  147. },
  148. deep: true,
  149. immediate: true,
  150. },
  151. },
  152. methods: {
  153. // 显示自定义样式文件列表
  154. onFileChange(file, fileList) {
  155. this.afterSelectFile(file);
  156. fileList.forEach((file) => {
  157. if (!file.progress || file.progress <= 0) file.progress = 0;
  158. });
  159. const lists = this.$refs.upload.uploadFiles;
  160. if (lists.length === 0) return;
  161. const files = lists.filter((item) => {
  162. let find = this.content.file_list.findIndex((p) => p.uid === item.uid);
  163. return find === -1;
  164. });
  165. if (this.limit !== null && this.content.file_list.length + files.length > this.limit) {
  166. this.$message.warning(
  167. `当前限制选择 ${this.limit} 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + this.content.file_list.length} 个文件`,
  168. );
  169. return;
  170. }
  171. this.content.file_list = [...this.content.file_list, ...files];
  172. },
  173. handleExceed(files, fileList) {
  174. this.$message.warning(
  175. `当前限制选择 ${this.limit} 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + fileList.length} 个文件`,
  176. );
  177. },
  178. // 删除文件
  179. removeFile(file, i) {
  180. this.$confirm('是否删除当前文件?', '提示', {
  181. confirmButtonText: '确定',
  182. cancelButtonText: '取消',
  183. type: 'warning',
  184. })
  185. .then(() => {
  186. this.$refs.upload.handleRemove(file);
  187. this.content.file_list.splice(i, 1);
  188. this.content.file_id_list.splice(i, 1);
  189. })
  190. .catch(() => {});
  191. },
  192. // 文件校验
  193. afterSelectFile(file) {
  194. const fileName = file.name;
  195. let singleSizeTip = `文件[${fileName}]大小超过${conversionSize(this.singleSize * 1024 * 1024)},被移除!`;
  196. if (file.size > this.singleSize * 1024 * 1024) {
  197. this.$message.error(singleSizeTip);
  198. this.$refs.upload.handleRemove(file);
  199. return false;
  200. }
  201. const suffix = fileName.slice(fileName.lastIndexOf('.') + 1, fileName.length).toLowerCase();
  202. let fileType = [];
  203. let typeTip = '';
  204. if (this.type === 'audio') {
  205. fileType = ['mp3', 'acc', 'wma'];
  206. typeTip = '音频文件只能是 mp3、acc、wma 格式!';
  207. } else if (this.type === 'picture') {
  208. fileType = ['jpg', 'png', 'jpeg'];
  209. typeTip = '图片文件只能是 jpg、png、jpeg 格式!';
  210. } else if (this.type === 'video') {
  211. fileType = ['mp4'];
  212. typeTip = '视频文件只能是 mp4 格式!';
  213. } else if (this.type === 'upload_preview') {
  214. fileType = ['png', 'jpg', 'pdf'];
  215. typeTip = '文件只能是图片或者pdf';
  216. }
  217. const isNeedType = fileType.includes(suffix);
  218. if (!isNeedType) {
  219. typeTip += `,[${fileName}]被移除!`;
  220. this.$message.error(typeTip);
  221. this.$refs.upload.handleRemove(file);
  222. return false;
  223. }
  224. },
  225. // 上传文件
  226. uploadFiles() {
  227. const files = (this.content.file_list || []).filter((file) => file.uid && file.status !== 'success');
  228. if (files.length <= 0) {
  229. this.$message.error('没有需要上传的文件!');
  230. return false;
  231. }
  232. const totalSize = files.reduce((sum, cur) => sum + Number(cur.size || 0), 0);
  233. if (totalSize > this.totalSize * 1024 * 1024) {
  234. this.$message.error(`文件总大小不能超过${conversionSize(this.totalSize * 1024 * 1024)}!`);
  235. return false;
  236. }
  237. files
  238. .filter((p) => {
  239. let pro = p.progress || -1;
  240. return pro <= 0;
  241. })
  242. .forEach((file) => {
  243. let form = new FormData();
  244. form.append(file.name, file.raw, file.name);
  245. fileUpload('Mid', form, {
  246. handleUploadProgress: (progressEvent) => {
  247. // 进度到99等服务器返回文件信息后才算实际完成
  248. let per = Number((progressEvent.progress * 99).toFixed(2) || 0);
  249. let en = this.content.file_list.find((p) => p.uid === file.uid);
  250. if (en) {
  251. en.progress = per;
  252. this.$forceUpdate();
  253. }
  254. },
  255. }).then(({ file_info_list }) => {
  256. let file_index = this.content.file_list.findIndex((p) => p.uid === file.uid);
  257. if (file_index > -1) {
  258. if (this.type === 'picture') {
  259. this.content.file_info_list[file_index] = {
  260. file_id: file_info_list[0].file_id,
  261. file_name: file_info_list[0].file_name,
  262. title: '',
  263. intro: '',
  264. };
  265. }
  266. this.content.file_list[file_index] = {
  267. file_id: file_info_list[0].file_id,
  268. file_name: file_info_list[0].file_name,
  269. file_url: file_info_list[0].file_url,
  270. };
  271. this.content.file_id_list.push(file_info_list[0].file_id);
  272. this.$refs.upload.uploadFiles = [];
  273. this.$forceUpdate();
  274. }
  275. });
  276. });
  277. },
  278. // 显示弹窗
  279. viewDialog(file_id) {
  280. if (file_id) this.visible = true;
  281. this.curFile = this.content.file_info_list.find((file) => file.file_id === file_id);
  282. },
  283. // 给文件加介绍
  284. fillDescribeToFile(file) {
  285. let en = this.content.file_info_list.find((p) => p.file_id === file.file_id);
  286. if (en) {
  287. Object.assign(en, file);
  288. }
  289. },
  290. },
  291. };
  292. </script>
  293. <style lang="scss" scoped>
  294. .module-content {
  295. .file-area {
  296. display: flex;
  297. column-gap: 16px;
  298. align-items: center;
  299. .label-text {
  300. font-size: 14px;
  301. color: $font-light-color;
  302. }
  303. div {
  304. flex: 1;
  305. }
  306. }
  307. .el-divider {
  308. margin: 16px 0;
  309. }
  310. .upload-tip {
  311. margin-bottom: 16px;
  312. font-size: 12px;
  313. color: #86909c;
  314. }
  315. .upload-box {
  316. display: flex;
  317. justify-content: space-between;
  318. .file-uploader {
  319. flex: 1;
  320. :deep .el-upload {
  321. &--text {
  322. width: 100%;
  323. background-color: $fill-color;
  324. border-radius: 2px 0 0 2px;
  325. .el-button {
  326. width: 100%;
  327. color: #86909c;
  328. text-align: left;
  329. }
  330. }
  331. }
  332. }
  333. .el-button {
  334. border-radius: 0 2px 2px 0;
  335. }
  336. }
  337. .old_file_list {
  338. margin-top: 16px;
  339. }
  340. .file-list {
  341. display: flex;
  342. flex-direction: column;
  343. row-gap: 16px;
  344. li {
  345. display: flex;
  346. column-gap: 12px;
  347. align-items: center;
  348. .file-name {
  349. display: flex;
  350. column-gap: 14px;
  351. align-items: center;
  352. justify-content: space-between;
  353. max-width: 360px;
  354. padding: 8px 12px;
  355. font-size: 14px;
  356. color: #1d2129;
  357. background-color: #f7f8fa;
  358. span {
  359. display: flex;
  360. column-gap: 14px;
  361. align-items: center;
  362. }
  363. }
  364. .svg-icon {
  365. cursor: pointer;
  366. }
  367. }
  368. }
  369. }
  370. </style>