UploadFile.vue 12 KB

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