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

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import md5 from 'js-md5';
function jsonString (obj) {
if (isObject(obj)) {
return sortObjByKey(obj);
} else if (isArray(obj)) {
let sortArray = [];
for (let i = 0; i < obj.length; i++) {
if (isObject(obj[i])) {
sortArray.push(sortObjByKey(obj[i]));
} else {
return obj;
}
}
return sortArray;
} else {
return obj;
}
}
// 将通讯录按照 ABCD字母的顺序排序
function sortObjByKey (obj) {
let keys = Object.keys(obj).sort();
let newObj = {}
for (let i = 0; i < keys.length; i++) {
let index = keys[i];
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 = {};
}
let gatewayRequest = {};//currentPage pageSize orderBy data time token sign isFrom
let time = new Date().getTime();
//找出data中值非null的key同时排除分页参数
//ignoreKeyList data中哪些key不参与签名
let dataKeyArray = [];
let excludeKeyArray = ['currentPage', 'pageSize', 'orderBy', 'ignoreKeyList'];
for (let key in data) {
if (data[key] == null) {
continue;
}
if (excludeKeyArray.indexOf(key) >= 0) {
gatewayRequest[key] = data[key];
delete data[key];
if (key === 'ignoreKeyList') {
//遍历ignoreKeyList不参与签名校验
for (let i = 0; i < data[key].length; i++) {
delete data[data[key][i]];
}
}
continue;
}
dataKeyArray.push(key);
}
//将key按升序排列
dataKeyArray.sort();
//生成签名串"time=%s&" + tempSignStr + "&token=%s"
let tempSignStr = "time=" + time;
for (let i = 0; i < dataKeyArray.length; i++) {
let key = dataKeyArray[i];
let value = data[key];
tempSignStr += "&";
tempSignStr += key;
tempSignStr += "=";
tempSignStr += JSON.stringify(jsonString(value));
}
tempSignStr += "&token=";
tempSignStr += token;
let signStr = md5(tempSignStr);
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')
}