UploadFile.vue 20 KB

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