postman中pre-script,生成MD5接口加密参数sign

postman中pre-script,生成MD5接口加密参数sign。
加密参数格式按照ascll排序,+timestarp时间戳的代码

//获取当前请求13位的时间戳
var currenttimestamp = Math.round(new Date().getTime());
pm.environment.set('currenttimestamp',currenttimestamp);
处理MD5加密
//传入一个json对象,得到一个key值按照ascii码排序进行url参数拼接的字符
function sort_ASCII(obj){
  var arr = new Array();
  var num = 0;
  for (var i in obj) {
    arr[num] = i;
    num++;
  }
  var sortArr = arr.sort();
  var functionRespStr = []
  for (var i in sortArr) {
    var key = sortArr[i]
    var value = obj[key]
    if(key == 'timestamp'){
        value = currenttimestamp
    }
    if(value != '' && key != 'sign'){
        //值不能为空,不加入sign
        functionRespStr.push(key+"="+value)
    }
  }
  return functionRespStr.join('&');
}
//设置MD5密钥值
pm.environment.set('md5key',"md5key001");
//获取请求的body参数
var jsondata = pm.request.body.raw;
console.log(jsondata);
//转为json对象
var fromatJson = JSON.parse(jsondata)
var sign = sort_ASCII(fromatJson)
//拼接密钥
sign = (sign+'&secret='+pm.environment.get("md5key"))
console.log("加密的参数:\n"+sign)
pm.environment.set("sign", CryptoJS.MD5(sign).toString());

你可能感兴趣的:(java,javascript)