
export async function reqGet(dir, cb) {
  fetch(dir).then(function(data) {
    return data.json();
  }).then(function(data) {
    console.log(data);
    cb({data: data, err: null});
    return data;
  }).catch(function(error) {
    cb({data: {}, err: error});
  });
}

export async function reqGetDownload(dir, cb) {
  fetch(dir).then(function(data) {
    console.log(data);
    window.open(dir);
    return data.blob();
  }).catch(function(error) {
    console.log(error);
    cb({err: error});
  });
}

export function reqPost(dir, reqData, cb) {
  if (reqData && typeof reqData !== 'string') {
    reqData = JSON.stringify(reqData);
  }
  fetch(dir, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Accept': 'application/json'
    },
    body: reqData,
  }).then((res) => {
    if (res.status >= 200 && res.status < 300) {
      return res;
    } else {
      return Promise.reject('请求失败！');
    }
  }).then(data => {
    console.log(data);
    cb({data: data, err: null});
    return data;
  }).catch(error => {
    console.log(error);
    cb({data: {}, err: error});
    return error;
  });
}

function getServerCurrentPathFiles(method, reqData) {
  if (method !== 'GET' || method != 'POST') {
    console.log('请求方法只能是 GET 或者 POST');
    return;
  }
  if (method === 'GET') {
    reqData = undefined;
  } else {
    reqData = reqData && JSON.stringify(reqData);
  }
  fetch('', {
    method: method,
    headers: {
      'Content-Type': 'application/json',
      'Accept': 'application/json'
    },
    body: reqData,
  }).then((res) => {
    if (res.status >= 200 && res.status < 300) {
      return res;
    } else {
      return Promise.reject('请求失败！');
    }
  }).then(data => {
    console.log(data);
    return {data: data, err: null};
  }).catch(error => {
    console.log(error);
    return {data: {}, err: error};
  });
}