postman 验签

sign 验签规则

var timestamp = getTimeStamp();
postman.setEnvironmentVariable("timestamp", timestamp)
var sign = makeSign();
postman.setEnvironmentVariable("sign", sign)
console.log("++++++++++++++++++++")
console.log(timestamp)
console.log(sign)
//获取时间戳
function getTimeStamp() {
    var time = Date();
    var date = new Date(time);
    Y = date.getFullYear() + '-';
    M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    D = date.getDate();
    if (D < 10) {
        D = '0' + D;
    }
    D = D + ' '
    var h = date.getHours();
    if (h < 10) {
        h = '0' + h + ':';
    } else {
        h = h + ':';
    }
    m = date.getMinutes();
    if (m < 10) {
        m = '0' + m + ':';
    } else {
        m = m + ':';
    }
    s = date.getSeconds();
    if (s < 10) {
        s = '0' + s;
    }
    time = Y + M + D + h + m + s;
    return time;
}

function makeSign()     //得到md5签名
{
    var requestUrl = request.url;                       //获取url 字符串
    // console.log(requestUrl); 
    //去除空白字符
    // console.log((requestUrl));
    var requestBody = request.data;                       //获取请求体
    var appsecret = "7bea0f725c2f";   //appsercet 随环境而改变
    var position = requestUrl.indexOf('?');
    requestUrl = requestUrl.substring(position + 1);
    var keyValueArr = requestUrl.split("&");
    var paramsArr = {};
    var key = "", value = "";
    for (var i = 0, count = keyValueArr.length; i < count; i++) {
        position = keyValueArr[i].indexOf('=');
        key = keyValueArr[i].substring(0, position);
     switch (key) {
            case "timestamp":
                value = postman.getEnvironmentVariable("timestamp");
                //   console.log(value);
                break;
            default:
                value = keyValueArr[i].substring(position + 1);
                break;
        }
        paramsArr[key] = value;
    }
    paramsArr = objKeySort(paramsArr);                    //paramsArr 数组按照字典序排序

    var md5Str = "";
    for (key in paramsArr) {
        // console.log(key + paramsArr[key]);
        if (key == "sign") {
            continue;
        }

        md5Str += key + paramsArr[key];
    }
    console.log("::::::::::::::::::::::::::::::")
    console.log(md5Str)
    console.log(requestBody)
    md5Str = appsecret + md5Str + requestBody + appsecret;
    // md5Str = requestBody
    // md5Str = md5Str.replace(/\s+/g,"");                                                        //去除空白字符
    // console.log((md5Str));
    var sign = CryptoJS.MD5(md5Str).toString().toUpperCase();
    return sign;
}

function objKeySort(arys)           //将关联数组按照key 字典序排序,返回值是排序后的数组。
{
    //先用Object内置类的keys方法获取要排序对象的属性名,再利用Array原型上的sort方法对获取的属性名进行排序,newkey是一个数组
    var newkey = Object.keys(arys).sort();
    var newObj = {}; //创建一个新的对象,用于存放排好序的键值对
    for (var i = 0; i < newkey.length; i++) {
        newObj[newkey[i]] = arys[newkey[i]];
    }
    return newObj;
}

  

posted @ 2022-08-02 13:59  PHP小媛  阅读(243)  评论(0编辑  收藏  举报