import _Message from "@alifd/next/es/message";
import _Notification from "@alifd/next/es/notification";
import _extends from "@babel/runtime/helpers/extends";
import axios from "axios";
import { getCookie } from "../tools";

// export const baseURL = localStorage.getItem('currentServerHost') || `${window.location.origin}/restcloud`;
// export const baseURL = "https://bestapplink.com/restcloud";
export var baseURL = "http://dev.restcloud.cn:8082/restcloud";
export var imgUrl = baseURL + "/rest/base/files/mgdb/image/";
export var service = axios.create({
  baseURL: baseURL,
  timeout: 180 * 1000,
  responseType: "json"
});
service.interceptors.request.use(function (config) {
  var identitytoken = getCookie('identitytoken');
  var obj = _extends({}, config, {
    headers: {}
  });
  if (identitytoken) {
    obj.headers = {
      identitytoken: identitytoken
    };
  }
  return obj;
}, function (error) {
  return Promise.reject(error);
});
service.interceptors.response.use(function (res) {
  if (res.status === 200) {
    return Promise.resolve(res);
  } else {
    return Promise.reject(res);
  }
}, function (err) {
  if (!err) return false;
  if (err && !err.response) {
    var newErr = _extends({}, err);
    newErr.response = {};
    newErr.response.status = 2021;
    newErr.response.data = {};
    newErr.response.data.msg = '请求接口超时!';
    newErr.response.data.state = false;
    _Notification.open({
      title: '操作提示',
      content: '服务请求超时！',
      type: "error"
    });
    return Promise.reject(newErr.response && newErr.response.data);
  } else if (err.response.status === 500) {
    _Notification.open({
      title: '操作提示',
      content: err.response.data && err.response.data.msg || '服务请求处理异常!',
      type: "error"
    });
    return Promise.reject(err.response && err.response.data);
  } else if (err.response.status === 404) {
    _Notification.open({
      title: '操作提示',
      content: err.response.data && err.response.data.msg || '服务请求失败,请检查服务器填写及服务接口是否正确!',
      type: "error"
    });
    return Promise.reject(err.response && err.response.data);
  } else {
    _Message.error(err.response && err.response.data && err.response.data.msg || '接口异常');
    return Promise.reject(err.response && err.response.data);
  }
});
export var get = function get(url, params) {
  return new Promise(function (resolve, reject) {
    service.get(url, {
      params: params
    }).then(function (res) {
      resolve(res);
    })["catch"](function (err) {
      reject(err);
    });
  });
};
export var post = function post(url, params, config) {
  return new Promise(function (resolve, reject) {
    service.post(url, _extends({}, params), config).then(function (res) {
      resolve(res);
    })["catch"](function (err) {
      reject(err);
    });
  });
};