Blame view

src/plugin/axios/index.js 4.28 KB
1 2 3
import store from '../../store/index'
import axios from 'axios'
import router from '../../router'
4
import {Message, MessageBox} from 'element-ui'
杨威 committed
5
import { Loading } from 'element-ui';
6
import {httpStatus} from '@/api/config'
7 8 9 10 11

// 创建一个 axios 实例
let BASE_URL = "/api";
const CONTENT_TYPE = "application/json";
const service = axios.create({
12 13 14 15 16
    baseURL: BASE_URL,
    headers: {
        "content-type": CONTENT_TYPE,
    },
    timeout: 5000 // 请求超时时间
17
})
18

19
function errorLog() {
杨威 committed
20 21 22 23
    //请求完成关闭loading
    // this.$nextTick(() => { // 以服务的方式调用的 Loading 需要异步关闭
      loadingInstance.close();
    // });
24 25 26 27
    MessageBox.alert('报错了请联系管理员', '消息提示', {
        confirmButtonText: '确定',
        type: 'warning'
    })
28
}
杨威 committed
29 30
//定义loading
let loadingInstance = null
31

32 33
// 请求拦截器
service.interceptors.request.use(
34 35 36 37 38
    config => {
        // 在请求发送之前做一些处理
        // const token = util.cookies.get('token')
        // 让每个请求携带token-- ['X-Token']为自定义key 请根据实际情况自行修改
        // config.headers['authkey'] = token
杨威 committed
39 40 41 42 43 44 45
        //请求发送成功显示loading
        loadingInstance = Loading.service({
            lock: true,
            text: 'Loading',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.5)'
          });
46 47 48 49 50 51
        return config
    },
    error => {
        // 发送失败
        Promise.reject(error)
    }
52 53 54 55
)

// 响应拦截器
service.interceptors.response.use(
56 57 58 59
    response => {
        // dataAxios 是 axios 返回数据中的 data
        const dataAxios = response.data
        const {code} = dataAxios
杨威 committed
60 61 62 63
        //请求完成关闭loading
        // this.$nextTick(() => { // 以服务的方式调用的 Loading 需要异步关闭
        loadingInstance.close();
        // });
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
        // 根据 code 进行判断
        if (code === undefined) {
            // 如果没有 code 代表这不是项目后端开发的接口 比如可能是 D2Admin 请求最新版本
            return dataAxios
        } else {
            // 有 code 代表这是一个后端接口 可以进行进一步的判断
            switch (code) {
                // code === 200 代表没有错误
                case httpStatus.OK.code:
                    return dataAxios
                // code === 11001
                case httpStatus.LOGIN_FAILURE.code:
                    router.replace({
                        path: '/login',
                        query: {redirect: router.currentRoute.fullPath}
                    })
                    util.cookies.remove('token')
                    Message({
                        message: '登录超时,请重新登录',
                        type: 'error'
                    });
                    break
                default:
                    return dataAxios;
            }
        }
    },
    error => {
        if (error && error.response) {
93
            loadingInstance.close();
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
            switch (error.response.status) {
                case 400:
                    error.message = '请求错误';
                    break
                case 401:
                    error.message = '未授权,请登录';
                    break
                case 403:
                    error.message = '拒绝访问';
                    break
                case 404:
                    error.message = `请求地址出错: ${error.response.config.url}`;
                    break
                case 408:
                    error.message = '请求超时';
                    break
                case 500:
                    errorLog()
                case 501:
                    error.message = '服务未实现';
                    break
                case 502:
                    error.message = '网关错误';
                    break
                case 503:
                    error.message = '服务不可用';
                    break
                case 504:
                    error.message = '网关超时';
                    break
                case 505:
                    error.message = 'HTTP版本不受支持';
                    break
                default:
                    break
            }
        }
        return Promise.reject(error)
132 133 134 135
    }
)

export default service