|
@@ -63,10 +63,73 @@
|
|
|
deletePic(event) {
|
|
|
this.fileList.splice(event.index, 1)
|
|
|
},
|
|
|
+ // 图片压缩
|
|
|
+ compressH5(urlData, targetSizeKB, initialQuality = 1.0) {
|
|
|
+ const maxQuality = 1.0;
|
|
|
+ const minQuality = 0.0;
|
|
|
+ const tolerance = 0.01; // 根据需要调整公差
|
|
|
+ let that = this
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ let binarySearch = (min, max) => {
|
|
|
+ const midQuality = (min + max) / 2;
|
|
|
+ uni.getImageInfo({
|
|
|
+ src: urlData.url,
|
|
|
+ success(res) {
|
|
|
+ let img = new Image()
|
|
|
+ img.src = res.path
|
|
|
+ img.onload = function() {
|
|
|
+ const canvas = document.createElement('canvas');
|
|
|
+ const ctx = canvas.getContext('2d');
|
|
|
+
|
|
|
+ canvas.width = img.width;
|
|
|
+ canvas.height = img.height;
|
|
|
+
|
|
|
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
|
+ ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
|
|
|
+
|
|
|
+ // 使用异步的 toBlob 方法
|
|
|
+ canvas.toBlob(async (blob) => {
|
|
|
+ const fileSizeKB = blob.size / 1024;
|
|
|
+ if (Math.abs(fileSizeKB - targetSizeKB) <
|
|
|
+ tolerance || max - min < tolerance) {
|
|
|
+ // 当前质量足够接近目标大小,使用当前质量解析
|
|
|
+ const dataUrl = canvas.toDataURL(urlData.type, midQuality);
|
|
|
+ let result = that
|
|
|
+ .uploadFilePromise(dataUrl)
|
|
|
+ setTimeout(() => {
|
|
|
+ resolve(
|
|
|
+ result)
|
|
|
+ }, 1000)
|
|
|
+
|
|
|
+ } else if (fileSizeKB > targetSizeKB) {
|
|
|
+ // 如果文件大小太大,降低质量,继续二分查找
|
|
|
+ binarySearch(min, midQuality);
|
|
|
+ } else {
|
|
|
+ // 如果文件大小太小,增加质量,继续二分查找
|
|
|
+ binarySearch(midQuality, max);
|
|
|
+ }
|
|
|
+ }, urlData.type, midQuality);
|
|
|
+ };
|
|
|
+ },
|
|
|
+ fail() {
|
|
|
+ uni.showModal({
|
|
|
+ title: '提示',
|
|
|
+ content: '图片压缩失败',
|
|
|
+ showCancel: false
|
|
|
+ });
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ // 开始二分查找
|
|
|
+ binarySearch(minQuality, maxQuality);
|
|
|
+ })
|
|
|
+ },
|
|
|
// 新增图片
|
|
|
async afterRead(event) {
|
|
|
+
|
|
|
// 当设置 multiple 为 true 时, file 为数组格式,否则为对象格式
|
|
|
let lists = [].concat(event.file)
|
|
|
+
|
|
|
let fileListLen = this.fileList.length
|
|
|
lists.map((item) => {
|
|
|
this.fileList.push({
|
|
@@ -76,7 +139,15 @@
|
|
|
})
|
|
|
})
|
|
|
for (let i = 0; i < lists.length; i++) {
|
|
|
- const result = await this.uploadFilePromise(lists[i])
|
|
|
+
|
|
|
+ let result = ""
|
|
|
+ if (lists[i].size > (200 * 1024)) {
|
|
|
+ result = await this.compressH5(lists[i], 150);
|
|
|
+
|
|
|
+ } else {
|
|
|
+ result = await this.uploadFilePromise(lists[i].url)
|
|
|
+ }
|
|
|
+
|
|
|
let item = this.fileList[fileListLen]
|
|
|
|
|
|
this.fileList.splice(fileListLen, 1, Object.assign(item, {
|
|
@@ -93,12 +164,11 @@
|
|
|
return new Promise((resolve, reject) => {
|
|
|
let a = uni.uploadFile({
|
|
|
url: this.BASE_URL + '/gwfile/upload?uploadPath=signature',
|
|
|
- filePath: url.url,
|
|
|
+ filePath: url,
|
|
|
name: 'file',
|
|
|
header: {
|
|
|
"token": $auth.getUserToken()
|
|
|
},
|
|
|
- formData: url,
|
|
|
success: (res) => {
|
|
|
setTimeout(() => {
|
|
|
resolve(res.data)
|