common.js 1.1 KB
import Axios from "axios";
// 请求拦截
Axios.interceptors.request.use(
    function(config) {
        return config;
    },
    function(err) {
        return Promise.reject(error);
    }
);
// 响应拦截
Axios.interceptors.response.use(
    function(response) {
        //对响应数据做事情
        console.log("请求成功", response);
        if (response.data.code == 1) {
            return response.data;
        }
    },
    function(error) {
        console.log(error);
        return Promise.reject(error);
    }
);

export default Axios;
export function uploadFile(Url, data) {
    //上传图片的方法
    return new Promise((resolve, reject) => {
        let instance = axios.create({
            baseURL: "http://tangyuanji.t.brotop.cn/api/",
            headers: {
                "Content-Type": "multipart/form-data"
            }
        });
        instance
            .post(Url, data)
            .then(res => {
                resolve(res.data.data);
            })
            .catch(error => {
                reject(error.data);
            });
    });
}