import fetch from "isomorphic-fetch";

function checkStatus(response) {
  if (response.status >= 200 && response.status < 300) {
    return response;
  }
  throw new Error(response.status);
}
export default function request(params, token) {
  token = token || window.localStorage.getItem("token");
  let opt = {
    headers: {
      version: "5.0",
      clientId: "pc",
      Authorization: token,
      // Authorization:
      //     'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjYWNoZUtleSI6IkNPTlNPTEU6VVNFUl9UT0tFTjphZG1pbjoyMDg4NzU4MTIwMjQ3MTQ4NTQ0IiwiY2xpZW50Q29kZSI6IjgwMCIsImlzcyI6Ik1JTkciLCJpc0FwcCI6ImZhbHNlIiwidXNlckNvZGUiOiJhZG1pbiJ9.Tg8TJzJMaiYwMzfQ3IHsdMuwk57mx9Bvonpwyy6dmuY',
      "Content-Type": params?.form
        ? "application/x-www-form-urlencoded"
        : "application/json",
      "If-Modified-Since": "0", //解决ie get请求缓存问题
      "Data-Source": "production",
      language: "zh_CN",
    },
    method: "post",
  };
  if (params.method) {
    opt.method = params.method;
  }
  if (params.bodys) {
    opt.body = JSON.stringify(params.bodys);
  }
  return fetch(params.url, opt)
    .then(checkStatus)
    .then(function (response) {
      return response.json();
    })
    .catch((e) => {
      console.log(e, "+++++++++++");
    });
}
 function requestfile(params, token) {
  token = token || window.localStorage.getItem("token");
  let opt = {
    headers: {
      version: "5.0",
      clientId: "pc",
      Authorization: token,
      // Authorization:
      //     'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjYWNoZUtleSI6IkNPTlNPTEU6VVNFUl9UT0tFTjoxNzUxNDUwNTk2MjgxNDE3NzI4IiwiY2xpZW50Q29kZSI6IjgwMCIsImlzcyI6Ik1JTkciLCJpc0FwcCI6ZmFsc2UsInVzZXJDb2RlIjoiYWRtaW4ifQ.AY75CGPBCTTA0Q0C4lRERI3j1WNSMC8WF2173jbmJWI',
      "Content-Type": params?.form
        ? "application/x-www-form-urlencoded"
        : "application/json",
      "If-Modified-Since": "0", //解决ie get请求缓存问题
      "Data-Source": "production",
      language: "zh_CN",
    },
    method: "post",
  };
  if (params.method) {
    opt.method = params.method;
  }
  if (params.bodys) {
    opt.body = JSON.stringify(params.bodys);
  }
  return fetch(params.url, opt)
    .then(function (response) {
      return response.blob();
    })
    .catch((e) => {
      console.log(e, "+++++++++++");
    });
}
export { requestfile}