对方法做个备份
方法一 下载及解压代码
第一个参数为下载地址,将下载地址以数组的方式传入,可以下载多个文件。
第二个参数为想要将下载并解压好的文件保存到的本地路径。
第三个参数为是不是微信云端路径,是的话为true;
const wxP = require('../utils/util.js');
Download_zip: function (load_path = [], target_path = '', if_cloud = false) {
const fs = wx.getFileSystemManager();
let length = load_path.length;
const unzip_API = function (res_path, target_path) {
wxP.saveFile({
tempFilePath: res_path,
}).then(res => {
let FilePath = res.savedFilePath
fs.unzip({
zipFilePath: FilePath,
targetPath: target_path,
success(res) {
console.log('fs.unzipe sucess')
if(length == 1) {
toast.sucess();
}
else {
length = length - 1;
wx.showToast({
title: '资源正在下载',
icon: 'loading',
mask: true,
duration: 60000
})
}
},
fail(err) {
}
})
}).catch(err => {
console.log('解压函数 unzip_API 失败',err);
})
}
let load_API = function (path) {
return new Promise((resolve, error) => {
if(if_cloud){
toast.loading();
wxP.clouddownloadFile({
fileID: path,
}).then(res => {
resolve(res);
}).catch(err => {
toast.hide().then(() => {toast.fail();})
error('Loadfn cloud 失败',err)
})}
else{
toast.loading();
wxP.downloadFile({
url: path,
}).then(res => {
resolve(res);
}).catch(err => {
toast.hide().then(() => {toast.fail();})
error('Loadfn 失败',err)
})}
})
}
const toast = {
loading: function () {
wx.showToast({
title: '正在下载中...',
icon: 'loading',
mask: true,
duration: 1000,
})
},
hide: function () {
return new Promise ((resolve,error) => {
wx.hideToast({
success: (res) => {resolve(res)},
fail: (err) => {error(err)}
})
})
},
sucess: function () {
wx.showToast({
title: '下载完成',
icon: 'success',
duration: 2000
})
},
fail: function () {
wx.showToast({
title: '请检查您的网络',
icon: 'error',
duration: 2000
})
}
}
const createfiles = function (target_path) {
fs.access({
path: target_path,
success(res) {
},
fail(err) {
fs.mkdir({
dirPath: target_path,
recursive: true,//很关键
success(res) {
},
fail(err) {
}
})
}
})
}
const clearfile = function () {
wx.getSavedFileList({ // 获取文件列表
success (res) {
res.fileList.forEach((val, key) => { // 遍历文件列表里的数据
// 删除存储的垃圾数据
wx.removeSavedFile({
filePath: val.filePath
});
})
}
})
}
let load_number = -1;
const Loading = function () {
return new Promise((resolve, error) => {
load_number++;
if(load_path[load_number]) {
resolve();
}
else {
error();
}
}).then(() => {
return load_API(load_path[load_number]);
}).then((res) => {
unzip_API(res.tempFilePath, target_path)
return Loading();
}).catch((err) => {
wx.hideToast();
fs.readdir({
dirPath: target_path,
success(res) {
// if(if_cloud)
// App.globalData.cloud_ifload = true;
// else
// App.globalData.Gif_ifload = true;
},
fail(err) {
}
})
})
}
const if_haddown = function () {
return new Promise((resolve, error) => {
fs.readdir({
dirPath: target_path,
recursive: true,
success: (res) => {if(res.files.length > 0) resolve(true); else error(false);},
fail: (err) => {error(false);}
})
})
}
clearfile();
createfiles(target_path);
if_haddown().then((res) => {console.log(target_path + '存在')}).catch((err) => {Loading()});
}
wxP是指微信API promise化
function wxcloudPromise(functionName, params) {
return new Promise(function(resolve, reject) {
wx.cloud[functionName]({
...params,
success: resolve,
fail: reject
})
})
}
function wxPromise(functionName, params) {
return new Promise(function(resolve, reject) {
wx[functionName]({
...params,
success: resolve,
fail: reject
})
})
}
function clouddownloadFile(params = {}) {
return wxcloudPromise('downloadFile', params);
}
function downloadFile(params = {}) {
return wxPromise('downloadFile', params);
}
function saveFile(params = {}) {
return wxPromise('saveFile', params);
}
function getFileSystemManager(params = {}) {
return wxPromise('getFileSystemManager', params);
}
function getStorage(params = {}) {
return wxPromise('getStorage', params);
}
// function (params = {}) {
// return wxPromise('', params);
// }
module.exports = {
clouddownloadFile,
downloadFile,
saveFile,
getFileSystemManager,
getStorage,
}