uni-app 点击下载网络图片

<template>
<view class="home_bottom_img_box" @click="saveImgToLocal">
    <image class="home_bottom_img" src="http://wework.qpic.cn/bizmail/k5Sr24cN7lysezOibTP0lbo50oXNsG0YOTlPHpApPBe7jz2ibotBUTYA/100"></image>
</view>
</template>
<script>
export default {
methods: {
    saveImgToLocal() {
                //获取相册授权
                const _self = this
                uni.getSetting({
                    success(res) {
                        console.log(res)
                        if (!res.authSetting['scope.writePhotosAlbum']) {
                            uni.authorize({
                                scope: 'scope.writePhotosAlbum',
                                success() {
                                    //这里是用户同意授权后的回调
                                    _self.saveImgToLocal()
                                },
                                fail() {//这里是用户拒绝授权后的回调
                                    _self.openSettingBtnHidden=false
                                }
                            })
                        } else {//用户已经授权过了
                            _self.saveImgToLocal()
                        }
                    }
                })
            },
            saveImgToLocal:function(e){   
                const _self = this   
                uni.showModal({
                    title: '提示',
                    content: '确定保存到相册吗',
                    success: function (res) {
                        if (res.confirm) {
                            uni.downloadFile({
                                url: "http://wework.qpic.cn/bizmail/k5Sr24cN7lysezOibTP0lbo50oXNsG0YOTlPHpApPBe7jz2ibotBUTYA/100",//图片地址
                                success: (res) =>{
                                        console.log('downloadFile success',res)
                                    if (res.statusCode === 200){
                                        console.log('downloadFile res.statusCode === 200',res)
                                        uni.saveImageToPhotosAlbum({
                                            filePath: res.tempFilePath,
                                            success: function() {
                                                uni.showToast({
                                                    title: "保存成功",
                                                    icon: "none"
                                                })
                                            },
                                            fail: function() {
                                                uni.showToast({
                                                    title: "保存失败",
                                                    icon: "none"
                                                })
                                            }
                                        })
                                    } else {
                                        console.log('downloadFile res.statusCode !== 200',res)
                                    }
                                }
                            })
                        } else if (res.cancel) {    
                        }
                    }
                })
            }
    }
}
</script>
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容