import _Message from "@alifd/next/es/message";
import axios from 'axios';
import { getCookieData, $_GET, getWxAppid } from "../utils/utils";
axios.defaults.baseURL = '/api';
axios.defaults.withCredentials = true;
axios.interceptors.request.use(function (config) {
  var _config$data, _config$headers, _$_GET$platformId, _$_GET$appId;
  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';
  }
  // url中有appid是来源于小程序的分享，appid等于getWxAppid中则是微信公众号
  var _urlAppId = $_GET === null || $_GET === void 0 ? void 0 : $_GET.appId;
  var _getCookieData = getCookieData(),
    token = _getCookieData.token;
  if (_urlAppId && _urlAppId === getWxAppid) {
    token = getCookieData().webToken;
  }
  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) || token || '';
  config.headers.platformId = (_$_GET$platformId = $_GET === null || $_GET === void 0 ? void 0 : $_GET.platformId) !== null && _$_GET$platformId !== void 0 ? _$_GET$platformId : 'WEB';
  config.headers.appId = (_$_GET$appId = $_GET === null || $_GET === void 0 ? void 0 : $_GET.appId) !== null && _$_GET$appId !== void 0 ? _$_GET$appId : 'WEB';
  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;