import axios from 'axios';
// var baseURL='http://118.190.145.217:8077'
var baseURL='http://xjctest.baianxi.com'
// var baseURL = '/'
axios.interceptors.request.use(
  config => {
    const token = localStorage.getItem('token')
    if(token){
      config.headers.Authorization = token
    }
    return config
  },
  err => {
    return Promise.reject(err)
  });
axios.interceptors.response.use(function (response) {
  // console.log(response.headers)
  // IE 8-9 
  if (response.data == null && response.config.responseType === 'json' && response.request.responseText != null) {
    try {
      // eslint-disable-next-line no-param-reassign
      response.data = JSON.parse(response.request.responseText)
    } catch (e) {
      // ignored
    }
  }
  // 对响应数据做点什么
  if (response.data.code == 401) {
    // window.location.hash = "/login";
    // window.location.hash = "/";
  }
  if (response.data.code == 305) {
    // window.location.hash = "/";
  }
  if (response.data.code == 403) {
    window.location.hash = "/";
  }
  return response;
}, function (error) {
  // 对响应错误做点什么
  if (error.request.status == 403) {
    window.location.hash = "/";
  }
  // console.log(Object.getOwnPropertyNames(error))
  // console.log(error.request,error.message)
  return Promise.reject(error)
});
export default {
  // 登录
  login: params => {
    return axios.post(`${baseURL}/api/account/doctor/login`, params)
  },
  sendPhcode: params => {
    return axios.post(`${baseURL}/api/account/phcode`, params)
  },
  logout: params => {
    return axios.post(`${baseURL}/api/account/logout`, params)
  },
  // 全局账号信息
  getAccountInfo: params => {
    return axios.get(`${baseURL}/api/account/doctor/authinfo`)
  },
  // 病人注册
  regPatient: params => {
    return axios.post(`${baseURL}/api/doctor/patient/register`, params)
  },
  // 病人注册
  regPatientCase: params => {
    return axios.post(`${baseURL}/api/doctor/patient/case/register`, params)
  },
  // 病例详情
  getPatientCaseInfo: params => {
    return axios.get(`${baseURL}/api/doctor/patient/case/info`, { params: params })
  },
  // 病例列表
  getPatientCaseList: params => {
    return axios.get(`${baseURL}/api/doctor/patient/case/list`, { params: params })
  },
  // 病人最新病例
  getPatientLatestCase: params => {
    return axios.get(`${baseURL}/api/doctor/patient/case/latest`, { params: params })
  },
  // 注册打卡
  regPatientRecords: params => {
    return axios.post(`${baseURL}/api/doctor/patient/records`, params)
  },
  // 患者打开记录
  getPatientRecordsList: params => {
    return axios.get(`${baseURL}/api/doctor/patient/records/list`, { params: params })
  },
  // 获取PK曲线
  getPKLineData: params => {
    return axios.get(`${baseURL}/api/doctor/patient/case/pkline`, { params: params })
  },
}
