微信小程序保存二维码

 	<button class='enjoys' open-type="{{opensett}}" catchtap='sharesss'>
        <text class='iconfont icon-tupian tupian'>text>
        <view class='fengx'>保存二维码view>
    button>
  data: {
    opensett:""
  },
 sharesss: function (e) {
    console.log(e)
    let that = this;
    wx.getSetting({//检查小程序的授权状态
      success(res) {
        console.log(res)//授权后的回调
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({//调用 wx.authorize 弹出对话窗提醒用户是否要授权小程序得到他的授权
            scope: 'scope.writePhotosAlbum',
            success(t) {//这里是用户同意授权后的回调
              console.log("这里是用户同意授权后的回调", t)
              that.setData({
                opensett: ""//清掉button获取授权属性
              })
              that.getImage();
            },
            fail(err) {//这里是用户拒绝授权后的回调
              console.log("这里是用户拒绝授权后的回调", err);
              that.setData({
                opensett: "openSetting"//重新给button赋属性
              });
              if (err.errMsg == "authorize:fail:auth deny") {//如果用户拒绝打开授权接口,通过button按钮的open-type="openSetting"属性来调起授权窗口
                console.log("打开设置窗口");
                if (that.data.opensett == "openSetting"){
                  console.log("第二次进来",that.data.opensett);
                  wx.openSetting({
                    success(settingdata) {
                      console.log(settingdata)
                      if (settingdata.authSetting["scope.writePhotosAlbum"]) {
                        console.log("获取权限成功,再次点击图片保存到相册")
                        that.setData({
                          opensett: ""
                        });
                      } else {
                        console.log("获取权限失败")
                      }
                    }
                  });
                }
               
               
              }
            }
          })
        } else {//用户已经授权过了
          console.log("用户已经授权过了")
          that.setData({
            opensett: ""
          })
          that.getImage();
        }
      }
    })
  },
  getImage(){
    wx.downloadFile({
      url: imageUrl,
      success: function (res) {
        console.log(res);
        //图片保存到本地
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: function (data) {
            console.log("保存到相册", data);
            wx.showToast({
              title: '保存成功',
              icon: "none"
            })
          },
          fail: function (err) {
            console.log(err)
          }
        })
      }
    })
  },

你可能感兴趣的:(微信小程序)