直接上代码
/*
1、需要生成一个小程序码 或者 后端帮忙处理返回一个图片地址
2、将其下载到本地
3、将链接变为对象 执行下载
*/
视图页面
<view class="btnShareImage" @click.stop="saveImage()">保存分享二维码</view>
// js methods
// 请求生成二维码并 下载
saveImage () {
let that = this;
uni.showLoading({
title: '正在保存中'
})
let data = {
uid: that.uid,
merId: that.merId,
deptId: that.deptId,
id: that.Info.id
}
getGiftImageCode(data).then(res=> {
this.posterImage = res.data.url;
console.log(this.posterImage);
// this.savePosterPath()
this.saveNetImageToLocal(res.data.url);
uni.hideLoading();
}).catch(err=> {
console.log(err);
uni.hideLoading();
})
},
saveNetImageToLocal(url){
let that = this
uni.downloadFile({
url:url,
success:(res)=>{
if (res.statusCode === 200) {
that.posterImage = res.tempFilePath; //地址
that.savePosterPath();
} else {
uni.showToast({
title: '网络错误',
});
}
}
});
},
/*
* 保存到手机相册
*/
// #ifdef MP
savePosterPath: function() {
let that = this;
uni.getSetting({
success(res) {
if (!res.authSetting['scope.writePhotosAlbum']) {
uni.authorize({
scope: 'scope.writePhotosAlbum',
success() {
uni.saveImageToPhotosAlbum({
filePath: that.posterImage,
success: function(res) {
that.$util.Tips({
title: '保存成功',
icon: 'success'
});
},
fail: function(res) {
console.log(res)
that.$util.Tips({
title: '保存失败'
});
}
})
}
})
} else {
uni.saveImageToPhotosAlbum({
filePath: that.posterImage,
success: function(res) {
that.$util.Tips({
title: '保存成功',
icon: 'success'
});
},
fail: function(res) {
console.log(res);
that.$util.Tips({
title: '保存失败'
});
},
})
}
}
})
},
// #endif
// #ifdef APP-PLUS
savePosterPath(){
let that = this
uni.saveImageToPhotosAlbum({
filePath: that.posterImage,
success: function(res) {
that.$util.Tips({
title: '保存成功',
icon: 'success'
});
},
fail: function(res) {
that.$util.Tips({
title: '保存失败'
});
},
})
}
// #endif