http.js 1.78 KB
import axios from 'axios'
import { Toast } from 'vant';
const baseURL = 'https://store.keliuyun.com';
const Axios = axios.create({
    baseURL: baseURL, // 因为我本地做了反向代理
    timeout: 0,
    // responseType: "json",
    changeOrigin: true,//允许跨域
    withCredentials: true, // 是否允许带cookie这些
    headers: {
        "Content-Type": "application/json;charset=UTF-8"
        // 'Content-Type': 'application/x-www-form-urlencoded'
    }
})
// 请求拦截器
Axios.interceptors.request.use(
    config => {
        return config;
    }, error => {
        console.error(error);
        return Promise.reject(error.data.msg)
    }
)
Axios.interceptors.response.use(
    res => {
        if (res.data && !res.data.success) {
            if (res.data.ecode && res.data.ecode != 200) {
                return res.data.enote;
            } else if (res.status == 200) {
				return res;
			}
            return Promise.reject(res.data.msg);
        } else if (!res.data && res.status == 200) {
            return res;
        }
        return res;
    }
)
export default function(method, url, data = null, config = null, bodyDelete) {
    method = method.toLowerCase()
    if (method == 'post') {
        return Axios.post(url, data, config)
    } else if (method == 'get') {
        let defaultParam = {
            _t: Date.parse(new Date()) / 1000
        }
        return Axios.get(url, { params: {...defaultParam, ...data }, config })
    } else if (bodyDelete) {
        return Axios.delete(url, { data: data }, config)
    } else if (method == 'delete') {
        return Axios.delete(url, { params: data }, config)
    } else if (method == 'put') {
        return Axios.put(url, data, config)
    } else {
        console.error('unknown method' + method)
        return false
    }
}