import axios from 'axios';
import { MessagePlugin } from 'tdesign-vue-next';

const instance = axios.create({
  timeout: 6000000,
  withCredentials: false,
});
// 请求头
instance.interceptors.request.use((config: any) => {
  return config;
});
instance.interceptors.response.use(
  (response) => {
    const { data, status } = response;
    if (status == 201 || status == 200) {
      return status;
    }
    if (data.code === 0) {
      return data;
    } else {
      MessagePlugin.error(data.msg || '请求错误');
      return Promise.reject(data.msg);
    }
  },
  (err) => {
    console.log(err);
    if ('response' in err) {
      const { message: msg } = err.response.data;
      if (err.response.data.indexOf('<Code>UserDisable</Code>') !== -1) {
        MessagePlugin.error('阿里云可能欠费');
      } else {
        MessagePlugin.error(msg || '请求错误');
      }
      return err.response;
    }
  }
);

export default instance;
