import axios from 'axios'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import {Message} from 'element-ui'

const service = axios.create({
    timeout: 60000,
    // withCredentials: true
});

service.interceptors.request.use(
    config => {
        if (!config.mute) {
            NProgress.start()
        }
        return config
    },
    error => {
        NProgress.remove();
        Message.error('服务异常');
        return Promise.reject(error)
    }
);

service.interceptors.response.use(
    response => {
        NProgress.done();
        if (response.data.res_info) {
            if (response.data.res_info.response_code !== '00000') {
                Message.error(response.data.res_info.response_msg)
            }
            return response.data.body
        } else {
            if (response.data.code !== 200) {
                Message.error(response.data.msg)
            }
            return response.data.data
        }
    },
    error => {
        Message.error('服务异常');
        NProgress.done();
        if (axios.isCancel(error)) {
            return Promise.reject(error)
        }
        return Promise.reject(error)
    }
);

export default service
