期望的结果
index = 0 按(图片压缩成功)=>(图片格式转换成功)=>(图片检测结果)的顺序执行完,再执行index = 1
onChangeFlockData: function (e) {
wx.chooseImage({
count: 3,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success: async res => {
const tempFilePaths = res.tempFilePaths;
this.setData({
tempFile: [...this.data.tempFile, ...tempFilePaths]
});
for (let i = 0; i < this.data.tempFile.length; i++) {
await this.compressImg(this.data.tempFile[i], i)
}
}
})
},
//图片压缩
async compressImg(imgUrl, index) {
return new Promise((resolve, reject) => {
wx.getImageInfo({
src: imgUrl,
}).then(res => {
const imgInfo = res.path;
const imgWidth = res.width;
const imgHeight = res.height;
const query = wx.createSelectorQuery()
query.select('#canvas')
.fields({
node: true,
size: true
})
.exec(async res => {
const canvas = res[0].node;
const ctx = canvas.getContext('2d');
const dpr = wx.getSystemInfoSync().pixelRatio;
const imgW = Math.trunc(imgWidth / dpr);
const imgH = Math.trunc(imgW / imgWidth * imgHeight);
canvas.width = imgW;
canvas.height = imgH;
ctx.clearRect(0, 0, imgW, imgH);
this.setData({
canvasWidth: imgW,
canvasHeight: imgH
});
let imageObj = canvas.createImage();
imageObj.src = imgInfo;
imageObj.onload = (res) => {
ctx.drawImage(imageObj, 0, 0, imgW, imgH)
};
const cfgSave = {
fileType: "jpg",
quality: 0.5,
width: imgW,
height: imgH,
destWidth: imgW,
destHeight: imgH,
canvas: canvas,
};
wx.canvasToTempFilePath({
...cfgSave,
}).then(async res => {
console.log("图片压缩成功:::", res.tempFilePath + "index:::", index)
resolve(res.tempFilePath)
let tempUrl = res.tempFilePath;
await this.imgSecCheck(tempUrl, index);
}).catch(err => {
reject(err)
})
})
})
})
},
//图片送审
imgSecCheck: async function (tempUrl, index) {
wx.showLoading({
mask: true
});
wx.getFileSystemManager().readFile({
filePath: tempUrl,
encoding: "base64",
success: async (res) => {
console.log("图片格式转换成功:::", res + "index:::", index);
let imgBuffer = res.data
await this.wait(imgBuffer, index)
},
fail: err => {
console.error(err);
},
})
},
wait: async function (imgBuffer, index) {
return new Promise((resolve, reject) => {
wx.cloud.callFunction({
name: "msgSecCheck",
data: {
type: 'imgSecCheckBuffer',
value: imgBuffer,
}
}).then(res => {
console.log("图片检测结果:", res.result + "index:::", index)
wx.hideLoading()
if (res.result.errCode === 87014) {
wx.hideLoading()
wx.showToast({
title: '图片含有违法违规内容',
icon: 'none'
})
return
}
resolve(res.result)
// this._uploadImg(tempUrl)
}).catch(err => {
console.log(err)
reject(err)
wx.hideLoading()
wx.showModal({
title: '提示',
content: '图片尺寸过大,请调整图片尺寸',
success(res) {
if (res.confirm) {
console.log('用户点击确定')
} else if (res.cancel) {
console.log('用户点击取消')
}
}
})
})
})
},
这么简单的功能,整不明白
Page({
upload: function() {
wx.chooseImage({
count: 3,
success: async res => {
const tempFilePaths = res.tempFilePaths;
for(let i = 0; i < tempFilePaths.length; i += 1) {
await this.compressImg(tempFilePaths[i]);
await this.imgCheck();
}
}
})
},
compressImg: async function () {
return null;
},
imgCheck: async function() {
const fs = wx.getFileSystemManager();
let base64 = fs.readFileSync();
return await wx.cloud.callFunction({
name: 'check',
data: {
base64
}
})
}
})