實(shí)現(xiàn)功能:點(diǎn)擊‘保存圖片’可以將商品的所有圖片以及商品的海報(bào)圖片保存到相冊(cè)中文章來源:http://www.zghlxwxcb.cn/news/detail-732458.html
由于downloadFile一次只能下載一張圖片,因此需要依次遍歷圖片數(shù)組,將圖片逐一保存文章來源地址http://www.zghlxwxcb.cn/news/detail-732458.html
// 一鍵保存所有圖片
oneSaveImg() {
// 首先判斷用戶是否登錄
if (this.$isLogin()) {
uni.showLoading({
title:"保存中"
})
//遍歷接口返回值 將返回值push到數(shù)組中
for(let i = 0; i < this.goodsInfo.gallery_list.length;i++){
//防止用戶多次點(diǎn)擊,判斷原數(shù)組中是否存在圖片,不存在則push
if(this.shopArr.length>=this.goodsInfo.gallery_list.length) break;
this.shopArr.push(this.goodsInfo.gallery_list[i].img_url)
}
let price = this.goodsInfo.goods_price || this.goodsInfo.shop_price
this.$store.dispatch('setGoodsShare', { //請(qǐng)求接口 獲取海報(bào)圖片
goods_id: this.goods_id,
price: price,
share_type: this.goodsInfo.is_distribution,
platform: uni.getStorageSync('platform'),
shop_id: uni.getStorageSync('shop_id')
}).then(res => {
if (res.status == 'success') {
this.shareImg = res.data,
this.shopArr.push(this.shareImg)
let url = res.data
// 將數(shù)組中的圖片鏈接遍歷 依次下載
for(let i=0;i<this.shopArr.length;i++){
uni.downloadFile({
url:this.shopArr[i],
success: res => {
uni.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success: res => {
uni.hideLoading()
if(i == this.shopArr.length-1){
uni.hideLoading()
uni.showToast({
title:"圖片保存成功"
})
this.shopArr = []
}
},
fail: res => {
if (res.errMsg === 'saveImageToPhotosAlbum:fail:auth denied' || res.errMsg === 'saveImageToPhotosAlbum:fail auth deny') {
uni.showModal({
title: '提示',
content: '需要您授權(quán)保存相冊(cè)',
showCancel: false,
success: modalSuccess => {
uni.openSetting({
success: settingdata => {
console.log('settingdata', settingdata);
if (settingdata.authSetting['scope.writePhotosAlbum']) {
uni.showModal({
title: '提示',
content: '獲取權(quán)限成功,再次點(diǎn)擊圖片即可保存',
showCancel: false
});
}else{
uni.showModal({
title: '提示',
content: '獲取權(quán)限失敗,將無法保存到相冊(cè)哦~',
showCancel: false,
})
}
},
fail:failData=>{
uni.showToast({
title: '保存失??!',
icon: 'none'
});
console.log("授權(quán)失敗",failData)
},
});
}
});
}
}
});
},fail:res=>{
console.log(res)
}
});
}
}
})
} else {
uni.showModal({
content: "您需要登錄會(huì)員!",
success: (res) => {
if (res.confirm) {
uni.navigateTo({
url: '/pages/login/login?delta=1'
})
}
}
})
}
},
作者:Reve2333
鏈接:https://juejin.cn/post/6844904122869612557
來源:稀土掘金
著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請(qǐng)聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請(qǐng)注明出處。
到了這里,關(guān)于微信小程序?qū)崿F(xiàn)一鍵保存多張圖片的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!