import axios from "axios";
import { ElMessage } from "element-plus";
axios.interceptors.request.use(function (config) {
    let token = "8681f75e775447f7903708c55a35634e";
    if (token) {
        config.headers.Authorization = token;
    }
    return config;
}, (err) => {
    return Promise.reject(err);
});
axios.interceptors.response.use(response => {
    return response.data;
}, err => {
    if (err && err.response) {
        switch (err.response.status) {
        case 401:
            err.msg = "请先登录";
            break;
        }
    } else {
        err.msg = "网络连接错误";
    }
    return Promise.reject(err);
});
let baseUrl = "http://10.40.8.9:3000"

export function $get ({ url, params = {}, callback }) {
    return axios({
        method: "get",
        url: baseUrl + url,
        params
    }).then(response => {
        callback && callback(response.data);
        return response;
    }).catch((err) => {
        ElMessage.error(err);
    });
};

export function $post ({ url, data = {}, callback }) {
    return axios.post(baseUrl + url, data).then((response) => {
        if (response.code === 0) {
            callback && callback(response.data);
        } else {
            ElMessage.error(response.message);
        }
        return response;
    }).catch((err) => {
        console.log(err);
        ElMessage.error(err.msg || "操作失败");
        if (err && err.response && err.response.status === 401) {
            router.replace("/login");
        }
    });
};
