http.js 3.39 KB
import axios from 'axios'
import md5 from 'blueimp-md5'
import { param2 } from '@/utils'
import { Toast } from 'vant';
//const baseURL = 'https://store.keliuyun.com/report';
//const baseURL = 'https://mall.keliuyun.com';
const baseURL = 'http://192.168.1.106:81';
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'
    }
})
let store = {
    source: {
        token: null,
        cancel: null
    }
}
// 请求拦截器
Axios.interceptors.request.use(
    config => {
        const suffix = '4c413628731691abc99eb2fca5f69aab'
        const { method, params, data, url } = config
        let language = localStorage.getItem('lang'),
            aToken = localStorage.getItem('atoken');
        if (language) {
            let languageObj = {
                'mall_CN': 'zh-CN,zh;q=0.9',
                'zh_CN': 'zh-CN,zh;q=0.9',
                'en_US': 'en-US,en;q=0.5',
                'zh_TW': 'zh-TW,zh;q=0.3',
                // 'zh_ar': 'zh-AR,zh;q=0.9'
            }
            languageObj[language] && (config.headers['Accept-Language'] = languageObj[language]);
        }
        aToken && (config.headers.Authorization = aToken);
        config.cancelToken = store.source.token
        const signalStr = method.toUpperCase() +
            (params ? param2(params) : '') +
            (data ? JSON.stringify(data) : '') +
            suffix + (config.headers.Authorization ? config.headers.Authorization : '');
        const hashSignature = md5(signalStr)
        config.headers['signature'] = hashSignature
        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) {
                Toast.fail(res.data.enote)
                console.error(res.data.enote)
                return ;
            } else {
                // 非标准返回体
                if (res.status == 200) {
                    return res;
                }
            }
            if (res.data.code == 500) {
                console.error(res.data.msg)
            }
            return Promise.reject(res.data.msg);
        } else if (!res.data && res.status == 200) {
            return res;
        } else if (res.data.success && res.config.method != 'get') {
            // 
        }
        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
    }
}