axiosリクエストの前にグローバルパラメータを付ける

2908 ワード

//  
const API = {
    local:'XXXXX',
    online:'XXXXX',       
};

//axios , 
// :
var login_key,uid;
function getUrlParam(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.hash.substr(1).match(reg);
    if (r!=null) return unescape(r[2]); return null;
}

function getQueryVariable(val) {
    var query = window.location.hash.substring(1);
    var vars = query.split("&");
    if( val == 'uid'){
        for (var i=0;i= 1) {
        for (var i = 0; i < arr.length; i++) {
            var name = arr[i].substring(0, arr[i].indexOf("="));
            if (name == paramName) {
                var begin = arr[i].indexOf("=");
                var end = arr[i].length;
                paramValue = arr[i].substring(begin + 1, end);
                break;
            }
        }
    }
    return paramValue;
}

login_key = request('login_key');
uid = request('uid');

------------------------------------------------------------------------------------------------------------------

//production  
if( process.env.NODE_ENV != 'production' ){
    axios.defaults.baseURL = API.local;
}else{
    axios.defaults.baseURL = API.online;
}
axios.defaults.timeout = 6000;

//http request  
axios.interceptors.request.use(
    config => {
        config.data = {
            ...config.data,
            login_key:login_key,
            uid:uid
        }
        config.data = qs.stringify(config.data);

        config.headers = {
            'Content-Type':'application/x-www-form-urlencoded'
        };
        return config;
    },
    err => {
      return Promise.reject(err);
    }
  );

//http response  
axios.interceptors.response.use(
    response => {
        if(response.status == 200){
            // console.log(response.data.error_code);
            if (response.data.error_code == 10001) {
                if(window.AnalyticsWebInterface){ //  
                    window.AnalyticsWebInterface.postMessage ('{loginInvalid: {}}');
                }else if(window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.firebase){ // IOS
                    window.webkit.messageHandlers.firebase.postMessage({loginInvalid: {}});
                }else{
                    alert(' ');
                }
            }else{
                return response.data;
            }
        }
    },
    error => {
        return Promise.reject(error.data)
    }
);