From 9e3ba31ef1811a47ab870ef63d1d81b902f72d91 Mon Sep 17 00:00:00 2001 From: aixianling Date: Fri, 7 Apr 2023 10:42:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E7=BB=84=E4=BB=B6=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E8=A7=86=E9=A2=91=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/AiImage/AiImage.vue | 5 +- src/components/AiUploader/AiUploader.vue | 62 ++++++++++++++---------- 2 files changed, 37 insertions(+), 30 deletions(-) diff --git a/src/components/AiImage/AiImage.vue b/src/components/AiImage/AiImage.vue index aafb06b..bce6957 100644 --- a/src/components/AiImage/AiImage.vue +++ b/src/components/AiImage/AiImage.vue @@ -31,10 +31,7 @@ export default { preview: Boolean, link: Boolean, miniapp: Boolean, - file: { - default: () => { - } - } + file: {default: () => ({})} }, methods: { ...mapActions(['previewFile', 'injectJWeixin']), diff --git a/src/components/AiUploader/AiUploader.vue b/src/components/AiUploader/AiUploader.vue index 1dc0816..ccfc204 100644 --- a/src/components/AiUploader/AiUploader.vue +++ b/src/components/AiUploader/AiUploader.vue @@ -11,7 +11,7 @@
- +
@@ -64,16 +64,22 @@ export default { fileId: String, mediaId: String, def: {default: () => []}, - action: {default: '/admin/file/add'}, + action: String, preview: Boolean, size: {default: 10 * 1024 * 1024}, disabled: Boolean, }, computed: { ...mapState(['token']), - errorImage() { - return this.$cdn + 'file.png' - }, + errorImage: v => v.$cdn + 'file.png', + api() { + if (this.action) return this.action + else return { + image: '/admin/file/add', + file: '/admin/file/add', + video: '/admin/file/addVideo', + }[this.type] + } }, watch: { def: { @@ -105,19 +111,17 @@ export default { upload(wait) { let count = this.limit - (this.fileList?.length || 0) if (count > 0) { - let params = { + const params = { count, sizeType: ['compressed'], sourceType: ['album', 'camera'], - success: (res) => { + success: res => { let count = this.fileList?.length + (res.tempFiles?.length || res.tempFile ? 1 : 0) if (count > this.limit && this.limit !== 1) { return this.$u.toast(`不能超过${this.limit}个`) } - if (res.tempFiles) { - res.tempFiles?.map((item) => { - this.uploadFile(item) - }) + if (res.tempFiles?.length > 0) { + res.tempFiles.map(this.uploadFile) } else if (res?.tempFile) { this.uploadFile(res.tempFile) } @@ -125,9 +129,9 @@ export default { } typeof wait == 'function' && wait() if (this.type == 'image') { - uni.chooseImage(params) + uni.chooseMedia({...params, mediaType: ['image']}) } else if (this.type == 'video') { - uni.chooseMedia(params) + uni.chooseMedia({...params, mediaType: ['video']}) } else { uni.chooseFile(params) } @@ -144,38 +148,43 @@ export default { this.$emit('manual', img) uni.hideLoading() } else { + const filePath = img.path || img.tempFilePath uni.uploadFile({ - url: this.$instance.defaults.baseURL + this.action, - filePath: img.path, + url: this.$instance.defaults.baseURL + this.api, + filePath, name: 'file', header: { 'Content-Type': 'multipart/form-data', - Authorization: uni.getStorageSync('token'), + Authorization: this.token, }, success: response => { const res = JSON.parse(response.data) if (res?.data) { this.$emit('data', res.data) this.$u.toast('上传成功!') - if (this.action.endsWith('/file/add')) { - this.fileList.push({url: res.data?.[0]?.split(";")?.[0], id: res.data?.[0]?.split(";")?.[1]}) - } else if (this.action == '/admin/file/add2') { + if (!this.action) {//默认组件接口 + if (this.type == 'image') { + const [image = "",] = res.data + this.fileList.push({url: image.split(";")[0], id: image.split(";")?.[1]}) + } else if (this.type == 'video') { + const [video = "", thumb = ""] = res.data + this.fileList.push({url: video.split(";")[0], id: video.split(";")?.[1], thumb: thumb.split(";")[0]}) + } + } else if (this.api == '/admin/file/add2') { let info = res.data this.$emit('update:fileId', info?.id) this.fileList.push(res.data) - } else if (this.action == '/admin/file/add-portrait') { + } else if (this.api == '/admin/file/add-portrait') { this.fileList.push({url: res.data?.split(";")?.[0], id: res.data?.split(";")?.[1]}) } - this.$emit("update:def", this.fileList) this.$emit("input", this.fileList) + this.$emit("update:def", this.fileList) } else { this.$u.toast(res.msg) } }, - fail: err => { - console.error(err) - }, - complete: () => uni.hideLoading() + fail: console.error, + complete: uni.hideLoading }) } @@ -212,9 +221,10 @@ export default { border-radius: 50%; overflow: hidden; background-color: #fff; + height: 40px; } - image { + image,video { width: 29vw; height: 29vw; }