UploadFile.vue 17 KB

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