UploadFile.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. <template>
  2. <div>
  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 />
  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. <FillDescribe :file-data="curFile" :visible.sync="visible" @fillDescribeToFile="fillDescribeToFile" />
  76. <SelectResource
  77. :visible.sync="visibleResource"
  78. :project-id="project_id"
  79. :accept="accept"
  80. :courseware-id="courseware_id"
  81. @selectResource="selectResource"
  82. />
  83. </div>
  84. </template>
  85. <script>
  86. import { fileUpload } from '@/api/app';
  87. import { conversionSize } from '@/utils/common';
  88. import FillDescribe from '../../common/FillDescribe.vue';
  89. import SelectResource from './SelectResource.vue';
  90. export default {
  91. name: 'UploadFile',
  92. components: {
  93. FillDescribe,
  94. SelectResource,
  95. },
  96. inject: ['property', 'courseware_id', 'project_id'],
  97. props: {
  98. // 课件id
  99. coursewareId: {
  100. type: String,
  101. default: '',
  102. },
  103. // 组件id
  104. componentId: {
  105. type: String,
  106. default: '',
  107. },
  108. // 组件标签
  109. labelText: {
  110. type: String,
  111. default: '',
  112. },
  113. // 上传支持的文件格式
  114. acceptFileType: {
  115. type: String,
  116. default: '*',
  117. },
  118. // 提示语
  119. uploadTip: {
  120. type: String,
  121. default: '',
  122. },
  123. // 图标
  124. iconClass: {
  125. type: String,
  126. default: '',
  127. },
  128. fileList: {
  129. type: Array,
  130. default: () => [],
  131. },
  132. fileIdList: {
  133. type: Array,
  134. default: () => [],
  135. },
  136. fileInfoList: {
  137. type: Array,
  138. default: () => [],
  139. },
  140. type: {
  141. type: String,
  142. default: '',
  143. },
  144. singleSize: {
  145. type: Number,
  146. default: 100,
  147. },
  148. totalSize: {
  149. type: Number,
  150. default: 1024,
  151. },
  152. limit: {
  153. type: Number,
  154. default: null,
  155. },
  156. canEditName: {
  157. type: Boolean,
  158. default: false,
  159. },
  160. fileInfo: {
  161. type: Object,
  162. default: () => ({}),
  163. },
  164. index: {
  165. // 如果是二维数组里循环上传 一维索引
  166. type: Number,
  167. default: null,
  168. },
  169. indexs: {
  170. // 如果是二维数组里循环上传 二维索引
  171. type: Number,
  172. default: null,
  173. },
  174. },
  175. data() {
  176. return {
  177. curFile: null,
  178. conversionSize,
  179. visible: false,
  180. content: {
  181. file_list: this.fileList,
  182. file_id_list: this.fileIdList,
  183. file_info_list: this.fileInfoList,
  184. file_info: this.fileInfo,
  185. },
  186. visibleResource: false,
  187. };
  188. },
  189. computed: {
  190. accept() {
  191. let accept = '*';
  192. if (this.acceptFileType.includes('.mp3')) {
  193. accept = 'audio';
  194. } else if (this.acceptFileType.includes('.mp4')) {
  195. accept = 'video';
  196. } else if (this.acceptFileType.includes('.jpg')) {
  197. accept = 'image';
  198. } else if (this.acceptFileType.includes('.zip')) {
  199. accept = 'h5_game';
  200. } else if (this.acceptFileType.includes('.txt')) {
  201. accept = 'text';
  202. }
  203. return accept;
  204. },
  205. },
  206. watch: {
  207. content: {
  208. handler(val) {
  209. this.$emit('updateFileList', val, this.index, this.indexs);
  210. },
  211. deep: true,
  212. },
  213. property: {
  214. handler(val) {
  215. if (val.isGetContent) {
  216. this.content = {
  217. file_list: this.fileList,
  218. file_id_list: this.fileIdList,
  219. file_info_list: this.fileInfoList,
  220. file_info: this.fileInfo,
  221. };
  222. }
  223. },
  224. deep: true,
  225. immediate: true,
  226. },
  227. },
  228. methods: {
  229. // 显示自定义样式文件列表
  230. onFileChange(file, fileList) {
  231. this.afterSelectFile(file);
  232. fileList.forEach((file) => {
  233. if (!file.progress || file.progress <= 0) file.progress = 0;
  234. });
  235. const lists = this.$refs.upload.uploadFiles;
  236. if (lists.length === 0) return;
  237. const files = lists.filter((item) => {
  238. let find = this.content.file_list.findIndex((p) => p.uid === item.uid);
  239. return find === -1;
  240. });
  241. if (this.limit !== null && this.content.file_list.length + files.length > this.limit) {
  242. this.$message.warning(
  243. `当前限制选择 ${this.limit} 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + this.content.file_list.length} 个文件`,
  244. );
  245. return;
  246. }
  247. this.content.file_list = [...this.content.file_list, ...files];
  248. },
  249. handleExceed(files, fileList) {
  250. this.$message.warning(
  251. `当前限制选择 ${this.limit} 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + fileList.length} 个文件`,
  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', 'obj', 'gltf', 'glb', 'mtl'];
  322. typeTip = '3D模型文件只能是 fbx、obj、gltf、glb、mtl 格式!';
  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. let form = new FormData();
  351. form.append(file.name, file.raw, file.name);
  352. fileUpload('Mid', form, {
  353. handleUploadProgress: (progressEvent) => {
  354. // 进度到99等服务器返回文件信息后才算实际完成
  355. let per = Number((progressEvent.progress * 99).toFixed(2) || 0);
  356. let en = this.content.file_list.find((p) => p.uid === file.uid);
  357. if (en) {
  358. en.progress = per;
  359. this.$forceUpdate();
  360. }
  361. },
  362. }).then(({ file_info_list }) => {
  363. let file_index = this.content.file_list.findIndex((p) => p.uid === file.uid);
  364. if (file_index > -1) {
  365. if (this.type === 'picture') {
  366. this.content.file_info_list[file_index] = {
  367. file_id: file_info_list[0].file_id,
  368. file_name: file_info_list[0].file_name,
  369. title: '',
  370. intro: '',
  371. };
  372. }
  373. this.content.file_list[file_index] = {
  374. file_id: file_info_list[0].file_id,
  375. file_name: file_info_list[0].file_name,
  376. file_url: file_info_list[0].file_url,
  377. };
  378. if (this.canEditName) {
  379. let obj = {
  380. xuhao: '',
  381. isEdit: false,
  382. file_name: file_info_list[0].file_name,
  383. };
  384. this.$set(this.content.file_info, file_info_list[0].file_id, obj);
  385. }
  386. this.content.file_id_list.push(file_info_list[0].file_id);
  387. this.$refs.upload.uploadFiles = [];
  388. this.$forceUpdate();
  389. }
  390. });
  391. });
  392. },
  393. // 显示弹窗
  394. viewDialog(file_id) {
  395. if (file_id) this.visible = true;
  396. this.curFile = this.content.file_info_list.find((file) => file.file_id === file_id);
  397. },
  398. // 给文件加介绍
  399. fillDescribeToFile(file) {
  400. let en = this.content.file_info_list.find((p) => p.file_id === file.file_id);
  401. if (en) {
  402. Object.assign(en, file);
  403. }
  404. },
  405. // 使用资源
  406. useResource() {
  407. this.visibleResource = true;
  408. },
  409. selectResource({ file_id, file_name, file_url, intro }) {
  410. this.content.file_list.push({ file_id, file_name, file_url });
  411. this.content.file_id_list.push(file_id);
  412. this.content.file_info_list.push({ file_id, file_name, title: '', intro });
  413. if (this.canEditName) {
  414. let obj = {
  415. xuhao: '',
  416. isEdit: false,
  417. file_name,
  418. };
  419. this.$set(this.content.file_info, file_id, obj);
  420. }
  421. this.visibleResource = false;
  422. },
  423. // 编辑文件名及序号
  424. changeIsEdit(file) {
  425. file.isEdit = !file.isEdit;
  426. },
  427. },
  428. };
  429. </script>
  430. <style lang="scss" scoped>
  431. .module-content {
  432. .file-area {
  433. display: flex;
  434. column-gap: 16px;
  435. align-items: center;
  436. .label-text {
  437. font-size: 14px;
  438. color: $font-light-color;
  439. }
  440. div {
  441. flex: 1;
  442. }
  443. }
  444. .el-divider {
  445. margin: 16px 0;
  446. }
  447. .upload-tip {
  448. margin-bottom: 16px;
  449. font-size: 12px;
  450. color: #86909c;
  451. }
  452. .upload-box {
  453. display: flex;
  454. justify-content: space-between;
  455. .el-button + .el-button {
  456. margin-left: 2px;
  457. }
  458. .file-uploader {
  459. flex: 1;
  460. :deep .el-upload {
  461. &--text {
  462. width: 100%;
  463. background-color: $fill-color;
  464. border-radius: 2px 0 0 2px;
  465. .el-button {
  466. width: 100%;
  467. color: #86909c;
  468. text-align: left;
  469. }
  470. }
  471. }
  472. }
  473. .el-button {
  474. border-radius: 0 2px 2px 0;
  475. }
  476. }
  477. .old_file_list {
  478. margin-top: 16px;
  479. }
  480. .file-list {
  481. display: flex;
  482. flex-direction: column;
  483. row-gap: 16px;
  484. li {
  485. display: flex;
  486. column-gap: 12px;
  487. align-items: center;
  488. .file-name {
  489. display: flex;
  490. column-gap: 14px;
  491. align-items: center;
  492. justify-content: space-between;
  493. max-width: 500px; // 360px有点窄
  494. padding: 8px 12px;
  495. font-size: 14px;
  496. color: #1d2129;
  497. background-color: #f7f8fa;
  498. span {
  499. display: flex;
  500. column-gap: 14px;
  501. align-items: center;
  502. }
  503. }
  504. .svg-icon {
  505. cursor: pointer;
  506. }
  507. }
  508. }
  509. }
  510. </style>