UploadFile.vue 15 KB

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