import axios from 'axios'
import qs from 'qs'
import { checkFalse } from '../../../static/js/checkStatus';

export function fetch(url, options) {
  return new Promise((resolve, reject) => {
    axios.post(url, options).then(res => {
      if (res.data.errorCode == 0) {
        resolve(res);
      } else {
        checkFalse(res.data.message);
        return reject();
      }
    }).catch(err => {
      checkFalse(err);
      return reject(err);
    });
  })
}
export function fetchqs(url,options) {
  return new Promise((resolve, reject) => {
    axios.post(url, qs.stringify(options)).then(res => {
      if (res.data.errorCode == 0) {
        resolve(res);
      } else {
        checkFalse(res.data.message);
        return reject();
      }
    }).catch(err => {
      checkFalse(err);
      return reject(err);
    });
  })
}

export function fetchGet(url, options) {
  return new Promise((resolve, reject) => {
    axios.get(url, { params: options }).then(res => {
      if (res.data.errorCode == 0) {
        resolve(res);
      } else {
        checkFalse(res.data.message);
        return reject();
      }
    }).catch(err => {
      checkFalse(err);
      return reject(err);
    })
  })
}
