Blame view

src/utils/request.js 3.02 KB
1 2 3
/**
 * 此文件主要创建 axios 实例,然后添加请求拦截器和响应拦截器
 */
赵千 committed
4
import axios from 'axios'
5 6 7
import { Message } from 'element-ui'
import { startLoadingAddCount, endLoadingSubCount } from './requestLoading'

赵千 committed
8 9
// create an axios instance
const service = axios.create({
10 11
    baseURL: process.env.VUE_APP_BASE_API,
    withCredentials: true,  //是否允许跨域
赵千 committed
12
    headers: {
liangyifan committed
13
        'Content-Type': 'application/json; charset=utf-8',
任超 committed
14
        'Authorization': 'bearer AT-16-oqkOHiUSsDdFA-eAZ49k2rJQDTzQpClO'
蔡俊立 committed
15
        //token列表
任超 committed
16
        // 'Authorization': 'bearer AT-12-eRKHta5I8ZWftIU86sSyJ8rUkPhMvMJU'
蔡俊立 committed
17 18
        //renc:bearer AT-30-KHB4LXc8-CZXwBEyaFJa9lRmMTc5sHVI
        //tianh:bearer AT-33-3zFTGkhQ4eUv4nXvzAmbgN5RPZppzEY6
蔡俊立 committed
19
        //zhangh:bearer AT-7-Tx8dlZH0LNRc33UjD1CX1xwa-1D7kQmQ
赵千 committed
20
    },
21
    timeout: 15000
赵千 committed
22 23 24 25 26
})

// request interceptor
service.interceptors.request.use(
    config => {
27 28 29
        if (config.showLoading) {
            startLoadingAddCount(config.loadingTarget);
        }
赵千 committed
30 31 32
        return config
    },
    error => {
任超 committed
33
        if (error.showLoading) {
34 35 36
            endLoadingSubCount(config.loadingTarget);
        }
        Message.error('请求超时!');
赵千 committed
37 38 39 40 41 42 43
        return Promise.reject(error)
    }
)

// response interceptor
service.interceptors.response.use(
    response => {
44 45
        if (response.config.showLoading) {
            endLoadingSubCount();
赵千 committed
46
        }
47 48 49 50 51 52 53
        /**
         * 对响应数据判断:
         *  如果成功返回数据,就通过return把数据返出去
         *  如果请求不成功,就在拦截器这里统一处理(组件的代码就不用关注错误的情况了)
         */
        if (response.status == 200) {
            return response.data;
赵千 committed
54
        } else {
55
            handleErrorData(response.data);
赵千 committed
56
        }
57
        return response;
赵千 committed
58 59
    },
    error => {
60 61 62 63 64 65
        // 对响应错误做点什么
        Message({
            message: '服务器异常,请联系管理员',
            type: 'error',
            duration: 5 * 1000
        })
任超 committed
66
        if (error.config.showLoading) {
任超 committed
67 68
            endLoadingSubCount();
        }
69
        return Promise.reject(error);
赵千 committed
70 71
    }
)
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
//对错误信息的处理函数
function handleErrorData (errMes) {
    if (errMes.message) {
        Message.error(errMes.message);
    } else {
        switch (errMes.code) {
            case 401:
                Message.error("未授权,请重新登录!");
                break;
            case 403:
                Message.error("拒绝访问");
                break;
            case 404:
                Message.error("很抱歉,资源未找到!");
                break;
            case 500:
                Message.error("服务器错误!");
                break;
            case 504:
                Message.error("网络超时!");
                break;
任超 committed
93 94 95
            case 2002:
                Message.error(errMes.message);
                break;
96 97 98 99 100 101 102
            default:
                Message.error("服务正在联调中,请稍后!");
                break;
        }
    }
}

赵千 committed
103
export default service
104