"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.service = exports.post = exports.imgUrl = exports.get = exports.baseURL = void 0;
var _message = _interopRequireDefault(require("@alifd/next/lib/message"));
var _notification = _interopRequireDefault(require("@alifd/next/lib/notification"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _axios = _interopRequireDefault(require("axios"));
var _tools = require("../tools");
// export const baseURL = localStorage.getItem('currentServerHost') || `${window.location.origin}/restcloud`;
// export const baseURL = "https://bestapplink.com/restcloud";
var baseURL = "http://dev.restcloud.cn:8082/restcloud";
exports.baseURL = baseURL;
var imgUrl = baseURL + "/rest/base/files/mgdb/image/";
exports.imgUrl = imgUrl;
var service = _axios["default"].create({
  baseURL: baseURL,
  timeout: 180 * 1000,
  responseType: "json"
});
exports.service = service;
service.interceptors.request.use(function (config) {
  var identitytoken = (0, _tools.getCookie)('identitytoken');
  var obj = (0, _extends2["default"])({}, 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 = (0, _extends2["default"])({}, err);
    newErr.response = {};
    newErr.response.status = 2021;
    newErr.response.data = {};
    newErr.response.data.msg = '请求接口超时!';
    newErr.response.data.state = false;
    _notification["default"].open({
      title: '操作提示',
      content: '服务请求超时！',
      type: "error"
    });
    return Promise.reject(newErr.response && newErr.response.data);
  } else if (err.response.status === 500) {
    _notification["default"].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["default"].open({
      title: '操作提示',
      content: err.response.data && err.response.data.msg || '服务请求失败,请检查服务器填写及服务接口是否正确!',
      type: "error"
    });
    return Promise.reject(err.response && err.response.data);
  } else {
    _message["default"].error(err.response && err.response.data && err.response.data.msg || '接口异常');
    return Promise.reject(err.response && err.response.data);
  }
});
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);
    });
  });
};
exports.get = get;
var post = function post(url, params, config) {
  return new Promise(function (resolve, reject) {
    service.post(url, (0, _extends2["default"])({}, params), config).then(function (res) {
      resolve(res);
    })["catch"](function (err) {
      reject(err);
    });
  });
};
exports.post = post;