小程序授权保存到相册完整操作

<button open-type="openSetting" bindopensetting="handleSetting" wx:if="{{isPic}}">授权保存</button> 
        <button bindtap="saveImg" wx:else>保存图片</button>

Page({
    data: {
        isPic: false
    },
    onLoad: function (options) {
        var that = this;
        var scene = decodeURIComponent(options.scene);
        

    },
    getConfirm(){

    },


    saveImg(){
       let that = this;
       console.log(1)
       wx.getSetting({
           success: function(res){
               //不存在相册授权
               if (!res.authSetting['scope.writePhotosAlbum']){
                   wx.authorize({
                       scope: 'scope.writePhotosAlbum',
                       success: function(){
                           that.savePhoto();
                           that.setData({
                               isPic: false
                           })
                       },
                       fail: function(err){
                           that.setData({
                               isPic: true
                           })
                       }
                   })
               }else{
                   that.savePhoto(); 
               }
           }
       })
       

    },
    handleSetting(e){
        var that = this;
        if (!e.detail.authSetting['scope.writePhotosAlbum']){
            wx.showModal({
                title: '警告',
                content: '不授权无法保存',
                showCancel: false
            })
            that.setData({
                isPic: true
            })
        }else{
            wx.showToast({
                title: '保存成功'
            })
            that.setData({
                isPic: false
            })
        }
    },
    savePhoto(){
        let urlStr = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1534762319109&di=8974aafb91846a92be2eee8db6d90399&imgtype=0&src=http%3A%2F%2Fe.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2Fb151f8198618367a2e8a46ee23738bd4b31ce586.jpg';
        var that = this;
        wx.downloadFile({
            url: urlStr,
            success: function (res) {
                wx.saveImageToPhotosAlbum({
                    filePath: res.tempFilePath,
                    success: function (data) {
                        wx.showToast({
                            title: '保存成功',
                            icon: 'success',
                            duration: 1500
                        })

                    }
                })
            }

        })
    },
    bindGetUserInfo(e){
        if(!e.detail.userInfo){
           console.log('用户点击了取消')
        }else{
            console.log(e.detail.userInfo)
        } 
    },
    onShareAppMessage: function () {
        return {
            title: '来自休休漠漠',
            desc: '这是一个测试分享',
            path: '/pages/test/test',
            success: function(res){
                console.log(res)
            },
            fail: function(res){
                console.log(res)
            }
        }
    }
})

此方法是用按钮的形式获得授权,wx.openSetting()方法将弃用

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值