LogsWorker.js 1.07 KB
import config from '@/config.js'
import { serializeIfObject } from '@/utils/utils.js'
export default class LogsWorker {
  logFile = null
  constructor() {
    this.logFile = uni.$logger.local.logger
  }
  getLogFileList() {
    return this.logFile.getLogFileList()
  }
  upload(deviceId, data) {
    const files = this.getLogFileList()
    this.process(files, deviceId)
  }
  process(files, deviceId) {
    files.forEach(file => this.post(file, deviceId))
  }
  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)}`)
      }
    })
  }
  work(deviceId, data) {
    this.upload(deviceId, data)
  }
}