微信小程序,保存相册,开启用户授权友好提示

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

saveImg: function () {
        var that = this;
        if ((that.data.codePic == '') || (that.data.codePic == null)) {
            app.basic_dialog('图片加载失败');
            return false;
        }

        wx.getSetting({
            success: function(res) {
                if (res.authSetting["scope.writePhotosAlbum"] == false) {
                    app.confirm_dialog('授权提醒', '您还未授权保存相册,请授权', function(res) {
                        if (res.confirm == true) {
                            wx.openSetting({
                                success: function(res) {
                                    if (res.authSetting["scope.writePhotosAlbum"] == true) {
                                        // 保存相册
                                        that.saveImg();
                                    } else {
                                        app.toast_none('授权失败');
                                        that.setData({
                                            chat: false
                                        });
                                    }
                                }
                            });
                        }
                    })
                } else {
                    // 下载这个图片
                    wx.downloadFile({
                        url: that.data.codePic,
                        success: function (res) {
                            wx.saveImageToPhotosAlbum({
                                filePath: res.tempFilePath,
                                success: function (response) {
                                    if (response.errMsg == "saveImageToPhotosAlbum:ok") {
                                        app.toast_none('下载成功', function () {
                                            that.setData({
                                                chat: false
                                            });
                                        });
                                    }
                                }
                            });
                        },
                        fail: function() {
                            app.toast_none('下载资源失败');
                        }
                    });
                }
            }
        });
    },

 

 

 

第二版优化, 解决第一次点击,反应时间比较长的问题

var that = this;
        if ((that.data.codePic == '') || (that.data.codePic == null)) {
            app.basic_dialog('图片加载失败');
            return false;
        }
        if (that.data.loading) {
            return false;
        }
        that.data.loading = true;
        that.setData({
            loading: that.data.loading
        });

        // 判断用户是否授权
        wx.getSetting({
            success: function(res) {
                if (res.authSetting["scope.writePhotosAlbum"] == false) {
                    app.confirm_dialog('授权提醒', '您还未授权保存相册,请授权', function(res) {
                        if (res.confirm == true) {
                            wx.openSetting({
                                success: function(res) {
                                    if (res.authSetting["scope.writePhotosAlbum"] == true) {
                                        // 保存相册
                                        that.saveImg();
                                    } else {
                                        app.toast_none('授权失败');
                                        that.setData({
                                            chat: false
                                        });
                                    }
                                }
                            });
                        }
                        that.data.loading = false;
                        that.setData({
                            loading: that.data.loading
                        });
                    })
                } else {
                    // 下载这个图片
                    wx.downloadFile({
                        url: that.data.codePic,
                        success: function (res) {
                            wx.saveImageToPhotosAlbum({
                                filePath: res.tempFilePath,
                                success: function (response) {
                                    if (response.errMsg == "saveImageToPhotosAlbum:ok") {
                                        app.toast_none('下载成功', function () {
                                            that.setData({
                                                chat: false
                                            });
                                        });
                                    }
                                }
                            });
                            that.data.loading = false;
                            that.setData({
                                loading: that.data.loading
                            });
                        },
                        fail: function() {
                            app.toast_none('下载资源失败');
                            that.data.loading = false;
                            that.setData({
                                loading: that.data.loading
                            });
                        }
                    });
                }
            }
        });

 

 

转载于:https://my.oschina.net/u/575762/blog/1819287

你可能感兴趣的:(微信小程序,保存相册,开启用户授权友好提示)