适用于Ewei Open Api的Sign计算脚本

2018-12-06

为方便在Postman中测试易维开放平台中需要sign参数相关的接口,写了两个Pre-Request-Script脚本,用于在Postman中动态计算签名。

文档:http://api.ewei.com/index#/index/developer_read/join_guide

##

v1 版本相关API

// 前置处理器:计算请求签名(OpenV1)
var _app_secret = pm.environment.get("provider_app_secret");
var _time_open_v1 = (new Date()).valueOf();
var _request_method = request.method;
var _request_url_no_query_string = getRequestUrlWithNoQueryString();
var _request_body = pm.request.body.raw;

var _params = _request_method 
    + getStringWithPopParameters(_request_url_no_query_string) 
    + ("_app_secret=" + _app_secret)
    + ("_timestamp=" + _time_open_v1)
    + getQueryStringWithNoSplitter()
    + (_request_body ? ("requestBody=" + _request_body) : "");
console.log("_params = " + _params);

var _url_encoded_params = encodeURIComponent(_params);
console.log("_url_encoded_params = " + _url_encoded_params);

var _lower_url_encoded_params = _url_encoded_params.toLowerCase();
console.log("_lower_url_encoded_params = " + _url_encoded_params);

var _sign_open_v1 = CryptoJS.MD5(_lower_url_encoded_params).toString();
console.log("_sign_open_v1 = " + _sign_open_v1);

pm.environment.set("_time_open_v1", _time_open_v1);
pm.environment.set("_sign_open_v1", _sign_open_v1);

function getQueryStringWithNoSplitter() {
    var request_url = request.url;
    var query_string = request_url.substring(request_url.indexOf('?') + 1);
    var query_string_no_splitter = query_string.replace("&","");
    return getStringWithPopParameters(query_string_no_splitter);
}

function getRequestUrlWithNoQueryString() {
    var request_url = request.url;
    return request_url.substring(0, request_url.indexOf('?'));
}

function getStringWithPopParameters(str) {
    var variableArray = extractPmVariables(str);
    for (var i = 0; i < variableArray.length; i++) {
        var va = variableArray[i];
        if (va.variableValue) {
            str = str.replace(va.variableKey, va.variableValue);
        }
    }
    return str;
}

function extractPmVariables(requestUrl) {
    var variableArray = [];
    var variables = requestUrl.match(//g);
    if (variables) {
       for (var i = 0; i < variables.length; i++) {
            var variableKey = variables[i];
            var variableName = variableKey.replace(",","");
            var variableValue = pm.environment.get(variableName);
            variableArray.push({
                "variableKey": variableKey,
                "variableName": variableName,
                "variableValue": variableValue
            });
        } 
    }
    return variableArray;
}

使用示例

image

image

v2 版本相关API

// 前置处理器:计算请求签名(OpenV2)
var _app_secret = pm.environment.get("provider_app_secret");
var _time_open_v2 = (new Date()).valueOf();
var _pre_sign = 'requestBody=' + pm.request.body.raw + ',time=' + _time_open_v2 + ',appSecret=' + _app_secret;
var _sign_open_v2 = CryptoJS.MD5(_pre_sign).toString();
pm.environment.set("_time_open_v2", _time_open_v2);
pm.environment.set("_sign_open_v2", _sign_open_v2);

console.log('[Pre]OpenAccountApi.getUserToken _pre_sign='+_pre_sign+',_sign_open_v2=' + _sign_open_v2);

使用示例

image

image