微信小程序,保存相冊,開啓用戶受權友好提示

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('下載資源失敗');
                        }
                    });
                }
            }
        });
    },

 

 

 

第二版優化, 解決第一次點擊,反應時間比較長的問題javascript

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
                            });
                        }
                    });
                }
            }
        });
相關文章
相關標籤/搜索