wx.downloadFile 下载视频 报错,进入 fail 回调,回掉参数:{“errMsg”:“downloadFile:fail 网络连接已中断。”}
而在fail 回掉里面我立即调用了 wx.getNetworkType 返回网络状态非 none。该bug非必现,但是在线上已经有很多用户反馈了,而且都是ios真机下发现的,麻烦官方看看这块是否有漏洞。
var app={getFileInfo(filePath){
var ctx = this;
return new Promise((resolve, reject) => {
wx.getFileInfo({
filePath,
complete( res ){
res.errMsg == ‘getFileInfo:ok’ ? resolve(res) : reject(res);
}
})
})
},
getNetworkType(){
return new Promise((resolve, reject) => {
wx.getNetworkType({
complete(res) {
resolve(res);
}
})
})
},
downloadFile( url ){
return new Promise((resolve, reject) => {
wx.downloadFile({
url,
success(res) {
resolve(res);
},
fail(res) {
reject(res);
}
})
})
}
}
//下面是下载模块
//var app = getApp();上面的方法就放在app上,这里简单模拟就直接定义了app如上代码。
var Promise = require(’…/…/…/…/utils/lib/es6-promise.min.js’);
var uploadVideoCache = app.globalData.uploadVideoCache;
var cacheTime = 3*60*1000;//缓存时间
var util = {
getLocalUrl(url) {
var cache = uploadVideoCache;
var key = encodeURIComponent(url);
var context = this;
return new Promise((resolve, reject) => {
/*return setTimeout(()=>{
resolve({ code: 200, url: url });
});*/
if (cache[key] && Date.now() - cache[key].time <= cacheTime) {
console.log(‘有文件缓存’ + cache[key].url);
resolve({ code: 200, url: cache[key].url });
} else {
console.log(‘开始下载:’+url);
context.downFile(url, key, cache, resolve);
}
});
},
downFile(url, key, cache, resolve) {
app.downloadFile(url).then(res => {
console.log([‘下载成功’, ‘原:’+url, ‘下载后:’+res.tempFilePath]);
cache[key] = { url: res.tempFilePath,time:Date.now()};
resolve({ code: 200, url: res.tempFilePath });
}, err => {
console.log([‘downloadFile_Error’,url,err]);
app.getNetworkType().then(res=>{
console.log([‘下载失败,网络:’, url, res.networkType]);
resolve({ code: res.networkType==‘none’?500:201, url: url });
res.networkType != ‘none’ && app.errorLog({//这里是给服务器发送错误日志的,目前服务端收到了很多这个错误
url: ‘downloadFile_Error’,
code: 30201,
userId: app.globalData.userId || ‘’,
serverData: JSON.stringify(err).substr(0, 500)
});
});
}).catch(err=>{
app.getNetworkType().then(res => {
console.log([‘下载失败catch,网络:’ + res.networkType, url, err]);
resolve({ code: res.networkType == ‘none’ ? 500 : 202, url: url });
res.networkType != ‘none’ && app.errorLog({
url: ‘downloadFile_Error’,
code: 30202,
userId: app.globalData.userId || ‘’,
serverData: JSON.stringify(err).substr(0, 500)
});
});
});
}
}// end util
现在的情况是进去页面下载第一个视频,开始播放,然后下载后一个视频,调用方法 util.getLocalUrl();
视频播放完成自动切换到下一个视频,然后又自动下载下下一个视频。
发现的问题是:downloadFile 下载失败, 微信接口返回错误信息为:{“errMsg”:“downloadFile:fail 网络连接已中断。”}