import axios from 'axios';
import {CookieUtils} from "@/utils/CookieUtils.ts";
import router from "@/router";
import MessageUtils from "@/utils/MessageUtils.ts";

// const baseUrlAddress = () => {
//     console.log(import.meta.env.MODE);
//     if (import.meta.env.MODE === 'production') {
//         // 生产环境逻辑
//         return 'http://localhost:9980/api/wcs';
//     } else {
//         // 开发环境逻辑
//         return 'http://localhost:9970/api/wcsdev';
//     }
// }

const axiosInstance = axios.create({
    baseURL: 'http://localhost:9970/api/wcsdev',
    timeout: 30000,
    headers: {
        "Content-Type": "application/json"
    }
});

// 请求前
axiosInstance.interceptors.request.use((config) => {
    const token = new CookieUtils().getItem('wcsToken');
    if (token) {
        config.headers['Wcstoken'] = token;
    }
    return config;
}, (error) => {
    return Promise.reject(error);
})

// 响应后动作
axiosInstance.interceptors.response.use((response) => {
    return response;
}, (error) => {
    if (error.response && error.response.status === 401) {
        new CookieUtils().clear();
        router.push({ name : 'login'}).then(r => {});
        MessageUtils.errMessage('您尚未登录或者登录已经过期，请重新登录');
    } else {
        console.log(error);
        MessageUtils.errMessage('服务器请求异常，请重试');
    }
    return Promise.reject(error);
})


export default axiosInstance;