import axios from 'axios';
import {Message,Modal,Spin} from 'view-design';
import router from "./../../router/index";
axios.defaults.baseURL = process.env.BASE_API;

axios.interceptors.request.use(function (config) {
    Spin.show({
        render: (h) => {
            return h('div', [
                h('Icon', {
                    'class': 'demo-spin-icon-load',
                    props: {
                        type: 'ios-loading',
                        size: 40
                    }
                }),
                h('div', '加载中...')
            ])
        }
    });
    config.headers.token = sessionStorage.getItem('adminToken');
    return config;
}, function (error) {
    return Promise.reject(error);
});
axios.interceptors.response.use(function (res) {
    if (res.data.code === 0) {
        Spin.hide();
        return res;
    } else if (res.data.code == 401) {
        Spin.hide();
        Message.warning(res.data.msg);
        Modal.confirm({
            title: '一诺云检SaaS管理平台',
            content: 'token已过期，请重新登录',
            okText: '去登录',
            cancelText: '等会再说',
            onOk: () => {
                router.push("/")
            },
            onCancel: () => {
            }
        });
    } else {
        Spin.hide();
        Message.warning(res.data.msg);
    }
}, function (error) {
    Spin.hide();
    Message.error('服务器错误');
});
export default axios;





