import vue from 'vue'
const request = (Obj) => {
  return new Promise((resolve, reject) => {
    axios(Obj).then(res => resolve(res)).catch(err => reject(err))
  })
};
// 数据过滤处理
const RequestDataHandle = (res) => {
  return new Promise((resolve, reject) => {
    let { code } = res.data;
    if (code == 0) {
      // vue.prototype.$message.success("请求成功！")
      resolve(JSON.parse(JSON.stringify(res.data)))
      return false;
    } else {
      reject(res.data.msg);
      return false;
    }
  })
};

const post = (url, query = {}) => {
  let Obj = {
    url,
    method: 'POST',
    headers: {
      Authorization: sessionStorage.getItem('token')
    },
    data: JSON.parse(JSON.stringify(query))
  }
  return new Promise((resolve, reject) => {
    request(Obj).then(res => RequestDataHandle(res)
    ).then(res => resolve(res)).catch(err => vue.prototype.$message.error(`温馨提示：${err}`))
  })
}

const get = (url, query = {}) => {
  let Obj = {
    url,
    method: 'GET',
    headers: {
      Authorization: sessionStorage.getItem('token')
    },
    params: JSON.parse(JSON.stringify(query))
  }
  return new Promise((resolve, reject) => {
    request(Obj).then(res => RequestDataHandle(res)).then(res => resolve(res)).catch(err => vue.prototype.$message.error(`温馨提示：${err}`))
  })
}

export {
  post, get
}