Commit c5001d21 by yixinjj

修复网络异常时中断文件上传轮询的BUG及优化

1 parent 7d9b6846
...@@ -44,12 +44,11 @@ export default { ...@@ -44,12 +44,11 @@ export default {
await this.uploadSuccess(recordId, filePathOnServer, filePathOnClient, fileName) await this.uploadSuccess(recordId, filePathOnServer, filePathOnClient, fileName)
} else { } else {
this.reportError(recordId, fileName, data) this.reportError(recordId, fileName, data)
// this.$sendSentry(`上传失败:文件名-${fileName} 错误码-${data.oss.res.status},失败原因-${JSON.stringify(data.oss.res)}`);
} }
} catch (e) { } catch (e) {
this.reportError(recordId, fileName, e) this.reportError(recordId, fileName, e)
} finally { } finally {
console.log('本次任务完成,开启下一次上传任务!'); console.log('本次任务完成,开启下一次上传任务!')
this.polling() this.polling()
} }
}, },
...@@ -58,7 +57,7 @@ export default { ...@@ -58,7 +57,7 @@ export default {
*/ */
reportError(recordId, filePath, res) { reportError(recordId, filePath, res) {
const { customerName, phoneNumber, carModel, salesName } = uni.$recordManager.getRecordById(recordId) || {} const { customerName, phoneNumber, carModel, salesName } = uni.$recordManager.getRecordById(recordId) || {}
const { name } = uni.$storage.get('userInfo') const { name } = uni.$storage.get('userInfo') || {}
this.$sendSentry(`上传失败:文件-${filePath} 手机号-${phoneNumber} 姓名-${customerName} 销售-${salesName} 车型-${carModel} 员工-${name}失败原因-${JSON.stringify(res)}`) this.$sendSentry(`上传失败:文件-${filePath} 手机号-${phoneNumber} 姓名-${customerName} 销售-${salesName} 车型-${carModel} 员工-${name}失败原因-${JSON.stringify(res)}`)
}, },
/** /**
...@@ -72,11 +71,19 @@ export default { ...@@ -72,11 +71,19 @@ export default {
initWebview() { initWebview() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
var currentWebview = this.$parent.$scope.$getAppWebview() //此对象相当于html5plus里的plus.webview.currentWebview()。在uni-app里vue页面直接使用plus.webview.currentWebview()无效 var currentWebview = this.$parent.$scope.$getAppWebview() //此对象相当于html5plus里的plus.webview.currentWebview()。在uni-app里vue页面直接使用plus.webview.currentWebview()无效
setTimeout(function () { let i = setInterval(() => {
var wv = currentWebview.children()[0] var wv = currentWebview.children()[0]
wv.setStyle({ top: '100%', height: 300 }) if (wv) {
resolve(wv) clearInterval(i)
}, 1000) //如果是页面初始化调用时,需要延时一下 wv.setStyle({ top: '100%', height: 300 })
resolve(wv)
}
}, 100)
// setTimeout(function () {
// var wv = currentWebview.children()[0]
// wv.setStyle({ top: '100%', height: 300 })
// resolve(wv)
// }, 1000) //如果是页面初始化调用时,需要延时一下
}) })
}, },
/** /**
...@@ -93,6 +100,7 @@ export default { ...@@ -93,6 +100,7 @@ export default {
async enQueue() { async enQueue() {
const _snapshot = await this.getRecordsSnapshotFlatten() const _snapshot = await this.getRecordsSnapshotFlatten()
this.snapshot = new Queue(_snapshot) this.snapshot = new Queue(_snapshot)
console.log('创建任务快照.下一轮快照文件个数:', this.snapshot.size)
}, },
/** /**
* 获取记录快照并展平数据 * 获取记录快照并展平数据
...@@ -119,6 +127,7 @@ export default { ...@@ -119,6 +127,7 @@ export default {
*/ */
async uploadRecord(record) { async uploadRecord(record) {
const { lsaccessKeySecret, securityToken, lsaccessKeyId } = await getOssToken() const { lsaccessKeySecret, securityToken, lsaccessKeyId } = await getOssToken()
console.log('获取Token成功!!', `lsaccessKeySecret: ${lsaccessKeySecret}, lsaccessKeyId: ${lsaccessKeyId}`)
this.wv.evalJS(`msgFromUniapp('${lsaccessKeyId}','${lsaccessKeySecret}','${securityToken}','${record.fileName}','${record.filePath}','${record.recordId}')`) this.wv.evalJS(`msgFromUniapp('${lsaccessKeyId}','${lsaccessKeySecret}','${securityToken}','${record.fileName}','${record.filePath}','${record.recordId}')`)
}, },
/** /**
...@@ -142,7 +151,7 @@ export default { ...@@ -142,7 +151,7 @@ export default {
const record = uni.$recordManager.getRecordById(recordId) const record = uni.$recordManager.getRecordById(recordId)
if (!record) return if (!record) return
this._changeFilePathToServer(record, filePathOnClient, filePathOnServer, fileName) this._changeFilePathToServer(record, filePathOnClient, filePathOnServer, fileName)
await this._saveOrUpdate(record,filePathOnClient) await this._saveOrUpdate(record, filePathOnClient)
} catch (err) { } catch (err) {
throw new Error(err) throw new Error(err)
} finally { } finally {
...@@ -196,7 +205,7 @@ export default { ...@@ -196,7 +205,7 @@ export default {
* @param {Object} record - 要保存或更新的记录对象 * @param {Object} record - 要保存或更新的记录对象
* @throws {Error} 如果在保存或更新记录时发生错误,将抛出错误 * @throws {Error} 如果在保存或更新记录时发生错误,将抛出错误
*/ */
async _saveOrUpdate(record,filePathOnClient) { async _saveOrUpdate(record, filePathOnClient) {
if (record.recordUrlList?.every(i => i.status == undefined)) { if (record.recordUrlList?.every(i => i.status == undefined)) {
const pathArr = record.recordUrlList.map(i => i.file) const pathArr = record.recordUrlList.map(i => i.file)
const params = { ...record, recordUrlList: pathArr } const params = { ...record, recordUrlList: pathArr }
...@@ -260,7 +269,11 @@ export default { ...@@ -260,7 +269,11 @@ export default {
console.log(this.snapshot.size, '当前待上传文件快照数量') console.log(this.snapshot.size, '当前待上传文件快照数量')
if (this.snapshot === null) return if (this.snapshot === null) return
// prettier-ignore // prettier-ignore
uni.$isNetWork().then(this.upload).catch(() =>{this.pollingLater(interval)}) uni.$isNetWork().then(this.upload).catch(e =>{
console.log('网络错误',e);
this.reportError('', '异常', e)
this.pollingLater(interval)
})
}, },
/** /**
* 上传文件或执行队列操作 * 上传文件或执行队列操作
...@@ -268,16 +281,13 @@ export default { ...@@ -268,16 +281,13 @@ export default {
* *
* @throws {Error} 如果在上传过程中发生错误,将抛出错误,延迟 interval 后继续 polling * @throws {Error} 如果在上传过程中发生错误,将抛出错误,延迟 interval 后继续 polling
*/ */
upload() { async upload() {
if (this.snapshot.size == 0) { if (this.snapshot.size == 0) {
this.pollingLater(interval) this.pollingLater(interval)
this.enQueue() this.enQueue()
} else { } else {
let record = this.snapshot.dequeue() let record = this.snapshot.dequeue()
// while(record.file.startsWith('http')) { await this.uploadRecord(record)
// record = this.snapshot.dequeue()
// }
this.uploadRecord(record)
} }
}, },
/** /**
...@@ -287,8 +297,10 @@ export default { ...@@ -287,8 +297,10 @@ export default {
* 然后,它将 snapshot 对象设置为 null,以释放资源并停止轮询 * 然后,它将 snapshot 对象设置为 null,以释放资源并停止轮询
*/ */
stopPolling() { stopPolling() {
this.snapshot.clear() if (this.snapshot) {
this.snapshot = null this.snapshot.clear()
this.snapshot = null
}
if (timer) clearTimeout(timer) if (timer) clearTimeout(timer)
}, },
/** /**
...@@ -302,6 +314,7 @@ export default { ...@@ -302,6 +314,7 @@ export default {
timer = setTimeout(() => { timer = setTimeout(() => {
this.polling() this.polling()
}, interval * 1000) }, interval * 1000)
console.log(interval + 's后开始下一轮上传!')
} }
}, },
/** /**
......
...@@ -90,7 +90,7 @@ class RecordManager { ...@@ -90,7 +90,7 @@ class RecordManager {
* 根据给定的 ID 获取一条记录 * 根据给定的 ID 获取一条记录
* *
* @param {number} id - 要获取的记录的 ID * @param {number} id - 要获取的记录的 ID
* @returns {Object} - 具有给定 ID 的记录对象,如果没有找到符合条件的记录,则返回 undefined881008 * @returns {Object} - 具有给定 ID 的记录对象,如果没有找到符合条件的记录,则返回 undefined
* *
*/ */
getRecordById(id) { getRecordById(id) {
......
...@@ -96,9 +96,9 @@ var isNetWork = function () { ...@@ -96,9 +96,9 @@ var isNetWork = function () {
uni.getNetworkType({ uni.getNetworkType({
complete: function (res) { complete: function (res) {
if (res.networkType == "none") { if (res.networkType == "none") {
reject(); reject('网络异常,请检查网络连接');
} else { } else {
resolve(); resolve('网络正常');
} }
}, },
}); });
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!