/*eslint-disable*/
import axios from 'axios'
import store from '../store/index'
// import router from '../router'


let adminUrl = process.env.NODE_ENV === 'production' ? '':'http://gicdev.demogic.com'
let request ;
request  = axios.create({
  baseURL: adminUrl,
  withCredentials: true,//跨域凭证
  timeout: 30000,
  // headers: {
  //   'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
  //   },
});

// http request 拦截器
request.interceptors.request.use(
    config => {
        config.params={
            requestProject:"integral-mall",
            ...config.params
        }

        // if (store.state.token) {
        //     config.headers.Authorization = `token ${store.state.token}`;
        // }
        return config;
    },
    err => {
        return Promise.reject(err);
    });

// http response 拦截器
request.interceptors.response.use(
    response => {
        if(response.status == 200 && response.data.errorCode === 401) {
          window.location.href = window.location.origin + ('/integral-mall')
          // window.location.href = 'http://gicdev.com/gic-web'
        }
        return response;
    },
    error => {
        if (error.response) {
            switch (error.response.status) {
                case 401:
                    // 401 清除token信息并跳转到登录页面
                    store.commit(types.LOGOUT);
                    router.replace({
                        name: 'login',
                        query: {redirect: router.currentRoute.fullPath}
                    })
            }
        }
        return Promise.reject(error.response.data)
    });

export default request ;
