| | |
| | | // 后端地址 |
| | | action: { |
| | | type: String, |
| | | default: '' |
| | | default: 'http://127.0.0.1:8890/common/lmk/file/upload' |
| | | }, |
| | | // 最大上传数量 |
| | | maxCount: { |
| | |
| | | this.lists[index].error = false; |
| | | this.uploading = true; |
| | | // 创建上传对象 |
| | | |
| | | const task = uni.uploadFile({ |
| | | url: this.action, |
| | | filePath: this.lists[index].url, |
| | | name: this.name, |
| | | formData: this.formData, |
| | | header: this.header, |
| | | name: 'file', |
| | | // file: this.formData, |
| | | // header: this.header, |
| | | success: res => { |
| | | // 判断是否json字符串,将其转为json格式 |
| | | let data = this.toJson && this.$u.test.jsonString(res.data) ? JSON.parse(res.data) : res.data; |
| | |
| | | this.uploadError(index, data); |
| | | } else { |
| | | // 上传成功 |
| | | const fileType = this.getFileType(this.lists[index].url); |
| | | this.lists[index].response = data; |
| | | this.lists[index].progress = 100; |
| | | this.lists[index].error = false; |
| | | this.$emit('on-success', data, index, this.lists); |
| | | this.$emit('success', data, index, this.lists,fileType); |
| | | } |
| | | }, |
| | | fail: e => { |
| | |
| | | } |
| | | }); |
| | | }, |
| | | getFileType(filePath) { |
| | | const extension = filePath.split('.').pop().toLowerCase(); |
| | | // 图片类型 |
| | | const imageTypes = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp']; |
| | | // 视频类型 |
| | | const videoTypes = ['mp4', 'mov', 'avi', 'wmv', 'flv', 'm3u8']; |
| | | |
| | | if (imageTypes.includes(extension)) { |
| | | return 'image'; |
| | | } else if (videoTypes.includes(extension)) { |
| | | return 'video'; |
| | | } else { |
| | | return 'text'; // 其他所有类型归为text |
| | | } |
| | | }, |
| | | // 上传失败 |
| | | uploadError(index, err) { |
| | | this.lists[index].progress = 0; |