axios的post传参时,将参数转为form表单格式

import axios from 'axios';
import alert from './alert.js';
import Qs from 'qs'  //引入qs  时axios的自带模块

let env = process.env.NODE_ENV;
let root = '';

if (env === 'development') {
    console.log("api");
} else if (env === 'production') {
    console.log("pro");
    root = '';
} else {
    throw '请检查process.env.NODE_ENV的值,是否符合这些值之一:development,production';
}

Date.prototype.format = function (fmt) {
    var o = {
        "y+": this.getFullYear(),
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "H+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds() //秒 
    };
    if (!fmt) {
        fmt = 'yyyy-MM-dd HH:mm:ss';
    }
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        }
    }
    return fmt;
}

// 自定义判断元素类型JS
function toType(obj) {
    return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase();
}

// 参数过滤函数
function filterNull(o) {
    for (var key in o) {
        if (o[key] === null) {
            delete o[key];
        }
        if (toType(o[key]) === 'string') {
            o[key] = o[key].trim();
        } else if (toType(o[key]) === 'date') {
            o[key] = (o[key].format());
        } else if (toType(o[key]) === 'object') {
            o[key] = filterNull(o[key]);
        } else if (toType(o[key]) === 'array') {
            o[key] = filterNull(o[key]);
        }
    }
    return o;
}

function apiAxios(method, url, params, success, failure, authFail) {
    console.log('url:' + url);
    if (params) {
        params = filterNull(params);
    }

    var base = "";
    if (url.indexOf(".html") != -1) {
        base = "";
    } else {
        base = root;
    }

    axios({
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' 
        },
        transformRequest: [function(data) {//在请求之前对data传参进行格式转换
            data = Qs.stringify(data)
            return data
        }],
        method: method,
        url: url,
        data: method === 'POST' || method === 'PUT' || method === 'DELETE' ? params : null,
        params: method === 'GET' ? params : null,
        baseURL: base,
        withCredentials: true
    }).then(function (res) {
        if (res.status >= 200 && res.status <= 210) {
            if (success) {
                success(res);
            }
        } else {
            //不走
            // window.alert('error: ' + JSON.stringify(res.data));
        }
    }).catch(function (err) {
        let res = err.response;
        if (err && res) {
            console.log(res.status);
            if (res.status == 504) {
                alert.eduToast("服务器连接失败!请检查您的网络或服务器!!",2000);
                return;
            } else if (res.status == 401) {
                console.log('------------------:status'+res.status);
                console.log('------------------:authFail'+authFail);
            }
            if (failure) {
                failure(res);
            } else {
                alert.eduToast(res.data,2000);
            }
        } else {
            if(authFail){
                // localStorage.setItem('login', '');
            }else{
                console.log(err);
            }
        }
    });
}

// 返回在vue模板中的调用接口
export default {
    get: function (url, params, success, failure,authFail) {
        return apiAxios('GET', url, params, success, failure,authFail);
    },
    post: function (url, params, success, failure) {
        return apiAxios('POST', url, params, success, failure);
    },
    put: function (url, params, success, failure) {
        return apiAxios('PUT', url, params, success, failure);
    },
    delete: function (url, params, success, failure) {
        return apiAxios('DELETE', url, params, success, failure);
    },
    initHeader: function () {
        console.log('------------------:initHeader');
    }
};

 

 

若传json格式,不用加header,和transformRequest   在合适地方将对象用JSON.stringfiy(),转化即可

posted @ 2019-05-10 18:50  古墩古墩  Views(3088)  Comments(0Edit  收藏  举报