Commit a0557323 by yixinjj

日志文件排队上传

1 parent 66959f50
......@@ -33,7 +33,7 @@ class LogCat {
path.mkdirs() //创建父路径
}
let fos = null
let bw = null
let bw = null
try {
fos = new this.FileOutputStream(fileName, true)
bw = new this.BufferedWriter(new this.OutputStreamWriter(fos))
......
......@@ -56,7 +56,7 @@ export class Ws {
this.ws.on(WsTyps.Connected, () => {
uni.$logger.local.info('========WebSocket 连接成功========')
})
this.ws.on(
this.ws.on(
WsTyps.MessageTypes.Private,
function (data) {
uni.$logger.local.info(`WebSocket 收到消息 -> ${JSON.stringify(data)}`)
......
......@@ -4,38 +4,59 @@ export default class LogsWorker {
logFile = null
constructor() {
this.logFile = uni.$logger.local.logger
this.isUploading = false
}
getLogFileList() {
return this.logFile.getLogFileList()
}
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()
this.process(files, deviceId)
if (files?.length) {
files.sort((a, b) => a.name.localeCompare(b.name))
this.process(files, deviceId)
}
}
process(files, deviceId) {
files.forEach(file => this.post(file, deviceId))
async process(files, 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) {
uni.uploadFile({
url: `${config.PROTOCAL}://${config.SERVER}:${config.HTTP_PORT}/${config.FILE_UPLOAD_URL}`,
filePath: path,
name: 'file',
header: {
'Content-Type': 'multipart/form-data'
},
formData: {
deviceId,
type: 'log'
},
success: res => {
uni.$logger.local.info(`文件上传成功! res->${serializeIfObject(res)}`)
},
fail: res => {
uni.$logger.local.error(`文件上传失败! res->${serializeIfObject(res)}`)
}
return new Promise((resolve, reject) => {
uni.uploadFile({
url: `${config.PROTOCAL}://${config.SERVER}:${config.HTTP_PORT}/${config.FILE_UPLOAD_URL}`,
filePath: path,
name: 'file',
header: {
'Content-Type': 'multipart/form-data'
},
formData: {
deviceId,
type: 'log'
},
success: res => {
uni.$logger.local.info(`文件上传成功! res->${serializeIfObject(res)} filePath->${path}`)
resolve()
},
fail: res => {
uni.$logger.local.error(`文件上传失败! res->${serializeIfObject(res)} filePath->${path} .请检查服务端是否有文件被打开`)
resolve()
}
})
})
}
work(deviceId, data,ws) {
work(deviceId, data, ws) {
this.ws = ws
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!