UploadFile.vue 15 KB

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