發(fā)表日期:2019-11 文章編輯:小燈 瀏覽次數(shù):5253
之前寫個一個版本的[多圖下載],重構(gòu)進行了代碼升級讓代碼更加簡介
分為兩步:
第一獲取保存到相冊權(quán)限
第二下載圖片
主要涉及兩個文件,index.js 和download.js
另外現(xiàn)在如果有圖片下載失敗也彈出下載完成后續(xù)需要優(yōu)化
/**
* 獲取相冊權(quán)限
*/
export function wxSaveAuth() {
return new Promise((resolve, reject) => {
wx.getSetting({
success(res) {
if (!res.authSetting['scope.writePhotosAlbum']) {
// 如果沒有寫入權(quán)限,則獲取寫入相冊權(quán)限
wx.authorize({
scope: 'scope.writePhotosAlbum',
success() {
resolve()
},
fail(err) {
reject(err)
// 用戶拒絕授權(quán)
wx.showModal({
content: '檢測到您沒打開捷買士的相冊權(quán)限,是否去設(shè)置打開?',
confirmText: '確認',
cancelText: '取消',
success(res) {
if (res.confirm) {
wx.openSetting({
success: res => {}
})
}
}
})
}
})
} else {
resolve()
}
},
fail(e) {
reject(e)
}
})
})
}
/**
* 多文件下載并且保存
* @param {Array} urls 網(wǎng)絡(luò)圖片數(shù)組
*/
export function downloadImgs(urls) {
wx.showLoading({
title: '圖片下載中',
mask: true
})
const imageList = []
// 循環(huán)數(shù)組
for (let i = 0; i < urls.length; i++) {
imageList.push(getTempPath(urls[i]))
}
const loadTask = []
let index = 0
while (index < imageList.length) {
loadTask.push(
new Promise((resolve, reject) => {
// 將數(shù)據(jù)分割成多個promise數(shù)組
Promise.all(imageList.slice(index, (index += 8)))
.then(res => {
resolve(res)
})
.catch(err => {
reject(err)
})
})
)
}
// Promise.all 所有圖片下載完成后彈出
Promise.all(loadTask)
.then(res => {
wx.showToast({
title: '下載完成',
duration: 3000
})
})
.catch(err => {
wx.showToast({
title: `下載完成`,
icon: 'none',
duration: 3000
})
})
}
/**
*
* @param {String} url 單張網(wǎng)絡(luò)圖片
*/
//下載內(nèi)容,臨時文件路徑
function getTempPath(url) {
return new Promise((resolve, reject) => {
wx.downloadFile({
url: url,
success: function(res) {
var temp = res.tempFilePath
wx.saveImageToPhotosAlbum({
filePath: temp,
success: function(res) {
return resolve(res)
},
fail: function(err) {
reject(url + JSON.stringify(err))
}
})
},
fail: function(err) {
reject(url + JSON.stringify(err))
}
})
})
}
// pages/save-imgs/index.js
import { wxSaveAuth, downloadImgs } from '../../utils/download'
Page({
/**
* 頁面的初始數(shù)據(jù)
*/
data: {
urls: [
'https://avatars0.githubusercontent.com/u/35954879?s=120&v=4',
'https://avatars0.githubusercontent.com/u/35954879?s=120&v=4',
'https://avatars0.githubusercontent.com/u/35954879?s=120&v=4',
'https://avatars0.githubusercontent.com/u/35954879?s=120&v=4',
'https://avatars0.githubusercontent.com/u/35954879?s=120&v=4',
'https://avatars0.githubusercontent.com/u/35954879?s=120&v=4',
'https://avatars0.githubusercontent.com/u/35954879?s=120&v=4',
'https://avatars0.githubusercontent.com/u/35954879?s=120&v=4'
]
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面加載
*/
onLoad: function(options) {},
download() {
// 獲取保存到相冊權(quán)限
wxSaveAuth().then(res => {
// 保存多張圖片到相冊
downloadImgs(this.data.urls)
})
},
})
git clone https://github.com/sunnie1992/sol-weapp.git
掃描添加下方的微信并備注 Sol 加交流群,交流學(xué)習(xí),及時獲取代碼最新動態(tài)。
如果對你有幫助送我一顆小星星(づ ̄3 ̄)づ╭?~
日期:2019-11 瀏覽次數(shù):5624
日期:2019-11 瀏覽次數(shù):12085
日期:2019-11 瀏覽次數(shù):4445
日期:2019-11 瀏覽次數(shù):5486
日期:2019-11 瀏覽次數(shù):5341
日期:2019-11 瀏覽次數(shù):7318
日期:2019-11 瀏覽次數(shù):5252
日期:2019-11 瀏覽次數(shù):15874
日期:2019-11 瀏覽次數(shù):4828
日期:2019-11 瀏覽次數(shù):6618
日期:2019-11 瀏覽次數(shù):5496
日期:2019-11 瀏覽次數(shù):4668
日期:2019-11 瀏覽次數(shù):10912
日期:2019-11 瀏覽次數(shù):8439
日期:2019-11 瀏覽次數(shù):5177
日期:2019-11 瀏覽次數(shù):4417
日期:2019-11 瀏覽次數(shù):9077
日期:2019-11 瀏覽次數(shù):4755
日期:2019-11 瀏覽次數(shù):4955
日期:2019-11 瀏覽次數(shù):4962
日期:2019-11 瀏覽次數(shù):4612
日期:2019-11 瀏覽次數(shù):5121
日期:2019-11 瀏覽次數(shù):10401
日期:2019-11 瀏覽次數(shù):5557
日期:2019-11 瀏覽次數(shù):5556
日期:2019-11 瀏覽次數(shù):5000
日期:2019-11 瀏覽次數(shù):12468
日期:2019-11 瀏覽次數(shù):7463
日期:2019-11 瀏覽次數(shù):8026
日期:2019-11 瀏覽次數(shù):4947
Copyright ? 2013-2018 Tadeng NetWork Technology Co., LTD. All Rights Reserved.