You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tanghe-report/report-ui/src/utils/signUtil.js

109 lines
2.8 KiB
JavaScript

3 years ago
import md5 from 'js-md5';
function jsonString (obj) {
if (isObject(obj)) {
return sortObjByKey(obj);
} else if (isArray(obj)) {
3 years ago
let sortArray = [];
for (let i = 0; i < obj.length; i++) {
3 years ago
if (isObject(obj[i])) {
sortArray.push(sortObjByKey(obj[i]));
} else {
return obj;
}
}
return sortArray;
} else {
return obj;
}
}
// 将通讯录按照 ABCD字母的顺序排序
function sortObjByKey (obj) {
3 years ago
let keys = Object.keys(obj).sort();
let newObj = {}
for (let i = 0; i < keys.length; i++) {
let index = keys[i];
3 years ago
newObj[index] = jsonString(obj[index]);
}
return newObj;
}
//判断是否是数组
function isArray (obj) {
return Object.prototype.toString.call(obj) === '[object Array]';
}
//判断是否是对象
function isObject (obj) {
return Object.prototype.toString.call(obj) === '[object Object]';
}
export default {
/**
* 第一步将data中参数按key升序排除null值&拼接得tempSignStr
* 第二步"time=%s&" + tempSignStr + "&token=%s"
* 第三步md5(第二步结果)
*/
sign: function (token, data) {
if (typeof (data) == "undefined" || data == null) {
data = {};
}
3 years ago
let gatewayRequest = {};//currentPage pageSize orderBy data time token sign isFrom
let time = new Date().getTime();
3 years ago
//找出data中值非null的key同时排除分页参数
//ignoreKeyList data中哪些key不参与签名
3 years ago
let dataKeyArray = [];
let excludeKeyArray = ['currentPage', 'pageSize', 'orderBy', 'ignoreKeyList'];
for (let key in data) {
3 years ago
if (data[key] == null) {
continue;
}
if (excludeKeyArray.indexOf(key) >= 0) {
gatewayRequest[key] = data[key];
delete data[key];
if (key === 'ignoreKeyList') {
//遍历ignoreKeyList不参与签名校验
3 years ago
for (let i = 0; i < data[key].length; i++) {
3 years ago
delete data[data[key][i]];
}
}
continue;
}
dataKeyArray.push(key);
}
//将key按升序排列
dataKeyArray.sort();
//生成签名串"time=%s&" + tempSignStr + "&token=%s"
3 years ago
let tempSignStr = "time=" + time;
for (let i = 0; i < dataKeyArray.length; i++) {
let key = dataKeyArray[i];
let value = data[key];
3 years ago
tempSignStr += "&";
tempSignStr += key;
tempSignStr += "=";
tempSignStr += JSON.stringify(jsonString(value));
}
tempSignStr += "&token=";
tempSignStr += token;
3 years ago
let signStr = md5(tempSignStr);
3 years ago
gatewayRequest['data'] = data;
gatewayRequest['time'] = time;
gatewayRequest['token'] = token;
gatewayRequest['sign'] = signStr;
gatewayRequest['isFrom'] = "PC";
return gatewayRequest;
}
}
/**
* 密码加盐后MD5
* @param {HTMLElement} elm
*
*/
export function transPsw (val) {
return md5(val + 'gaea')
}