UploadFile.vue 17 KB

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