微信小程序,长按图片保存图片到相册功能

    SaveImg: function (file) {
        var _self = this;
        wx.showModal({content: '扫码券:保存到相册?',
            success(res){
                if (res.confirm) {
                    _self.SaveFile(file);
                }
            }
        });
    },
    SaveFile: function (file) {
        var _self = this;
        if(file.substring(0,4)=='http'&&file.indexOf("http://tmp/")==-1){
            wx.downloadFile({url:file,success: function(res) {
                    _self.SaveFile(res.tempFilePath);
                }});
        }else{
            wx.getSetting({
                success(res) {
                    if (!res.authSetting['scope.writePhotosAlbum']) {
                        wx.authorize({scope: 'scope.writePhotosAlbum',
                            success() {
                                _self.SavePath(file);
                            }
                        });
                    }else{
                        _self.SavePath(file);
                    }
                }
            });
        }

    },
    SavePath:function(file){
        var _self = this;
        wx.saveImageToPhotosAlbum({filePath: file,
            success(res) {
                _self.alert('扫码券:图片已保存到相册,赶紧去分享一下吧~');
            },
            fail(res) {
                wx.showToast({title: '扫码券:保存失败',icon: 'none',duration: 1000});
            }
        });
    }

扫码券微信小程序:

posted @ 2020-09-10 14:54  有天  阅读(1000)  评论(0)    收藏  举报