import axios from 'axios';
import { MessagePlugin } from 'tdesign-vue-next';
import i18n from '@/language';
const instance = axios.create({
  timeout: 1000,
  withCredentials: true,
});

instance.interceptors.request.use((config) => {
  return config;
});

instance.defaults.timeout = 60000;

instance.interceptors.response.use(
  (response) => {
    const { data } = response;
    if (data.code === 200) {
      return data.data;
    } else if (data.code === 0) {
      return data;
    } else {
      MessagePlugin.closeAll();
      MessagePlugin.error(data.msg || i18n.global.t('request.error'));
      return Promise.reject(data.msg);
    }
  },
  (err) => {
    const { config } = err;
    if ('response' in err) {
      const { message: msg } = err.response.data;
      MessagePlugin.error(msg || i18n.global.t('request.error'));
    }
    if (!config || !config.retry) return Promise.reject(err);
  }
);

export default instance;
