微信小程序保存多圖到系統相冊

utils目錄下download.js 文件

//保存圖片到相冊
export const writePhotosAlbum = (successFun, failFun) => {
  wx.getSetting({
    success(res) {
      if (!res.authSetting['scope.writePhotosAlbum']) {
        wx.authorize({
          scope: 'scope.writePhotosAlbum',
          success: function () {
            successFun && successFun()
          },
          fail: function (res) {
            wx.hideLoading()
            wx.showModal({
              title: '提示',
              content: "小程序需要您的微信授權保存圖片,是否重新授權?",
              showCancel: true,
              cancelText: "否",
              confirmText: "是",
              success: function (res2) {
                if (res2.confirm) { //用戶點擊確定'
                  wx.openSetting({
                    success: (res3) => {
                      if (res3.authSetting['scope.writePhotosAlbum']) {
                        //已授權
                        successFun && successFun()
                      } else {
                        failFun && failFun()
                      }
                    }
                  })
                } else {
                  failFun && failFun()
                }
              }
            });
          }
        })
      } else {
        successFun && successFun()
      }
    }
  })
}

load.wxml

<view class="btn" bindtap="downLoadAll">下載多張圖片</view>

load.js

// pages/test/test.js
import { writePhotosAlbum } from '../../utils/download'
Page({

  /**
   * 頁面的初始數據
   */
  data: {
    list: [
      'https://timgs.top1buyer.com/admin/special/special_img_20190301160008479.jpg',
      'https://timgs.top1buyer.com/admin/special/special_img_20190301160013201.jpg',
      'https://timgs.top1buyer.com/admin/special/special_img_20190301160015969.jpg',
      'https://timgs.top1buyer.com/admin/special/special_img_20190301160025498.jpg',
      'https://timgs.top1buyer.com/admin/special/special_img_20190301160031519.jpg',
      'https://timgs.top1buyer.com/admin/special/special_img_20190301160042689.jpg',
      'https://timgs.top1buyer.com/admin/special/special_img_20190301160108243.jpg',
      'https://timgs.top1buyer.com/admin/special/special_img_20190301160111756.jpg',
      'https://timgs.top1buyer.com/admin/special/special_img_20190304160141454.jpg'
    ],
    loading: false
  },

  /**
   * 生命週期函數--監聽頁面加載
   */
  onLoad: function (options) {

  },
  downLoadAll() {   //物料多個下載
    // 下載圖片

      var _this = this
      // 獲取保存到相冊權限
      writePhotosAlbum(
        function success() {
          wx.showLoading({
            title: '加載中',
            mask: true
          })
          // 調用保存圖片promise隊列
          _this
            .queue(_this.data.list)
            .then(res => {
              wx.hideLoading()
              wx.showToast({
                title: '下載完成'
              })
            })
            .catch(err => {
              wx.hideLoading()
              console.log(err)
            })
        },
        function fail() {
          wx.showToast({
            title: '您拒絕了保存到相冊'
          })
        }
      )
  },
  // 隊列
  queue(urls) {
    let promise = Promise.resolve()
    urls.forEach((url, index) => {
      promise = promise.then(() => {
        return this.download(url)
      })
    })
    return promise
  },
  // 下載
  download(url) {
    return new Promise((resolve, reject) => {
      wx.downloadFile({
        url: url,
        success: function (res) {
          var temp = res.tempFilePath
          wx.saveImageToPhotosAlbum({
            filePath: temp,
            success: function (res) {
              resolve(res)
            },
            fail: function (err) {
              reject(res)
            }
          })
        },
        fail: function (err) {
          reject(err)
        }
      })
    })
  }
})

參考鏈接:https://www.jb51.net/article/157338.htm

github地址:https://github.com/sunnie1992/soul-weapp.git

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章