欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

axios

程序员文章站 2022-03-04 23:50:34
...

import axios from ‘axios’
import * as Cookies from ‘js-cookie’;
import {Notification} from “element-ui”;
import { MessageBox } from ‘mint-ui’;
import router from ‘…/…/router’
import config from ‘…/…/assets/ajaxConfig’
import bus from ‘…/…/server/bus’
/**

  • Create Axios
    */
    var num=0;
    var lang=localStorage.language;
    if(lang==‘简体中文’ || lang==undefined){
    lang=‘zh-cn’
    }else{
    lang=‘en-us’
    }

export const http = axios.create({baseURL:config.url})
var _token= localStorage._token;

http.defaults.headers.common = {
lang,
‘X-Requested-With’: ‘XMLHttpRequest’,
“Content-Type”: “application/json;charset=UTF-8;javascript”,//
Accept: ‘application/json;script’,
Authorization: _token,
};

// let pending = []; //声明一个数组用于存储每个ajax请求的取消函数和ajax标识
// let cancelToken = axios.CancelToken;
// let removePending = (config) => {
// for(let p in pending){
// if(pending[p].u === config.url + ‘&’ + config.method) { //当当前请求在数组中存在时执行函数体
// pending[p].f(); //执行取消操作
// pending.splice(p, 1); //把这条记录从数组中移除
// }
// }
// }
/**

  • axios 请求拦截
    */
    // http.interceptors.request.use(response=>{
    // removePending(response);
    // response.cancelToken = new cancelToken(©=>{
    // // 这里的ajax标识我是用请求地址&请求方式拼接的字符串,当然你可以选择其他的一些方式
    // pending.push({ u: response.url + ‘&’ + response.method, f: c });
    // });

// return response;
// },error=>{
// return Promise.reject(error);
// })

/**

  • axios 响应拦截

  • @param requestUrl

  • @returns
    */
    http
    .interceptors
    .response
    .use((response) => {
    setTimeout(()=>{
    if(response.data.code==5001){
    localStorage.removeItem(’_token’);
    bus.KaTeX parse error: Expected 'EOF', got '}' at position 26: …ut'); }̲ },300) …emit(‘unopen’,response.data.msg);
    }
    return response.data;
    }, (error) => {

     const {response} = error
    
     return response
    

    });

export default function install(Vue) {
Object.defineProperty(Vue.prototype, ‘$http’, {
get() {//get 函数为调用原型方法时调用
router.beforeEach((to,from,next)=>{
if(to.name==‘home’ && from.name==‘login’){//此条件为重新登录后
_token=localStorage._token//重新设置token;
http.defaults.headers.common = {
‘X-Requested-With’: ‘XMLHttpRequest’,
“Content-Type”: “application/json;charset=UTF-8;javascript”,
Accept: ‘application/json’,
Authorization: _token,
lang,
};
}
if((to.path==’/person’ && from.path==’/user/language’)||(to.path==’/login’ && from.path==’/user/language’) ){
lang=localStorage.language
if(lang==‘简体中文’ || lang==undefined){

                lang='zh-cn'
            }else{
               
                lang='en-us'
            }
            http.defaults.headers.common = {
                lang,
                'X-Requested-With': 'XMLHttpRequest',
                "Content-Type": "'application/x-www-form-urlencoded;charset=UTF-8;script",//
                Accept: 'application/json;script',
                Authorization: _token, 
            };  
          
        }
        next();
    }) 
        return http
    }
})

}

相关标签: axios