微信小程序点击图片保存到相册

小程序实现点击图片保存到相册

温馨提示:小程序图片在本地保存成功后放到线上失效,应该是没有设置合法的文件下载域名

微信小程序点击图片保存到相册_第1张图片

 // 下载事件函数
  bindImage: function (e) {
    var url = e.currentTarget.dataset.img;
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.writePhotosAlbum']) {
          wx.downloadFile({
            url: url,
            success: function (res) {
              // 图片保存到本地
              wx.saveImageToPhotosAlbum({
                filePath: res.tempFilePath,
                success(res) {
                  console.log(res);
                  wx.showToast({
                    title: '保存成功',
                    icon: 'success',
                    duration: 500
                  })
                },
                fail() {
                  wx.showToast({
                    title: "保存取消",
                    icon: "none"
                  });
                }
              })
            }
          })
        } else {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success(res) {
              if (res.errMsg === 'authorize:ok') {
                wx.downloadFile({
                  url: url,
                  success: function (res) {
                    wx.saveImageToPhotosAlbum({
                      filePath: res.tempFilePath,
                      success(res) {
                        wx.showToast({
                          title: '保存成功',
                          icon: 'success',
                          duration: 500
                        })
                      },
                      fail() {
                        wx.showToast({
                          title: "保存取消",
                          icon: "none"
                        });
                      }
                    })
                  }
                })
              }
            },
            fail() {
              wx.showToast({
                title: '请打开保存相册权限,再点击保存相册分享',
                icon: 'none',
                duration: 500
              })
            }
          })
        }
      }
    })
  },

你可能感兴趣的:(微信小程序,前端,js,微信小程序,javascript,前端)