uni-app 点击下载网络图片

2020-11-18  本文已影响0人  LingSun
<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>
上一篇 下一篇

猜你喜欢

热点阅读