PublicAxiosInstance.js 1.28 KB
import axios from 'axios'
import router from '@/router/index'
import Cookies from "js-cookie"
import {getTimestampString} from "@/PublicUtil/PublicUtil"

// 创建 axios 的一个实例
const axiosInstance = axios.create(
    {
        baseURL: window._baseUrl
    }
)

const axiosInstance2 = axios.create(
    {
        baseURL: window._baseUrl2,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    }
)

// 请求拦截器
axiosInstance.interceptors.request.use(
    config => {
        config.headers.token = Cookies.get('token')

        return config
    }
)

// 响应拦截器
axiosInstance.interceptors.response.use(
    (r) => {
        const responseData = r.data

        const message = responseData.msg
        if (message === '当前会话未登录')
        {
            router.push(
                '/'
            )
        }
        else
        {
            return responseData
        }
    },
    (e) => {
        const responseData = e.response

        return responseData
    }
)

axiosInstance2.interceptors.response.use(
    (r) => {
        const responseData = r.data
        return responseData
    },
    (e) => {
        const responseData = e.response

        return responseData
    }
)

export {axiosInstance, axiosInstance2}