发送请求时带版本号

1.在axios中的二次封装中加入

import axios from 'axios'
const { Indicator, Toast } = require('mint-ui')
//公共路径
axios.defaults.baseURL = process.env.VUE_APP_BASE_API;

axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';
axios.defaults.timeout = 8000;

//设置全局的请求次数,请求的间隙
axios.defaults.retry = 2;
axios.defaults.retryDelay = 5000;

//添加一个请求拦截器
axios.interceptors.request.use(function(config){
    //在请求发出之前进行一些操作
    return config;
},function(error){
    //Do something with request error
    return Promise.reject(error);
});
//添加一个响应拦截器
axios.interceptors.response.use(undefined, function axiosRetryInterceptor(err) {
    var config = err.config;
    // If config does not exist or the retry option is not set, reject
    if (!config || !config.retry) return Promise.reject(err);

    // Set the variable for keeping track of the retry count
    config.__retryCount = config.__retryCount || 0;

    // Check if we've maxed out the total number of retries
    if (config.__retryCount >= config.retry) {
        // Reject with the error
        return Promise.reject(err);
    }

    // Increase the retry count
    config.__retryCount += 1;

    // Create new promise to handle exponential backoff
    var backoff = new Promise(function(resolve) {
        setTimeout(function() {
            Indicator.close();
            Indicator.open("后台默认排队中,请等待5秒");
            resolve();
        }, config.retryDelay || 1);
    });

    // Return the promise in which recalls axios to retry the request
    return backoff.then(function() {
        return axios(config);
    });
});

export default {
    install: function(Vue) {

        Vue.prototype.$post = params => {
            //   Indicator.open("加载中...");
            return new Promise((resolve, reject) => {
                axios.post("/CallMethod", params,
                    //发送请求头中带版本号
                    {
                        headers:{
                            Vesion:'V2.2'
                        }
                    }
                )
                    .then(response => {
                        Indicator.close()
                        resolve(response.data);
                    })
                    .catch(err => {
                        Indicator.close();
                        Toast({
                            message: "页面请求超时,请刷新重试",
                            position: "center",
                            duration: 2000
                        });
                        reject(err);
                    })
            })
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值