import axios from 'axios';
import {show_message} from './tdesign';

const mode = import.meta.env.MODE;
const getBaseUrl = () => {
    if (mode == 'development') {
        return 'http://dexnav-main.test';
    } else {
        return '';
    }
};
const instance = axios.create({
    baseURL: getBaseUrl(),
    timeout: 60000,
    // withCredentials: mode == 'development' ? false : true,
    withCredentials: false,
});
// 请求头
instance.interceptors.request.use((config: any) => {
    return config;
});

instance.defaults.timeout = 60000;

instance.interceptors.response.use(
    (response) => {
        const {data} = response;
        if (data.code === 0 || data.code == 201 || data.code == 1) {
            return data;
        } else {
            show_message(data.msg || '请求错误', 'error');
            return Promise.reject(data.msg);
        }
    },
    (err) => {
        if ('response' in err) {
            const {message: msg, status} = err.response;
            if (status == 401) {
                show_message('请登录');
                window.location.replace("/login");
                return;
            }
            show_message(msg || '请求错误', 'error');
            return err.response;
        }
    }
);

export default instance;
