request.js 2.0 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']
35
        config.data = JSON.stringify(config.data);
Mr.奇淼('s avatar
Mr.奇淼( 已提交
36
        config.headers = {
37
            'Content-Type': 'application/json',
Mr.奇淼('s avatar
Mr.奇淼( 已提交
38 39 40 41 42
            'x-token': token
        }
        return config;
    },
    error => {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
43
        closeLoading()
Mr.奇淼('s avatar
Mr.奇淼( 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56
        Message({
            showClose: true,
            message: error,
            type: 'error'
        })
        return Promise.reject(error);
    }
);


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

export default service