<template>
    <div></div>
</template>

<script>
let qs = require('qs');
let Message = require('element-ui').Message;

export default {
    init(axios){
        axios.defaults.timeout = 50000;
        axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
        // data
        axios.defaults.transformRequest = function(data) {
            return qs.stringify(data);
        }
    },
    globalInit({ $axios, store, app, redirect }){

        $axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
        $axios.defaults.transformRequest = function (data) {
            return qs.stringify(data);
        }
    
        $axios.onRequest(config => {
            if (store.state.user) {
                const { userCode, token } = store.state.user;
    
                if (!config.params) {
                    config.params = {};
                }
    
                config.params._code = userCode;
                config.params.token = token;
                //config.params.token = "9lEpYHebueA3Mrx7";
                
            }
        });
    
        $axios.onResponse(res => {
            const data = res.data;
            if (data.code == 408) {
                app.$log.error('408');
                // redirect('login?msg=408');
                // return new Promise(() => {
                //     // router.replace({
                //     //     path: 'login'
                //     // });
                //     redirect('/login');
                // });
            }
    
            if (process.client) {
                if (data.code !== 1000 && data.code!==200) {
                    // Message({
                    //     type: 'error',
                    //     message: data.message,
                    //     duration: 2000
                    // });
                }
            }
        });
    
        $axios.onError(err => {
            app.$log.error('error', err);
            if (err.message.indexOf('Network Error') !== -1) {
                if (process.client) {
                    Message({
                        // message: '网络开小差了('+err.message+')',
                        message: 'Network Error('+err.message+')',
                        type: 'error',
                        duration: 2000
                    });
                    return;
                }
            }else  if(!err.response){
                if (process.client) {
                    Message({
                        message: err.message,
                        type: 'error',
                        duration: 2000
                    });
                }
            }
            try{
                if(err.response.data.code==-9999){
                    if (process.client) {
                        redirect('/login');
                    }
                }else if(err.response.data.message){
                    //登陆过期，请重新登陆
                    if(err.response.data.message.indexOf("重新登录")!=-1 || err.response.data.message=="用户信息参数丢失" ||err.response.data.message=="未登录"){
                        if (process.client) {
                            Message({
                                dangerouslyUseHTMLString: true,
                                message: err.response.data.message,
                                type: 'error',
                                duration: 3000
                            });
                            // redirect('/login');
                            store.dispatch('logout');
                            return;
                        }
                    } 
                }
    
                if (process.client){
                    if(err.response.status!=200){
                        Message({
                            dangerouslyUseHTMLString: true,
                            message: `接口调用失败</br>msg:  ${err.message}</br>url:  ${err.response.config.url}`,
                            type: 'error',
                            duration: 3000
                        });
                    }
                }
            }catch(ex){
    
            }
        });
    }
}

</script>