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

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

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

##

v1 版本相关API

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// 前置处理器:计算请求签名(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(/{{[A-Za-z0-9-\$_]+}}/g);
if (variables) {
for (var i = 0; i < variables.length; i++) {
var variableKey = variables[i];
var variableName = variableKey.replace("{{","").replace("}}","");
var variableValue = pm.environment.get(variableName);
variableArray.push({
"variableKey": variableKey,
"variableName": variableName,
"variableValue": variableValue
});
}
}
return variableArray;
}

使用示例

v2 版本相关API

1
2
3
4
5
6
7
8
9
// 前置处理器:计算请求签名(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);

使用示例