From 10c2f5c8ef30027c4fd4e1243020b86ed708b6b8 Mon Sep 17 00:00:00 2001 From: "Mr.jiang" <714156421@qq.com> Date: Wed, 14 Aug 2024 15:49:15 +0800 Subject: [PATCH] =?UTF-8?q?=E9=99=90=E5=88=B6=20=E8=A7=86=E9=A2=91?= =?UTF-8?q?=E5=9C=88=20=E4=B8=8A=E4=BC=A0=E5=86=85=E5=AE=B9=E5=A4=A7?= =?UTF-8?q?=E5=B0=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/video/setVideo.vue | 111 ++++++++++++++++++++++++++++----------- 1 file changed, 80 insertions(+), 31 deletions(-) diff --git a/pages/video/setVideo.vue b/pages/video/setVideo.vue index f567599..194c8d4 100644 --- a/pages/video/setVideo.vue +++ b/pages/video/setVideo.vue @@ -42,7 +42,7 @@ - + @@ -146,46 +146,95 @@ that.videos.push(res.tempFilePath); for (let i = 0; i < that.videos.length; i++) { that.$queue.showLoading("上传中..."); - uni.uploadFile({ // 上传接口 - url: websocketUtils.uploadFileUrl(), //真实的接口地址 - filePath: that.videos[i], - name: 'file', - success: (uploadFileRes) => { - that.videosList = JSON.parse(uploadFileRes.data).data - console.log("that.videosList",that.videosList) - uni.hideLoading(); - } + uni.getFileInfo({ + filePath: that.videos[i], + success: (infoRes) => { + const size = infoRes.size; + if (size / 1024 / 1024 > 10) { + // 图片大于2MB + uni.showToast({ + icon:'error', + title:'视频超出10MB限制,请重新选择' + }) + } else { + // 图片小于等于2MB + uni.uploadFile({ // 上传接口 + url: websocketUtils.uploadFileUrl(), //真实的接口地址 + filePath: that.videos[i], + name: 'file', + success: (uploadFileRes) => { + that.videosList = JSON.parse(uploadFileRes.data).data + console.log("that.videosList",that.videosList) + uni.hideLoading(); + } + }); + } + }, + fail: () => { + console.log("上传是被") + // callback(false); + } }); } } }) }, - addImage(e) { + // 检查图片尺寸 + checkImageSize(filePath, callback) { + let that = this + uni.getFileInfo({ + filePath: filePath, + success: (infoRes) => { + const size = infoRes.size; + if (size / 1024 / 1024 > 2) { + // 图片大于2MB + uni.showToast({ + icon:'error', + title:'图片超出2MB限制,请重新选择' + }) + callback(false); + } else { + // 图片小于等于2MB + this.uploadImage(filePath) + callback(true); + } + }, + fail: () => { + callback(false); + } + }); + }, + // 上传图片 + uploadImage(filePath) { + console.log("filePath",filePath) + let that = this + uni.uploadFile({ // 上传接口 + url: websocketUtils.uploadFileUrl(), //真实的接口地址 + filePath: filePath, + name: 'file', + success: (uploadFileRes) => { + that.form.headImg = JSON.parse(uploadFileRes.data).data + uni.hideLoading(); + + } + }); + }, + addImage() { let that = this uni.chooseImage({ count: 1, sourceType: ['album', 'camera'], success: res => { - for (let i = 0; i < res.tempFilePaths.length; i++) { - that.$queue.showLoading("上传中..."); - uni.uploadFile({ // 上传接口 - url: websocketUtils.uploadFileUrl(), //真实的接口地址 - filePath: res.tempFilePaths[i], - name: 'file', - success: (uploadFileRes) => { - if (e == 1) { - that.form.front = JSON.parse(uploadFileRes.data).data - } else if (e == 2) { - that.form.back = JSON.parse(uploadFileRes.data).data - } else if (e == 4) { - that.form.headImg = JSON.parse(uploadFileRes.data).data - console.log(that.form.headImg) - } - uni.hideLoading(); - - } - }); - } + that.$queue.showLoading("上传中..."); + var tempFilePaths=res.tempFilePaths[0] + that.checkImageSize(tempFilePaths, (isValid) => { + if (isValid) { + // 图片校验通过后的逻辑 + console.log('图片校验通过,可以上传'); + } else { + console.log('图片超出大小限制,请重新选择'); + } + }); } }) },