Ext 重写url请求,对请求做统一处理

Ext.Ajax.addListener("beforerequest", function(conn, options, eOpts ){
    if(options){
        var nParam = options.params;
        if (nParam != undefined) {
            //将form-data参数转为数组
            var arrParam = objTransArray(nParam);
            //新增时间戳
            var timestamp = new Date().getTime();
            arrParam.push("timestamp="+timestamp);
            nParam["timestamp"]=timestamp;
            // end
            //新增get请求的参数
            arrParam = addGetUrlParam(arrParam, options.url)
            //排序
            arrParam.sort();//排序 从小-大 ascii码
            //构建加密字符串
            var newP = newParamBuilder(arrParam);
            console.log("sign start");
            console.log(newP);
            console.log("sign end");
            //加密
            var sign = signBuild(newP);
            var headers = new Object();
            headers.verifyHeaderValue = sign;
            options.headers = headers;
            options.params = nParam;
        }
    }
}, this);
/**
 * obj对象转为数组
 * @param obj
 */
function objTransArray(obj){
    var array = $.map(obj,function(value,key){
        if(value != null && value != "null" && value != "" && value !=undefined) {
            return [key + "=" + value];
        }
    });
    return array;
}
function addGetUrlParam(arr,url){
    var urlArr  = url.split("?");
    if(urlArr[1] != undefined){
        var subUrlArr = urlArr[1].split("&");
        for(var i=0; i< subUrlArr.length;i++){
            arr.push(subUrlArr[i])
        }
    }
    return arr;
}

function newParamBuilder(arr){
    var param = "";
    for(var i = 0;i < arr.length; i++) {
        if(i + 1 == arr.length) {
            param += arr[i] ;
        }else{
            param += arr[i] + "123#&%678";
        }
    }
    return param;
}

/**
 * 生成校验值
 * @param arr
 * @returns {*}
 */
function signBuild(param){
    return $.md5(param);//依赖jquery.md5.js
}

  

posted @ 2021-02-01 14:06  SimpleSmile  阅读(401)  评论(0编辑  收藏  举报