Commit 11480330 by 李伟康

refactor(logger, form): 优化日志与表单处理逻辑

- 为LocalLog和LogCat类新增flushAndClose异步方法,保障日志完整落盘
- 重构LogsWorker日志上传流程,改为异步上传并添加异常捕获
- 全面重构subform.js表单工具函数,优化多选框、日期字段处理,重写显示规则解析器,新增场景与品牌过滤能力
1 parent 6c7563bc
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
"name" : "展业助手", "name" : "展业助手",
"appid" : "__UNI__D3BD3DF", "appid" : "__UNI__D3BD3DF",
"description" : "", "description" : "",
"versionName" : "2.26.4", "versionName" : "2.26.5",
"versionCode" : 2264, "versionCode" : 2265,
"transformPx" : false, "transformPx" : false,
"sassImplementationName" : "node-sass", "sassImplementationName" : "node-sass",
/* 5+App特有相关 */ /* 5+App特有相关 */
......
...@@ -93,5 +93,14 @@ class LocalLog { ...@@ -93,5 +93,14 @@ class LocalLog {
// } // }
// }) // })
} }
// 等待写入队列清空并强制关闭当前文件流,确保日志完整落盘
async flushAndClose () {
// #ifdef APP-PLUS
if (this.logger && typeof this.logger.flushAndClose === 'function') {
return this.logger.flushAndClose()
}
// #endif
return Promise.resolve()
}
} }
export default LocalLog export default LocalLog
...@@ -219,5 +219,24 @@ class LogCat { ...@@ -219,5 +219,24 @@ class LogCat {
return [] return []
} }
} }
// 等待写入队列清空并强制关闭当前文件流,确保日志完整落盘
flushAndClose() {
return new Promise((resolve) => {
// 先入队一个空任务,等前面所有写入任务都执行完
this.taskQueue.enqueue(() => {
return new Promise((r) => {
setTimeout(() => {
try {
this._closeStream()
} catch (e) {
console.error('flushAndClose 关闭流失败:', e)
} finally {
r()
}
}, 50)
})
}).then(() => resolve()).catch(() => resolve())
})
}
} }
export default LogCat export default LogCat
\ No newline at end of file \ No newline at end of file
...@@ -4,13 +4,14 @@ export default class LogsWorker { ...@@ -4,13 +4,14 @@ export default class LogsWorker {
logFile = null logFile = null
constructor() { constructor() {
this.logFile = uni.$logger.local.logger this.logFile = uni.$logger.local.logger
this.localLog = uni.$logger.local
this.isUploading = false this.isUploading = false
this.files = [] this.files = []
} }
getLogFileList() { getLogFileList() {
return this.logFile.getLogFileList() return this.logFile.getLogFileList()
} }
upload(deviceId, data) { async upload(deviceId, data) {
if (this.isUploading) { if (this.isUploading) {
const msg = `日志上传中,请稍后再试!` const msg = `日志上传中,请稍后再试!`
uni.$logger.local.info(msg) uni.$logger.local.info(msg)
...@@ -18,14 +19,36 @@ export default class LogsWorker { ...@@ -18,14 +19,36 @@ export default class LogsWorker {
return return
} }
this.isUploading = true this.isUploading = true
const files = this.getLogFileList() try {
if (this.localLog && typeof this.localLog.flushAndClose === 'function') {
await this.localLog.flushAndClose()
}
let files = this.getLogFileList() || []
if (!files.length) {
await new Promise(r => setTimeout(r, 100))
files = this.getLogFileList() || []
}
this.files = JSON.stringify(files) this.files = JSON.stringify(files)
if (files?.length) { if (files.length) {
files.sort((a, b) => a.name.localeCompare(b.name)) files.sort((a, b) => a.name.localeCompare(b.name))
this.process(files, deviceId) this.process(files, deviceId)
} else {
const msg = `没有可上传的日志文件`
uni.$logger.local.info(msg)
this.ws.send({ code: 200, msg })
this.isUploading = false
this.files = []
}
} catch (e) {
const msg = `日志上传异常: ${e?.message || e}`
uni.$logger.local.error(msg)
this.ws.send({ code: 500, error: msg })
this.isUploading = false
this.files = []
} }
} }
async process(files, deviceId) { async process(files, deviceId) {
try {
if (files.length > 0) { if (files.length > 0) {
const file = files.shift() const file = files.shift()
await this.post(file, deviceId) await this.post(file, deviceId)
...@@ -33,7 +56,14 @@ export default class LogsWorker { ...@@ -33,7 +56,14 @@ export default class LogsWorker {
} else { } else {
this.ws.send({ code: 200, msg: `日志上传完成 logs -> ${this.files}`}) this.ws.send({ code: 200, msg: `日志上传完成 logs -> ${this.files}`})
this.isUploading = false this.isUploading = false
this.files=[] this.files = []
}
} catch (e) {
const msg = `日志处理异常: ${e?.message || e}`
uni.$logger.local.error(msg)
this.ws.send({ code: 500, error: msg })
this.isUploading = false
this.files = []
} }
} }
post({ name, path }, deviceId) { post({ name, path }, deviceId) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!