request.js 1.89 KB
const Axios = axios.create({
    baseURL: window._CONF_.apiUrl,
    timeout: 0,
    withCredentials: true,
    headers: {
        "Content-Type": "application/json;charset=UTF-8"
    }
})

const getUrlQueryByName = function(name) {
    name = name.replace(/[\[\]]/g, '\\$&')
    let regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)')
    let results = regex.exec(window.location.href)
    if (!results)
    {
        return null
    }
    if (!results[2])
    {
        return ''
    }
    return decodeURIComponent(results[2].replace(/\+/g, ' '))
}

Axios.interceptors.request.use(
    config => {

        if (window.location.hostname === 'localhost')
        {
            config.headers.Authorization = 'c3b85226-4301-45fb-a087-4caf3194fc90'
        }
        else
        {
            config.headers.Authorization = Cookies.get('atoken')
        }
        return config
    },
    error => {
        return Promise.reject(error)
    }
)

Axios.interceptors.response.use(
    res => {
        // if (res.data.enote || res.data.code === 401) {
        //   ELEMENT.MessageBox.alert('授权到期, 请重新登录', '提示', {
        //     confirmButtonText: '确定',
        //     callback: action => {
        //       console.log('action', action)
        //       window.location.replace(window.location.href + 'login.html')
        //     }
        //   });
        // } else {
        return Promise.resolve(res.data)
        // }
    },
    error => {
        console.error(error)
        return Promise.reject(error)
    }
)

function get(url, params = {}, config = {}) {
    params['s'] = +new Date()
    return Axios.get(url, {...config, params})
}

function post(url, params, config = {}) {
    return Axios.post(url, params, config)
}

function deletes(url, params, config = {}) {
    return Axios.delete(url, params, config)
}

function put(url, params, config = {}) {
    return Axios.put(url, params, config)
}