import { MessagePlugin } from 'tdesign-vue-next';
import axios from 'axios';
import { getCookie } from '@/services/cookie';
import router from './router';
import i18n from './i18n';
const $t = i18n.global.t;
// axios 配置
axios.defaults.timeout = 60000;
axios.defaults.withCredentials = false;

// http request 拦截器
axios.interceptors.request.use(
  (config: any) => {
    config.headers.Authorization =
      'Bearer ' + ( getCookie('token') || new URLSearchParams(location.search).get('token') || '');

    return config;
  },
  (err: any) => Promise.reject(err)
);

// http response 拦截器
axios.interceptors.response.use(
  (response: any) => {
    if (response && response.data && response.data.error) {
      MessagePlugin.error(response.data.error);
      return;
    }
    if (response) {
      if (response.status == undefined) {
        return response;
      }
      return response.data;
    }
    return;
  },
  (error: any) => {
    if (error && !error.response) {
      return;
    }
    if (error && error.response) {
      if (
        error.response.config.url === '/api/account/profile' ||
        error.response.data.error === $t('此为付费数据，请购买后访问')
      ) {
        return;
      }

      if (
        error.response.status !== 401 &&
        error.response &&
        error.response.data &&
        error.response.data.error
      ) {
        MessagePlugin.error(error.response.data.error);
        return;
      }

      switch (error.response.status) {
        case 401:
          if (error.response.config.url==='/api/account/statistics') {
            break;
          }
          sessionStorage.setItem('cb', encodeURIComponent(location.href));
          MessagePlugin.error($t('请先登录！'));
          // router.replace({ path: '/login' });
          break;
        case 403:
          MessagePlugin.error($t('请求错误，不合法的请求！'));

          break;
        case 404:
          if (error.response.config.url.indexOf('/data/') !== 0) {
            MessagePlugin.error($t('访问数据不存在，请检查后重试！'));
          }
          break;
        case 500:
          MessagePlugin.error($t('请求服务错误，请稍后重试！'));
          break;
        case 504:
          MessagePlugin.error($t('网络超时，请检测你的网络！'));
          break;
        default:
          MessagePlugin.error($t('未知网络错误！'));
          break;
      }
    }
    // return error.response ? error.response.data : error;
  }
);

export default axios;
