UploadFile.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. <template>
  2. <div class="upload-file">
  3. <div class="file-area">
  4. <span class="label-text" v-if="labelText">{{ labelText }}</span>
  5. <div class="upload-box">
  6. <el-upload
  7. v-show="false"
  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>上传附件</el-button> -->
  21. </el-upload>
  22. <el-button size="small" type="primary" @click="selectAndUpload"> 本地上传 </el-button>
  23. <el-button size="small" type="primary" @click="useResource"> 使用资源 </el-button>
  24. </div>
  25. </div>
  26. <div class="upload-tip">{{ uploadTip }}</div>
  27. <el-divider v-if="showDivider" />
  28. <ul v-if="fileList.length > 0" slot="file-list" class="file-list">
  29. <li v-for="(file, i) in fileList" :key="i">
  30. <div class="file-name">
  31. <span>
  32. <SvgIcon v-if="iconClass" :icon-class="iconClass" size="12" />
  33. <!-- 编辑序号和名称 -->
  34. <template v-if="content.file_info[file.file_id] && content.file_info[file.file_id].isEdit">
  35. <el-input v-model="content.file_info[file.file_id].xuhao" placeholder="序号" style="width: 80px" />
  36. <el-input v-model="content.file_info[file.file_id].file_name" placeholder="名称" />
  37. </template>
  38. <!-- 可以编辑序号名称状态下显示序号 -->
  39. <span v-else>{{
  40. canEditName && file.file_id
  41. ? content.file_info[file.file_id].xuhao + content.file_info[file.file_id].file_name
  42. : (file.file_name ?? file.name)
  43. }}</span>
  44. <!-- <span>({{ file.size }})</span> -->
  45. </span>
  46. <el-progress
  47. v-if="file.progress > 0 && file.progress < 100"
  48. type="circle"
  49. :percentage="file.progress"
  50. :width="20"
  51. color="#2A5AF6"
  52. stroke-linecap="butt"
  53. :show-text="false"
  54. />
  55. <span v-else-if="file.file_id"> 完成</span>
  56. </div>
  57. <SvgIcon icon-class="delete-black" size="12" @click="removeFile(file, i)" />
  58. <SvgIcon
  59. v-show="type === 'picture' && file.file_id"
  60. icon-class="mark"
  61. size="12"
  62. @click="viewDialog(file.file_id)"
  63. />
  64. <!-- 编辑名称和序号 -->
  65. <template v-if="canEditName && file.file_id">
  66. <SvgIcon
  67. v-if="content.file_info[file.file_id].isEdit"
  68. icon-class="icon-save"
  69. size="12"
  70. @click="changeIsEdit(content.file_info[file.file_id])"
  71. />
  72. <SvgIcon v-else icon-class="icon-edit" size="12" @click="changeIsEdit(content.file_info[file.file_id])" />
  73. </template>
  74. </li>
  75. </ul>
  76. <SelectResource
  77. :visible.sync="visibleResource"
  78. :project-id="projectId"
  79. :accept="accept"
  80. :courseware-id="coursewareId"
  81. from-page="remark"
  82. @selectResource="selectResource"
  83. />
  84. </div>
  85. </template>
  86. <script>
  87. import { fileUpload } from '@/api/app';
  88. import { conversionSize } from '@/utils/common';
  89. import { isEnable } from '@/views/book/courseware/data/common';
  90. import { getConfig, getToken } from '@/utils/auth';
  91. import SelectResource from '@/views/book/courseware/create/components/base/common/SelectResource.vue';
  92. import { SubmitFileToResourceStore } from '@/api/book';
  93. export default {
  94. name: 'UploadFile',
  95. components: {
  96. SelectResource,
  97. },
  98. props: {
  99. // 项目id
  100. projectId: {
  101. type: String,
  102. default: '',
  103. },
  104. // 课件id
  105. coursewareId: {
  106. type: String,
  107. default: '',
  108. },
  109. // 组件id
  110. componentId: {
  111. type: String,
  112. default: '',
  113. },
  114. // 组件标签
  115. labelText: {
  116. type: String,
  117. default: '',
  118. },
  119. // 上传支持的文件格式
  120. acceptFileType: {
  121. type: String,
  122. default: '*',
  123. },
  124. // 提示语
  125. uploadTip: {
  126. type: String,
  127. default: '',
  128. },
  129. // 图标
  130. iconClass: {
  131. type: String,
  132. default: '',
  133. },
  134. fileList: {
  135. type: Array,
  136. default: () => [],
  137. },
  138. fileIdList: {
  139. type: Array,
  140. default: () => [],
  141. },
  142. fileInfoList: {
  143. type: Array,
  144. default: () => [],
  145. },
  146. type: {
  147. type: String,
  148. default: '',
  149. },
  150. singleSize: {
  151. type: Number,
  152. default: 100,
  153. },
  154. totalSize: {
  155. type: Number,
  156. default: 1024,
  157. },
  158. limit: {
  159. type: Number,
  160. default: null,
  161. },
  162. canEditName: {
  163. type: Boolean,
  164. default: false,
  165. },
  166. fileInfo: {
  167. type: Object,
  168. default: () => ({}),
  169. },
  170. index: {
  171. // 如果是二维数组里循环上传 一维索引
  172. type: Number,
  173. default: null,
  174. },
  175. indexs: {
  176. // 如果是二维数组里循环上传 二维索引
  177. type: Number,
  178. default: null,
  179. },
  180. // 是否显示分割线
  181. showDivider: {
  182. type: Boolean,
  183. default: true,
  184. },
  185. },
  186. data() {
  187. return {
  188. curFile: null,
  189. conversionSize,
  190. visible: false,
  191. content: {
  192. file_list: this.fileList,
  193. file_id_list: this.fileIdList,
  194. file_info_list: this.fileInfoList,
  195. file_info: this.fileInfo,
  196. },
  197. visibleResource: false,
  198. isEnable,
  199. };
  200. },
  201. computed: {
  202. accept() {
  203. let accept = '*';
  204. if (this.acceptFileType.includes('.jpg')) {
  205. accept = 'image';
  206. } else if (this.acceptFileType.includes('.mp4')) {
  207. accept = 'video';
  208. } else if (this.acceptFileType.includes('.mp3')) {
  209. accept = 'audio';
  210. } else if (this.acceptFileType.includes('.zip')) {
  211. accept = 'h5_game';
  212. } else if (this.acceptFileType.includes('.txt')) {
  213. accept = 'text';
  214. }
  215. return accept;
  216. },
  217. },
  218. watch: {
  219. content: {
  220. handler(val) {
  221. this.$emit('updateFileList', val, this.index, this.indexs);
  222. },
  223. deep: true,
  224. },
  225. },
  226. methods: {
  227. selectAndUpload() {
  228. const uploadInput = this.$refs.upload.$el.querySelector('input[type="file"]');
  229. if (uploadInput) {
  230. uploadInput.value = '';
  231. uploadInput.click();
  232. }
  233. },
  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} 个文件,共选择了 ${
  249. files.length + this.content.file_list.length
  250. } 个文件`,
  251. );
  252. return;
  253. }
  254. this.content.file_list = [...this.content.file_list, ...files];
  255. // 选中文件后自动上传
  256. this.uploadFiles();
  257. },
  258. handleExceed(files, fileList) {
  259. this.$message.warning(
  260. `当前限制选择 ${this.limit} 个文件,本次选择了 ${files.length} 个文件,共选择了 ${
  261. files.length + fileList.length
  262. } 个文件`,
  263. );
  264. },
  265. // 删除文件
  266. removeFile(file, i) {
  267. this.$confirm('是否删除当前文件?', '提示', {
  268. confirmButtonText: '确定',
  269. cancelButtonText: '取消',
  270. type: 'warning',
  271. })
  272. .then(() => {
  273. this.$refs.upload.handleRemove(file);
  274. this.content.file_list.splice(i, 1);
  275. this.content.file_id_list.splice(i, 1);
  276. })
  277. .catch(() => {});
  278. },
  279. // 文件校验
  280. afterSelectFile(file) {
  281. const fileName = file.name;
  282. let singleSizeTip = `文件[${fileName}]大小超过${conversionSize(this.singleSize * 1024 * 1024)},被移除!`;
  283. if (file.size > this.singleSize * 1024 * 1024) {
  284. this.$message.error(singleSizeTip);
  285. this.$refs.upload.handleRemove(file);
  286. return false;
  287. }
  288. const suffix = fileName.slice(fileName.lastIndexOf('.') + 1, fileName.length).toLowerCase();
  289. let fileType = [];
  290. let typeTip = '';
  291. if (this.type === 'audio') {
  292. fileType = ['mp3', 'acc', 'wma', 'wav'];
  293. typeTip = '音频文件只能是 mp3、acc、wma、wav格式!';
  294. } else if (
  295. this.type === 'picture' ||
  296. this.type === 'image_text' ||
  297. this.type === 'drawing' ||
  298. this.type === 'character_structure' ||
  299. this.type === 'newWord_template' ||
  300. this.type === 'character'
  301. ) {
  302. fileType = ['jpg', 'png', 'jpeg'];
  303. typeTip = '图片文件只能是 jpg、png、jpeg 格式!';
  304. } else if (this.type === 'video' || this.type === 'video_interaction') {
  305. fileType = ['mp4'];
  306. typeTip = '视频文件只能是 mp4 格式!';
  307. } else if (this.type === 'upload_preview' || this.type === 'video_interaction_file') {
  308. fileType = [
  309. 'png',
  310. 'jpg',
  311. 'jpeg',
  312. 'txt',
  313. 'pdf',
  314. 'doc',
  315. 'docx',
  316. 'xls',
  317. 'xlsx',
  318. 'ppt',
  319. 'pptx',
  320. 'mp3',
  321. 'wma',
  322. 'mp4',
  323. 'mov',
  324. 'zip',
  325. 'rar',
  326. ];
  327. typeTip = '文件不支持';
  328. } else if (this.type === 'h5_games') {
  329. fileType = ['zip', 'html'];
  330. typeTip = 'H5游戏文件只能是 zip、html 格式!';
  331. } else if (this.type === '3DModel') {
  332. fileType = ['fbx', 'zip', 'gltf', 'glb'];
  333. typeTip = '3D模型文件只能是 fbx、gltf、glb、zip 格式!';
  334. } else if (this.type === 'upload_remark') {
  335. fileType = [
  336. 'png',
  337. 'jpg',
  338. 'jpeg',
  339. 'mp3',
  340. 'mp4',
  341. 'zip',
  342. 'rar',
  343. 'txt',
  344. 'pdf',
  345. 'doc',
  346. 'docx',
  347. 'xls',
  348. 'xlsx',
  349. 'ppt',
  350. 'pptx',
  351. ];
  352. typeTip = '文件不支持';
  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. };
  464. </script>
  465. <style lang="scss" scoped>
  466. .file-area {
  467. display: flex;
  468. column-gap: 16px;
  469. align-items: center;
  470. .label-text {
  471. font-size: 14px;
  472. color: $font-light-color;
  473. }
  474. div {
  475. flex: 1;
  476. }
  477. }
  478. .el-divider {
  479. margin: 16px 0;
  480. }
  481. .upload-tip {
  482. margin-bottom: 16px;
  483. font-size: 12px;
  484. color: #86909c;
  485. }
  486. .upload-box {
  487. // display: flex;
  488. // justify-content: space-between;
  489. .el-button + .el-button {
  490. margin-left: 2px;
  491. }
  492. .file-uploader {
  493. flex: 1;
  494. :deep .el-upload {
  495. &--text {
  496. width: 100%;
  497. background-color: $fill-color;
  498. border-radius: 2px 0 0 2px;
  499. .el-button {
  500. width: 100%;
  501. color: #86909c;
  502. text-align: left;
  503. }
  504. }
  505. }
  506. }
  507. .el-button {
  508. border-radius: 0 2px 2px 0;
  509. }
  510. }
  511. .old_file_list {
  512. margin-top: 16px;
  513. }
  514. .file-list {
  515. display: flex;
  516. flex-direction: column;
  517. row-gap: 16px;
  518. li {
  519. display: flex;
  520. column-gap: 12px;
  521. align-items: center;
  522. .file-name {
  523. display: flex;
  524. column-gap: 14px;
  525. align-items: center;
  526. justify-content: space-between;
  527. max-width: 500px; // 360px有点窄
  528. padding: 8px 12px;
  529. font-size: 14px;
  530. color: #1d2129;
  531. background-color: #f7f8fa;
  532. span {
  533. display: flex;
  534. column-gap: 14px;
  535. align-items: center;
  536. }
  537. }
  538. .svg-icon {
  539. cursor: pointer;
  540. }
  541. }
  542. }
  543. </style>