小程序点击按钮保存图片

<view class="qr-code">
   <image src="../img/drmxcxbg.png"></image>
   <view class="qr-desc">用微信扫描二维码,查看详情</view>
</view>
    
<view class="share-li" catchtap="saveCode">
   <image src="../../img/shareicon2.png"></image>
   <view>保存二维码</view>
</view>
// 点击保存二维码
    saveCode(){
    
    
      var url = '../img/drmxcxbg.png';
      this.isCheckSave(url);
    },
    isCheckSave(url){
    
    
      var _this = this;
      wx.getSetting().then(res=>{
    
    
        console.log(res);
        if (!res.authSetting['scope.writePhotosAlbum']) {
    
     //未授权的话发起授权
          wx.authorize({
    
    
            scope: 'scope.writePhotosAlbum',
            success: () => {
    
     //用户允许授权,保存到相册
              _this.saveImg(url);
            },
            fail: () => {
    
     //用户拒绝授权,然后就引导授权(这里的话如果用户拒绝,不会立马弹出引导授权界面,坑就是上边所说的官网原因)
              wx.showModal({
    
    
                content: '请允许打开设置页授权',
                success: function (res2) {
    
    
                  if (res2.confirm) {
    
    
                    wx.openSetting({
    
    
                      success: () => {
    
    
                        wx.authorize({
    
    
                          scope: 'scope.writePhotosAlbum',
                          succes: () => {
    
     //授权成功,保存图片
                            _this.saveImg(url);
                          }
                        })
                      }
                    })
                  }
                }
              })

            }
          })
        }else{
    
    
          _this.saveImg(url);
        }
      })
    },
    /* 保存图片 */
    saveImg(url) {
    
    
      wx.getImageInfo({
    
    
        src: url,//图片链接
        success(path) {
    
    
          console.log(path);
          wx.saveImageToPhotosAlbum({
    
    
            filePath: path.path,
            success(){
    
    
              wx.showToast({
    
    
                title: '已保存至相册',
              })
            }
          })
        }
      })
    },

猜你喜欢

转载自blog.csdn.net/BubbleABC/article/details/129500010