微信小程序保存图片到手机相册(拒绝授权可重新唤起)

微信小程序保存图片到手机相册

一次性保存多张图片:https://blog.csdn.net/qq_43764578/article/details/103641190

.wxml

<image src="{{images}}"></image>
<button wx:if="{{!isAuthSavePhoto}}" bindtap="onSaveToPhone" class="btn button-hover" >
  保存图片到手机
</button>
<button wx:else bind:tap="showModal"  class="btn button-hover" >            
  保存图片到手机
</button>

.js

Page({
  data: {
    images: "https://img2.woyaogexing.com/2020/03/06/1be01938694b4427a216283f3e19eff2.jpeg",
    isAuthSavePhoto: false,
  },
  // 保存图片到手机
  onSaveToPhone() {
    this.getSetting().then((res) => {
      if (!res.authSetting['scope.writePhotosAlbum']) {
        this.authorize().then(() => {
          this.savedownloadFile(this.data.images)
          this.setData({
            isAuthSavePhoto: false
          })
        }).catch(() => {
          wx.showToast({
            title: '您拒绝了授权',
            icon: 'none',
            duration: 1500
          })
          this.setData({
            isAuthSavePhoto: true
          })
        })
      } else {
        this.savedownloadFile(this.data.images)
      }
    })
  },
  onOpenSetting() {
    wx.openSetting({
      success: (res) => {
        console.log(res.authSetting)
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.showToast({
            title: '您未授权',
            icon: 'none',
            duration: 1500
          })
          this.setData({
            isAuthSavePhoto: true
          })

        } else {
          this.setData({
            isAuthSavePhoto: false
          })
          this.onSaveToPhone()
        }

      }
    })

  },
  getSetting() {
    return new Promise((resolve, reject) => {
      wx.getSetting({
        success: res => {
          resolve(res)
        }
      })
    })
  },
  authorize() {
    return new Promise((resolve, reject) => {
      wx.authorize({
        scope: 'scope.writePhotosAlbum',
        success: () => {
          resolve()
        },
        fail: res => {
          console.log('拒绝授权')
          reject()
        }
      })
    })
  },
  savedownloadFile(img) {
    this.downLoadFile(img).then((res) => {
      return this.saveImageToPhotosAlbum(res.tempFilePath)
    }).then(() => {
    })
  },
  downLoadFile(img) {
    return new Promise((resolve, reject) => {
      wx.downloadFile({
        url: img,
        success: (res) => {
          console.log('downloadfile', res)
          resolve(res)
        }
      })
    })
  },
  saveImageToPhotosAlbum(saveUrl) {
    return new Promise((resolve, reject) => {
      wx.saveImageToPhotosAlbum({
        filePath: saveUrl,
        success: (res) => {
          wx.showToast({
            title: '保存成功',
            duration: 1000,
          })
          resolve()
        }
      })
    })
  },
  showModal() {
    wx.showModal({
      title: '检测到您没有打开保存到相册的权限,是否前往设置打开?',
      success: (res) => {
        if (res.confirm) {
          console.log('用户点击确定')
          this.onOpenSetting()        
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }
})

有什么问题欢迎评论留言,我会及时回复你的

原创文章 75 获赞 87 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43764578/article/details/105593345