ApiPost
是一个支持模拟POST、GET、PUT等常见HTTP请求,支持团队协作,
并可直接生成并导出接口文档的API调试、管理工具
国产版postman
@link https://www.apipost.cn/
这里主要记录一个常规sign校验生成脚本
一、增加执行脚本
便于API开发时多接口的sign值自动生成,为了方便通常在目录级设置“目录公用预执行脚本”
以下是预执行脚本
完整代码
/**
* Sign规则生成器
*
* @param {Object} param 请求参数
* @param {String} key sign校验盐
*
* @return {String} 规则生成的校验密串
*/
$.signRule = function (param, key) {
//拼接
let _sign = [];
for (let k in param) {
_sign.push(`${k}=${param[k]}`);
}
_sign = _sign.join("&");
_sign = _sign.replace(/\(/, "%28");
_sign = _sign.replace(/\)/, "%29") + (!!key ? "#" + key : "");
console.log("加密前:");
console.log(_sign);
_sign = CryptoJS.MD5(_sign).toString();
_sign = _sign.toUpperCase();
console.log("加密后:");
console.log(_sign);
return _sign;
};
/**
* Sign生成器
*
* @param {String} signkey sign校验盐
* @param {String} fieldMode 字段模式:request(默认)、variable
* @param {String} varName 字段模式为variable时,设定要赋值的变量名称
*/
$.sign = function (signkey, fieldMode, varName) {
const _method = request.method;
const _signName = "sign";
fieldMode = fieldMode ? fieldMode : "request";
varName = varName ? varName : "sign";
const _toolsUnit = {
/**
* 判断是否是空值
*
* @return {Boolean}
*/
isEmpty: function (obj) {
if (typeof obj === "undefined" || obj === null || obj === "") {
return true;
} else {
return false;
}
},
/**
* 判断是否是JSON数据
*
* @param {String|Object} str 待验证数据
*
* @return {Boolean}
*/
isJson: function (str) {
if (typeof str == "string") {
try {
JSON.parse(str);
return true;
} catch (e) {
console.log(e);
return false;
}
} else {
return typeof str == "object";
}
},
/**
* 获取请求参数
*
* @param {request} request
*
* @return {Object}
*/
getParam: function (request) {
if (_method === "GET") {
param = request.request_querys;
} else {
param = request.request_bodys;
}
if (this.isJson(param) && typeof param == "string") {
//BODY不是JSON格式
param = JSON.parse(param);
}
// 替换变量
param = this.replaceVar(param);
return param;
},
/**
* 替换参数变量
*
* @param {Object} param
*
* @return {Object}
*/
replaceVar: function (param) {
// 替换变量
for (let i in param) {
if (this.isJson(param[i])) {
let _tamp = JSON.stringify(param[i]);
var _var = _tamp.substring(2, _tamp.length - 2);
_tamp = undefined;
} else {
var _var = param[i].substring(2, param[i].length - 2);
}
if (apt.variables.get(_var)) {
param[i] = apt.variables.get(_var);
} else {
param[i] = param[i];
}
if (this.isEmpty(param[i])) {
delete param[i];
}
}
for (let fName in param) {
if (apt.globals.get(fName)) {
param[fName] = apt.globals.get(fName);
} else {
param[fName] = param[fName];
}
if (this.isEmpty(param[fName])) {
delete param[fName];
}
}
return param;
},
/**
* 请求参数追加校验字段
*
* @param {String} sign
*
* @return {Boolean}
*/
autoSignAttach: function (sign) {
if (fieldMode === "request") {
if (_method === "GET") {
apt.setRequestQuery(_signName, sign);
} else {
apt.setRequestBody(_signName, sign); //只针对 form-data、urlencode有效
}
} else {
apt.variables.set(varName, sign);
}
return true;
},
};
const _signUnit = function () {};
/**
* 创建Sign校验值
*
* @return {String}
*/
_signUnit.prototype.create = function () {
let param = _toolsUnit.getParam(request);
//取key
var keys = [];
for (let k in param) {
if (k == "sign") {
continue;
}
keys.push(k);
}
//排序
keys.sort();
//取value
var kv = {};
var _tamp = "";
for (let k of keys) {
if (k != "timestamp" && k != "biz_content") {
kv[k] = param[k];
} else {
// _tamp = encodeURIComponent(param[k]);
// _tamp = _tamp.replace(/\(/, '%28');
// _tamp = _tamp.replace(/\)/, '%29');
kv[k] = encodeURIComponent(param[k]); //urlencode编码
}
}
var sign = $.signRule(kv, signkey);
// 追加到请求参数中
_toolsUnit.autoSignAttach(sign);
return sign;
};
return new _signUnit(signkey);
};
// $.signRule方法定义SIGN生成规则,根据项目需要,修改业务需要规则
// 调用示例
let _signKey = apt.variables.get("sign_key");
// 请求时追加到参数中
$.sign(_signKey,'request').create();
// 设置到环境变量中
$.sign(_signKey,'variable','sign').create();
- 若是,设置到环境中,在
目录公用Body
或目录公用Query
增加一条字段属性
保存后,该目录下的请求会自动生成对应请求sign值,并通过{{sign}}变量同其他参数一同发给后端