config.js 3.16 KB
import axios from 'axios'
import qs from 'qs'
import { Message } from "element-ui";
import {CONSTANT} from '../constant'
import store from '../vuex/store'

axios.interceptors.request.use(config => {
    //NProgress.start()
    return config
}, error => {
    return Promise.reject(error)
})

axios.interceptors.response.use(response => response, error => Promise.resolve(error.response))

function checkStatus(response) {

    if(response){
        const { headers, status } = response;
        if(headers&&headers.sessionstatus == 'timeout'){
            window.location.href = '../ots/login';
            return;
        }
        if (status === 200 || status === 304){
            return response.data
        }
    }

    return {
        returnCode: -404,
        message: response ? response.statusText : '',
        data: response ? response.statusText : ''
    }
}

function checkCode(res) {
    if (('returnCode' in res && res.returnCode != 0) || res.error) {
    	Message({
          	message: res.message,
          	type: 'error'
        });
    }
    return res
}

// 从mock获取基础域名配置
const MOCK_CONFIG_URL = 'https://oss-config.test.glsx.com.cn/mock/60a371eafb9c105681999ab8/microCustomizationService/e-shield-oShield-config';
let baseFlag = true;
const getBaseUrl = () => {
    return new Promise((resolve, reject) => {
        let BASE_URL = store.getters.baseURL;
        if(!BASE_URL && baseFlag) {
            baseFlag = false;
            axios({
                url: MOCK_CONFIG_URL,
                method: 'get'
            }).then((res) => {
                const resData = res.data;
                if(resData) {
                    const resBaseURL = resData.axios.baseURL;
                    store.commit('SET_BASE_URL', resBaseURL);
                    baseFlag = !resBaseURL;
                    resolve(resBaseURL);
                } else {
                    baseFlag = true;
                    resolve(BASE_URL);
                }
            }).catch((err) => {
                baseFlag = true;
                resolve(BASE_URL)
            });
        } else {
            resolve(BASE_URL)
        }
    })
};

export default {
    post(url, data) {
        getBaseUrl().then((baseUrl) => {
           return axios({
                method: 'post',
                url: `${baseUrl}/ots/${url}`,
                data: typeof data == 'string'?data:qs.stringify(data),
                timeout: 30000,
                headers: {
                    'X-Requested-With': 'XMLHttpRequest',
                    'Content-Type': typeof data == 'string'?'application/json':'application/x-www-form-urlencoded; charset=UTF-8'
                }
            }).then(checkStatus).then(checkCode)
        }, (err) => {
        })
       
    },
    get(url, params) {
        getBaseUrl().then((baseUrl) => {
            return axios({
                method: 'get',
                url: `${baseUrl}/ots/${url}`,
                params,
                //timeout: 300000,
                headers: {
                    'X-Requested-With': 'XMLHttpRequest'
                },
                withCredentials:true
            }).then(checkStatus).then(checkCode)
        }, (err) => {
        })
    }
}