import http from 'axios'

let readerApi = {
  connect: 'http://127.0.0.1:18889/api/connect',
  disconnect: 'http://127.0.0.1:18889/api/disconnect',
  readCert: 'http://127.0.0.1:18889/api/readCert'
}

export function getReaderConnect () {
  return http.post(readerApi.connect, true).then(res => {
    try {
      let data = typeof res.data === 'string' ? JSON.parse(res.data.replace(/\r|\n|\t/g, '').replace(/\\/g, '\\\\')) : res.data
      return data
    } catch (e) {
      console.log(e)
      return {}
    }
  })
}
export function getReaderDisconnect () {
  return http.post(readerApi.disconnect, true)
}

export function getReadCert () {
  return http.post(readerApi.readCert).then(res => {
    try {
      let data = typeof res.data === 'string' ? JSON.parse(res.data.replace(/\r|\n|\t/g, '').replace(/\\/g, '\\\\')) : res.data
      return data
    } catch (e) {
      console.log(e)
      return {}
    }
  })
}
