I found how to save multiple pictures in a small program on the Internet. The method is a for loop, and the callback function is used in conjunction with the callback function. I didn't understand the essence, so I changed it to a different method. The mobile phone test can save all the pictures in turn.
Ideas:
Use wx.downloadFile() to get the temporary path of the picture, and wx.saveImageToPhotosAlbum() to save the changed temporary path to the phone album. If the save succeeds, i++ calls dow_temp(i++), if it fails, continues dow_temp(i) to execute it again.
Code:
//The sharing function copies the copy, saves the picture to the album, and opens the sharing floating layer after success
share:function(e){
var that = this;
var data = that.data.goods.goodsPhotoUrls;
var sharetext = e.currentTarget.dataset.text;
that.setData({
sharetext: sharetext
})
wx.showLoading({
title:'Image downloading...',
})
var all_n = data.length;
that.dow_temp(0);
},
//Download single content
dow_temp: function (i, callback) {
var that = this;
var data = that.data.goods.goodsPhotoUrls;
var all_n = data.length;
if (i < all_n) {
wx.authorize({
scope: 'scope.writePhotosAlbum',
success() {
// The user has agreed to the authorization of the Mini Program to use the album
const downloadTask = wx.downloadFile({
url: data[i],
success: function (res) {
var temp = res.tempFilePath;
console.info("temporary path", temp)
wx.saveImageToPhotosAlbum({
filePath: temp,
success: function () {
console.info('第',(i + 1),'Zhang save successfully');
that.dow_temp(i+1);
//Turn on sharing floating layer
if (i == all_n-1) {
wx.setClipboardData({
data: that.data.sharetext,
success(res) {
that.setData({
shareMaskStatus: 'show'
})
}
})
}
},
fail: function () {
console.info('第', (i + 1),'Zhang save failed');
that.dow_temp(i);
}
})
},
fail: function (res) {
wx.showToast({
icon: 'none',
title:'Failed to obtain the temporary path of the picture',
})
}
})
},
fail: function () {
wx.showToast({
icon: 'none',
title:'Failed to obtain authorization',
})
}
})
}
},