import _Message from "@alifd/next/es/message";
import axios from 'axios';
import { getCookieData, $_GET } from "../utils/utils";
axios.defaults.baseURL = '/api';
axios.defaults.withCredentials = true;
axios.interceptors.request.use(function (config) {
  var _config$data, _config$headers, _$_GET$appId, _$_GET$platformId;
  if (config !== null && config !== void 0 && (_config$data = config.data) !== null && _config$data !== void 0 && _config$data.ContentType) {
    var _config$data2;
    config.headers['Content-Type'] = config === null || config === void 0 ? void 0 : (_config$data2 = config.data) === null || _config$data2 === void 0 ? void 0 : _config$data2.ContentType;
    delete config.data.ContentType;
  } else {
    config.headers['Content-Type'] = 'multipart/form-data';
  }
  config.headers['Access-Control-Allow-Origin'] = '*';
  config.headers.Authorization = (config === null || config === void 0 ? void 0 : (_config$headers = config.headers) === null || _config$headers === void 0 ? void 0 : _config$headers.Authorization) || ($_GET === null || $_GET === void 0 ? void 0 : $_GET.token) || getCookieData().token || '';
  var _appId = (_$_GET$appId = $_GET === null || $_GET === void 0 ? void 0 : $_GET.appId) !== null && _$_GET$appId !== void 0 ? _$_GET$appId : '';
  var _platformId = (_$_GET$platformId = $_GET === null || $_GET === void 0 ? void 0 : $_GET.platformId) !== null && _$_GET$platformId !== void 0 ? _$_GET$platformId : '';
  if (_appId && _platformId) {
    var _$_GET$platformId2, _$_GET$appId2;
    config.headers.platformId = (_$_GET$platformId2 = $_GET === null || $_GET === void 0 ? void 0 : $_GET.platformId) !== null && _$_GET$platformId2 !== void 0 ? _$_GET$platformId2 : '';
    config.headers.appId = (_$_GET$appId2 = $_GET === null || $_GET === void 0 ? void 0 : $_GET.appId) !== null && _$_GET$appId2 !== void 0 ? _$_GET$appId2 : '';
  }
  return config;
});
axios.interceptors.response.use(function (res) {
  var status = res.status,
    data = res.data,
    baseURL = res.config.baseURL;
  var header = res.headers.header;
  if (status !== 200) {
    _Message.error('服务错误，请联系管理员');
    return;
  }
  switch (data.code) {
    case 401:
      _Message.notice('登录失效,请重新登录');
      break;
    case 200:
      return res.data;
    case 10200:
      switch (baseURL) {
        case '/comp-api':
          return res.data;
        default:
          return {
            a: res.data,
            b: header
          };
      }
    default:
      return _Message.warning(data.msg || '');
  }
}, function (error) {
  return Promise.reject(error);
});
export default axios;