import axios from 'axios';
import {Dialog} from 'vant';

let apiInstance = axios.create({
    baseURL: process.env.VUE_APP_BASE_URL,
    timeout: 10000,
    withCredentials: true,
});
/* 为请求添加请求头中的token */
apiInstance.interceptors.request.use(
    config => {
        const token = sessionStorage.getItem('token');
        token && (config.headers.Authorization = token);
        return config;
    }, 
    error => {
        return Promise.error(error);
    },
);
/* 响应拦截器 */
apiInstance.interceptors.response.use(response => {
    if (response.data.code === 'BIZ.BUSSINESS_EXCEPTION') {
        Dialog.alert({
            title: '提示',
            message: response.data.msg,
        });
    };
    if (response.data.code === 'PARAM.EXCEPTION') {
        Dialog.alert({
            title: '提示',
            message: '系统异常，请联系客服！',
        });
    };
    if (response.data.code === 'SYS.UNKNOWN_EXCEPTION') {
        Dialog.alert({
            title: '提示',
            message: '系统异常，请联系客服！',
        });
    };
    return response.data;
}, error => {
    Dialog.alert({
        title: '提示',
        message: error,
    });
    return Promise.reject(error);
});

export default apiInstance;