request.js 2.2 KB
Newer Older
Mr.奇淼('s avatar
Mr.奇淼( 已提交
1
import axios from 'axios'; // 引入axios
Mr.奇淼('s avatar
Mr.奇淼( 已提交
2
import { Message, Loading } from 'element-ui';
Mr.奇淼('s avatar
Mr.奇淼( 已提交
3 4 5
import { store } from '@/store/index'
const service = axios.create({
    baseURL: process.env.VUE_APP_BASE_API,
Mr.奇淼('s avatar
Mr.奇淼( 已提交
6
    timeout: 99999
Mr.奇淼('s avatar
Mr.奇淼( 已提交
7
})
Mr.奇淼('s avatar
Mr.奇淼( 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21
let acitveAxios = 0
let loadingInstance
let timer
const showLoading = () => {
    acitveAxios++
    if (timer) {
        clearTimeout(timer)
    }
    timer = setTimeout(() => {
        if (acitveAxios > 0) {
            loadingInstance = Loading.service({ fullscreen: true })
        }
    }, 400);
}
Mr.奇淼('s avatar
Mr.奇淼( 已提交
22

Mr.奇淼('s avatar
Mr.奇淼( 已提交
23
const closeLoading = () => {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
24 25 26 27 28
        acitveAxios--
        if (acitveAxios <= 0) {
            clearTimeout(timer)
            loadingInstance && loadingInstance.close()
        }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
29
    }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
30
    //http request 拦截器
Mr.奇淼('s avatar
Mr.奇淼( 已提交
31 32
service.interceptors.request.use(
    config => {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
33
        showLoading()
Mr.奇淼('s avatar
Mr.奇淼( 已提交
34
        const token = store.getters['user/token']
Mr.奇淼('s avatar
Mr.奇淼( 已提交
35
        const user = store.getters['user/userInfo']
36
        config.data = JSON.stringify(config.data);
Mr.奇淼('s avatar
Mr.奇淼( 已提交
37
        config.headers = {
38
            'Content-Type': 'application/json',
Mr.奇淼('s avatar
Mr.奇淼( 已提交
39
            'x-token': token,
Mr.奇淼('s avatar
Mr.奇淼( 已提交
40
            'x-user-id': user.ID
Mr.奇淼('s avatar
Mr.奇淼( 已提交
41 42 43 44
        }
        return config;
    },
    error => {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
45
        closeLoading()
Mr.奇淼('s avatar
Mr.奇淼( 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58
        Message({
            showClose: true,
            message: error,
            type: 'error'
        })
        return Promise.reject(error);
    }
);


//http response 拦截器
service.interceptors.response.use(
    response => {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
59
        closeLoading()
Mr.奇淼('s avatar
Mr.奇淼( 已提交
60 61 62
        if (response.headers["new-token"]) {
            store.commit('user/setToken', response.headers["new-token"])
        }
63
        if (response.data.code == 0 || response.headers.success === "true") {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
64 65 66 67
            return response.data
        } else {
            Message({
                showClose: true,
Mr.奇淼('s avatar
Mr.奇淼( 已提交
68
                message: response.data.msg || decodeURI(response.headers.msg),
69
                type: 'error',
Mr.奇淼('s avatar
Mr.奇淼( 已提交
70
            })
Mr.奇淼('s avatar
Mr.奇淼( 已提交
71 72 73
            if (response.data.data && response.data.data.reload) {
                store.commit('user/LoginOut')
            }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
74 75 76 77
            return Promise.reject(response.data.msg)
        }
    },
    error => {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
78
        closeLoading()
Mr.奇淼('s avatar
Mr.奇淼( 已提交
79 80 81 82 83 84 85 86 87 88
        Message({
            showClose: true,
            message: error,
            type: 'error'
        })
        return Promise.reject(error)
    }
)

export default service