import axios from "axios";
import "../../public/config";
import store from "@/store";
import {Message} from "element-ui";
import router from "@/router";
import {UDI_SYNC_SERVER_IP} from "../config/app";

axios.defaults.withCredentials = true;

// 创建axios实例
const service = axios.create({

    baseURL: UDI_SYNC_SERVER_IP, // api的base_url
    timeout: 15000 // 请求超时时间
});

// request拦截器
service.interceptors.response.use(
    response => {
        const data = response.data;
        if (data.code) {
            if (data.code === 2) {
                store.dispatch("fedLogout").then(() => {
                    Message.error("登录失效,请重新登录");
                    router.push({
                        path: "/login",
                        query: {redirect: router.currentRoute.fullPath} // 从哪个页面跳转过来
                    });
                });
            }
        }
        return data;
    },
    error => {
        Message({
            message: error.message,
            type: "error",
            duration: 5 * 1000
        });
        return Promise.reject(error);
    }
);
export default service;
