PublicAxiosInstance.js 1.98 KB
import axios from 'axios'
import router from '@/router/index'
import Cookies from "js-cookie"
import {getTimestampString} from "@/PublicUtil/PublicUtil"
import {ElMessage} from 'element-plus'
// 创建 axios 的一个实例
let baseURL =  ['localhost', '192.168.1.134'].includes(window.location.hostname) ? '' : window._baseUrl
const axiosInstance = axios.create(
    {
        baseURL: baseURL,
        // timeout:10000
    }
)
// 请求拦截器
axiosInstance.interceptors.request.use(
    config => {
        // Cookies.set('atoken','94bbea90-fe9e-461e-93a2-4e4e9b2b6652')
        if(!Cookies.get('atoken')){
            ElMessage({
            	message: `登录过期,请重新登录`,
            	type: 'warning'
            })
            setTimeout(()=>{
                let url =  ['localhost', '192.168.1.134'].includes(window.location.hostname) ? 'https://store.keliuyun.com/' : window.location.origin
                // if(url.includes('36.112.68.214')){
                //     url = 'http://36.112.68.214:33333/'
                // }
                window.open(url,'_blank')
            },300)
            return
        }
        config.headers.Authorization = Cookies.get('atoken')

        return config
    }
)

// 响应拦截器
axiosInstance.interceptors.response.use(
    (r) => {
        const responseData = r.data
		if(responseData.ecode && responseData.ecode == '401'){
			ElMessage({
				message: `登录过期,请重新登录`,
				type: 'warning'
			})
            setTimeout(()=>{
                let url =  ['localhost', '192.168.1.134'].includes(window.location.hostname) ? 'https://store.keliuyun.com/' : window.location.origin
                // if(url.includes('36.112.68.214')){
                //     url = 'http://36.112.68.214:33333/'
                // }
                window.open(url,'_blank')
            },300)
			return
		}
        return responseData
    },
    (e) => {
        const responseData = e.response

        return responseData
    }
)

export default axiosInstance