Commit 2cb1e5eb by yixinjj

调整定时器启动逻辑

1 parent 1c4757c6
import { getOssToken } from '@/utils/ossUploadFile/uploadFile.js' import { getOssToken } from '@/utils/ossUploadFile/uploadFile.js'
import Queue from '@/utils/Queue.js' import Queue from '@/utils/Queue.js'
const interval = 60*10 const interval = 60 * 10
const timer = null
export default { export default {
onLoad() {}, onLoad() {},
data() { data() {
...@@ -24,7 +25,7 @@ export default { ...@@ -24,7 +25,7 @@ export default {
async init() { async init() {
this.wv = await this.initWebview() this.wv = await this.initWebview()
await this.enQueue() await this.enQueue()
this.pollingLater(interval) this.polling()
}, },
/** /**
* 处理消息函数 * 处理消息函数
...@@ -35,32 +36,29 @@ export default { ...@@ -35,32 +36,29 @@ export default {
* @param {Object} result - 包含消息详情的对象 * @param {Object} result - 包含消息详情的对象
* @throws {Error} 如果解构对象时缺少关键属性,将抛出错误 * @throws {Error} 如果解构对象时缺少关键属性,将抛出错误
*/ */
handlerMessage(result) { async handlerMessage(result) {
const { code, fileName, data, recordId, filePath, filePathOnClient = filePath + fileName } = result.detail.data[0] const { code, fileName, data, recordId, filePath, filePathOnClient = filePath + fileName } = result.detail.data[0]
try{ try {
if (code == 0) { if (code == 0) {
const filePathOnServer = data.oss.res.requestUrls[0].split('?')[0] const filePathOnServer = data.oss.res.requestUrls[0].split('?')[0]
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.polling()
// this.$sendSentry(`上传失败:文件名-${fileName} 错误码-${data.oss.res.status},失败原因-${JSON.stringify(data.oss.res)}`); // 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 {
this.polling()
} }
}, },
/** /**
* 上报文件上传失败日志 * 上报文件上传失败日志
*/ */
reportError(recordId, filePath,res) { reportError(recordId, filePath, res) {
const { customerName, phoneNumber, carModel, salesName } = const { customerName, phoneNumber, carModel, salesName } = uni.$recordManager.getRecordById(recordId) || {}
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)}`
);
}, },
/** /**
* 初始化 WebView 的异步函数 * 初始化 WebView 的异步函数
...@@ -146,11 +144,10 @@ export default { ...@@ -146,11 +144,10 @@ export default {
this._removeFileOnClient(filePathOnClient) this._removeFileOnClient(filePathOnClient)
await this._saveOrUpdate(record) await this._saveOrUpdate(record)
} catch (err) { } catch (err) {
this.reportError(recordId,filePathOnClient,err) this.reportError(recordId, filePathOnClient, err)
Promise.reject(error) Promise.reject(error)
} finally { } finally {
unlock() unlock()
this.polling()
} }
}, },
/** /**
...@@ -236,7 +233,7 @@ export default { ...@@ -236,7 +233,7 @@ export default {
flattenData(records) { flattenData(records) {
const result = records.reduce((acc, cur) => { const result = records.reduce((acc, cur) => {
const recordUrlList = cur.recordUrlList.map(file => { const recordUrlList = cur.recordUrlList.map(file => {
if(!file.status) { if (!file.status) {
return [] return []
} }
const aFullPath = file.file.split('/') const aFullPath = file.file.split('/')
...@@ -291,6 +288,7 @@ export default { ...@@ -291,6 +288,7 @@ export default {
stopPolling() { stopPolling() {
this.snapshot.clear() this.snapshot.clear()
this.snapshot = null this.snapshot = null
if (timer) clearTimeout(timer)
}, },
/** /**
* 在指定的时间间隔后启动轮询 * 在指定的时间间隔后启动轮询
...@@ -299,7 +297,8 @@ export default { ...@@ -299,7 +297,8 @@ export default {
* @returns {void} - 此函数不返回任何值 * @returns {void} - 此函数不返回任何值
*/ */
pollingLater(interval = 60 * 10) { pollingLater(interval = 60 * 10) {
setTimeout(() => { if (timer) clearTimeout(timer)
timer = setTimeout(() => {
this.polling() this.polling()
}, interval * 1000) }, interval * 1000)
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!