UploadFile.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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>选取{{ 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. <span>{{ file.file_name ?? file.name }}</span>
  33. </span>
  34. <el-progress
  35. v-if="file.progress > 0 && file.progress < 100"
  36. type="circle"
  37. :percentage="file.progress"
  38. :width="20"
  39. color="#2A5AF6"
  40. stroke-linecap="butt"
  41. :show-text="false"
  42. />
  43. <span v-else-if="file.file_id"> 完成 </span>
  44. </div>
  45. <SvgIcon icon-class="delete-black" size="12" @click="removeFile(file, i)" />
  46. <SvgIcon
  47. v-show="type === 'picture' && file.file_id"
  48. icon-class="mark"
  49. size="12"
  50. @click="viewDialog(file.file_id)"
  51. />
  52. </li>
  53. </ul>
  54. <FillDescribe :file-data="curFile" :visible.sync="visible" @fillDescribeToFile="fillDescribeToFile" />
  55. <SelectResource
  56. :visible.sync="visibleResource"
  57. :project-id="project_id"
  58. :accept="accept"
  59. :courseware-id="courseware_id"
  60. @selectResource="selectResource"
  61. />
  62. </div>
  63. </template>
  64. <script>
  65. import { fileUpload } from '@/api/app';
  66. import { conversionSize } from '@/utils/common';
  67. import FillDescribe from '../../common/FillDescribe.vue';
  68. import SelectResource from './SelectResource.vue';
  69. export default {
  70. name: 'UploadFile',
  71. components: {
  72. FillDescribe,
  73. SelectResource,
  74. },
  75. inject: ['property', 'courseware_id', 'project_id'],
  76. props: {
  77. // 课件id
  78. coursewareId: {
  79. type: String,
  80. default: '',
  81. },
  82. // 组件id
  83. componentId: {
  84. type: String,
  85. default: '',
  86. },
  87. // 组件标签
  88. labelText: {
  89. type: String,
  90. default: '',
  91. },
  92. // 上传支持的文件格式
  93. acceptFileType: {
  94. type: String,
  95. default: '*',
  96. },
  97. // 提示语
  98. uploadTip: {
  99. type: String,
  100. default: '',
  101. },
  102. // 图标
  103. iconClass: {
  104. type: String,
  105. default: '',
  106. },
  107. fileList: {
  108. type: Array,
  109. default: () => [],
  110. },
  111. fileIdList: {
  112. type: Array,
  113. default: () => [],
  114. },
  115. fileInfoList: {
  116. type: Array,
  117. default: () => [],
  118. },
  119. type: {
  120. type: String,
  121. default: '',
  122. },
  123. singleSize: {
  124. type: Number,
  125. default: 100,
  126. },
  127. totalSize: {
  128. type: Number,
  129. default: 1024,
  130. },
  131. limit: {
  132. type: Number,
  133. default: null,
  134. },
  135. },
  136. data() {
  137. return {
  138. curFile: null,
  139. conversionSize,
  140. visible: false,
  141. content: {
  142. file_list: this.fileList,
  143. file_id_list: this.fileIdList,
  144. file_info_list: this.fileInfoList,
  145. },
  146. visibleResource: false,
  147. };
  148. },
  149. computed: {
  150. accept() {
  151. let accept = '*';
  152. if (this.acceptFileType.includes('.mp3')) {
  153. accept = 'audio';
  154. } else if (this.acceptFileType.includes('.mp4')) {
  155. accept = 'video';
  156. } else if (this.acceptFileType.includes('.jpg')) {
  157. accept = 'image';
  158. } else if (this.acceptFileType.includes('.zip')) {
  159. accept = 'h5_game';
  160. } else if (this.acceptFileType.includes('.txt')) {
  161. accept = 'text';
  162. }
  163. return accept;
  164. },
  165. },
  166. watch: {
  167. content: {
  168. handler(val) {
  169. this.$emit('updateFileList', val);
  170. },
  171. deep: true,
  172. },
  173. property: {
  174. handler(val) {
  175. if (val.isGetContent) {
  176. this.content = {
  177. file_list: this.fileList,
  178. file_id_list: this.fileIdList,
  179. file_info_list: this.fileInfoList,
  180. };
  181. }
  182. },
  183. deep: true,
  184. immediate: true,
  185. },
  186. },
  187. methods: {
  188. // 显示自定义样式文件列表
  189. onFileChange(file, fileList) {
  190. this.afterSelectFile(file);
  191. fileList.forEach((file) => {
  192. if (!file.progress || file.progress <= 0) file.progress = 0;
  193. });
  194. const lists = this.$refs.upload.uploadFiles;
  195. if (lists.length === 0) return;
  196. const files = lists.filter((item) => {
  197. let find = this.content.file_list.findIndex((p) => p.uid === item.uid);
  198. return find === -1;
  199. });
  200. if (this.limit !== null && this.content.file_list.length + files.length > this.limit) {
  201. this.$message.warning(
  202. `当前限制选择 ${this.limit} 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + this.content.file_list.length} 个文件`,
  203. );
  204. return;
  205. }
  206. this.content.file_list = [...this.content.file_list, ...files];
  207. },
  208. handleExceed(files, fileList) {
  209. this.$message.warning(
  210. `当前限制选择 ${this.limit} 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + fileList.length} 个文件`,
  211. );
  212. },
  213. // 删除文件
  214. removeFile(file, i) {
  215. this.$confirm('是否删除当前文件?', '提示', {
  216. confirmButtonText: '确定',
  217. cancelButtonText: '取消',
  218. type: 'warning',
  219. })
  220. .then(() => {
  221. this.$refs.upload.handleRemove(file);
  222. this.content.file_list.splice(i, 1);
  223. this.content.file_id_list.splice(i, 1);
  224. })
  225. .catch(() => {});
  226. },
  227. // 文件校验
  228. afterSelectFile(file) {
  229. const fileName = file.name;
  230. let singleSizeTip = `文件[${fileName}]大小超过${conversionSize(this.singleSize * 1024 * 1024)},被移除!`;
  231. if (file.size > this.singleSize * 1024 * 1024) {
  232. this.$message.error(singleSizeTip);
  233. this.$refs.upload.handleRemove(file);
  234. return false;
  235. }
  236. const suffix = fileName.slice(fileName.lastIndexOf('.') + 1, fileName.length).toLowerCase();
  237. let fileType = [];
  238. let typeTip = '';
  239. if (this.type === 'audio') {
  240. fileType = ['mp3', 'acc', 'wma', 'wav'];
  241. typeTip = '音频文件只能是 mp3、acc、wma、wav格式!';
  242. } else if (this.type === 'picture' || this.type === 'image_text' || this.type === 'drawing') {
  243. fileType = ['jpg', 'png', 'jpeg'];
  244. typeTip = '图片文件只能是 jpg、png、jpeg 格式!';
  245. } else if (this.type === 'video' || this.type === 'video_interaction') {
  246. fileType = ['mp4'];
  247. typeTip = '视频文件只能是 mp4 格式!';
  248. } else if (this.type === 'upload_preview' || this.type === 'video_interaction_file') {
  249. fileType = [
  250. 'txt',
  251. 'pdf',
  252. 'doc',
  253. 'docx',
  254. 'xls',
  255. 'xlsx',
  256. 'ppt',
  257. 'pptx',
  258. 'mp3',
  259. 'wma',
  260. 'mp4',
  261. 'mov',
  262. 'zip',
  263. 'rar',
  264. ];
  265. typeTip = '文件不支持';
  266. } else if (this.type === 'h5_games') {
  267. fileType = ['zip'];
  268. typeTip = 'H5游戏文件只能是 zip 格式!';
  269. } else if (this.type === '3DModel') {
  270. fileType = ['fbx', 'obj', 'gltf', 'glb'];
  271. typeTip = '3D模型文件只能是 fbx、obj、gltf、glb 格式!';
  272. }
  273. const isNeedType = fileType.includes(suffix);
  274. if (!isNeedType) {
  275. typeTip += `,[${fileName}]被移除!`;
  276. this.$message.error(typeTip);
  277. this.$refs.upload.handleRemove(file);
  278. return false;
  279. }
  280. },
  281. // 上传文件
  282. uploadFiles() {
  283. const files = (this.content.file_list || []).filter((file) => file.uid && file.status !== 'success');
  284. if (files.length <= 0) {
  285. this.$message.error('没有需要上传的文件!');
  286. return false;
  287. }
  288. const totalSize = files.reduce((sum, cur) => sum + Number(cur.size || 0), 0);
  289. if (totalSize > this.totalSize * 1024 * 1024) {
  290. this.$message.error(`文件总大小不能超过${conversionSize(this.totalSize * 1024 * 1024)}!`);
  291. return false;
  292. }
  293. files
  294. .filter((p) => {
  295. let pro = p.progress || -1;
  296. return pro <= 0;
  297. })
  298. .forEach((file) => {
  299. let form = new FormData();
  300. form.append(file.name, file.raw, file.name);
  301. fileUpload('Mid', form, {
  302. handleUploadProgress: (progressEvent) => {
  303. // 进度到99等服务器返回文件信息后才算实际完成
  304. let per = Number((progressEvent.progress * 99).toFixed(2) || 0);
  305. let en = this.content.file_list.find((p) => p.uid === file.uid);
  306. if (en) {
  307. en.progress = per;
  308. this.$forceUpdate();
  309. }
  310. },
  311. }).then(({ file_info_list }) => {
  312. let file_index = this.content.file_list.findIndex((p) => p.uid === file.uid);
  313. if (file_index > -1) {
  314. if (this.type === 'picture') {
  315. this.content.file_info_list[file_index] = {
  316. file_id: file_info_list[0].file_id,
  317. file_name: file_info_list[0].file_name,
  318. title: '',
  319. intro: '',
  320. };
  321. }
  322. this.content.file_list[file_index] = {
  323. file_id: file_info_list[0].file_id,
  324. file_name: file_info_list[0].file_name,
  325. file_url: file_info_list[0].file_url,
  326. };
  327. this.content.file_id_list.push(file_info_list[0].file_id);
  328. this.$refs.upload.uploadFiles = [];
  329. this.$forceUpdate();
  330. }
  331. });
  332. });
  333. },
  334. // 显示弹窗
  335. viewDialog(file_id) {
  336. if (file_id) this.visible = true;
  337. this.curFile = this.content.file_info_list.find((file) => file.file_id === file_id);
  338. },
  339. // 给文件加介绍
  340. fillDescribeToFile(file) {
  341. let en = this.content.file_info_list.find((p) => p.file_id === file.file_id);
  342. if (en) {
  343. Object.assign(en, file);
  344. }
  345. },
  346. // 使用资源
  347. useResource() {
  348. this.visibleResource = true;
  349. },
  350. selectResource({ file_id, file_name, file_url, intro }) {
  351. this.content.file_list.push({ file_id, file_name, file_url });
  352. this.content.file_id_list.push(file_id);
  353. this.content.file_info_list.push({ file_id, file_name, title: '', intro });
  354. this.visibleResource = false;
  355. },
  356. },
  357. };
  358. </script>
  359. <style lang="scss" scoped>
  360. .module-content {
  361. .file-area {
  362. display: flex;
  363. column-gap: 16px;
  364. align-items: center;
  365. .label-text {
  366. font-size: 14px;
  367. color: $font-light-color;
  368. }
  369. div {
  370. flex: 1;
  371. }
  372. }
  373. .el-divider {
  374. margin: 16px 0;
  375. }
  376. .upload-tip {
  377. margin-bottom: 16px;
  378. font-size: 12px;
  379. color: #86909c;
  380. }
  381. .upload-box {
  382. display: flex;
  383. justify-content: space-between;
  384. .el-button + .el-button {
  385. margin-left: 2px;
  386. }
  387. .file-uploader {
  388. flex: 1;
  389. :deep .el-upload {
  390. &--text {
  391. width: 100%;
  392. background-color: $fill-color;
  393. border-radius: 2px 0 0 2px;
  394. .el-button {
  395. width: 100%;
  396. color: #86909c;
  397. text-align: left;
  398. }
  399. }
  400. }
  401. }
  402. .el-button {
  403. border-radius: 0 2px 2px 0;
  404. }
  405. }
  406. .old_file_list {
  407. margin-top: 16px;
  408. }
  409. .file-list {
  410. display: flex;
  411. flex-direction: column;
  412. row-gap: 16px;
  413. li {
  414. display: flex;
  415. column-gap: 12px;
  416. align-items: center;
  417. .file-name {
  418. display: flex;
  419. column-gap: 14px;
  420. align-items: center;
  421. justify-content: space-between;
  422. max-width: 500px; // 360px有点窄
  423. padding: 8px 12px;
  424. font-size: 14px;
  425. color: #1d2129;
  426. background-color: #f7f8fa;
  427. span {
  428. display: flex;
  429. column-gap: 14px;
  430. align-items: center;
  431. }
  432. }
  433. .svg-icon {
  434. cursor: pointer;
  435. }
  436. }
  437. }
  438. }
  439. </style>