Commit a0557323 by yixinjj

日志文件排队上传

1 parent 66959f50
...@@ -33,7 +33,7 @@ class LogCat { ...@@ -33,7 +33,7 @@ class LogCat {
path.mkdirs() //创建父路径 path.mkdirs() //创建父路径
} }
let fos = null let fos = null
let bw = null let bw = null
try { try {
fos = new this.FileOutputStream(fileName, true) fos = new this.FileOutputStream(fileName, true)
bw = new this.BufferedWriter(new this.OutputStreamWriter(fos)) bw = new this.BufferedWriter(new this.OutputStreamWriter(fos))
......
...@@ -56,7 +56,7 @@ export class Ws { ...@@ -56,7 +56,7 @@ export class Ws {
this.ws.on(WsTyps.Connected, () => { this.ws.on(WsTyps.Connected, () => {
uni.$logger.local.info('========WebSocket 连接成功========') uni.$logger.local.info('========WebSocket 连接成功========')
}) })
this.ws.on( this.ws.on(
WsTyps.MessageTypes.Private, WsTyps.MessageTypes.Private,
function (data) { function (data) {
uni.$logger.local.info(`WebSocket 收到消息 -> ${JSON.stringify(data)}`) uni.$logger.local.info(`WebSocket 收到消息 -> ${JSON.stringify(data)}`)
......
...@@ -4,38 +4,59 @@ export default class LogsWorker { ...@@ -4,38 +4,59 @@ export default class LogsWorker {
logFile = null logFile = null
constructor() { constructor() {
this.logFile = uni.$logger.local.logger this.logFile = uni.$logger.local.logger
this.isUploading = false
} }
getLogFileList() { getLogFileList() {
return this.logFile.getLogFileList() return this.logFile.getLogFileList()
} }
upload(deviceId, data) { upload(deviceId, data) {
if (this.isUploading) {
const msg = `日志上传中,请稍后再试!`
uni.$logger.local.info(msg)
// this.ws.send(msg)
return
}
this.isUploading = true
const files = this.getLogFileList() const files = this.getLogFileList()
this.process(files, deviceId) if (files?.length) {
files.sort((a, b) => a.name.localeCompare(b.name))
this.process(files, deviceId)
}
} }
process(files, deviceId) { async process(files, deviceId) {
files.forEach(file => this.post(file, deviceId)) if (files.length > 0) {
const file = files.shift()
await this.post(file, deviceId)
this.process(files, deviceId)
} else {
this.isUploading = false
}
} }
post({ name, path }, deviceId) { post({ name, path }, deviceId) {
uni.uploadFile({ return new Promise((resolve, reject) => {
url: `${config.PROTOCAL}://${config.SERVER}:${config.HTTP_PORT}/${config.FILE_UPLOAD_URL}`, uni.uploadFile({
filePath: path, url: `${config.PROTOCAL}://${config.SERVER}:${config.HTTP_PORT}/${config.FILE_UPLOAD_URL}`,
name: 'file', filePath: path,
header: { name: 'file',
'Content-Type': 'multipart/form-data' header: {
}, 'Content-Type': 'multipart/form-data'
formData: { },
deviceId, formData: {
type: 'log' deviceId,
}, type: 'log'
success: res => { },
uni.$logger.local.info(`文件上传成功! res->${serializeIfObject(res)}`) success: res => {
}, uni.$logger.local.info(`文件上传成功! res->${serializeIfObject(res)} filePath->${path}`)
fail: res => { resolve()
uni.$logger.local.error(`文件上传失败! res->${serializeIfObject(res)}`) },
} fail: res => {
uni.$logger.local.error(`文件上传失败! res->${serializeIfObject(res)} filePath->${path} .请检查服务端是否有文件被打开`)
resolve()
}
})
}) })
} }
work(deviceId, data,ws) { work(deviceId, data, ws) {
this.ws = ws this.ws = ws
this.upload(deviceId, data) this.upload(deviceId, data)
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!