mirror of
https://gitee.com/orangeform/orange-admin.git
synced 2026-01-17 10:36:31 +08:00
223 lines
6.5 KiB
JavaScript
223 lines
6.5 KiB
JavaScript
import JSEncrypt from 'jsencrypt';
|
||
|
||
/**
|
||
* 列表数据转换树形数据
|
||
* @param {Array} data 要转换的列表
|
||
* @param {String} id 主键字段字段名
|
||
* @param {String} pid 父字段字段名
|
||
* @returns {Array} 转换后的树数据
|
||
*/
|
||
export function treeDataTranslate (data, id = 'id', pid = 'parentId') {
|
||
var res = []
|
||
var temp = {}
|
||
for (var i = 0; i < data.length; i++) {
|
||
temp[data[i][id]] = data[i]
|
||
}
|
||
for (var k = 0; k < data.length; k++) {
|
||
if (temp[data[k][pid]] && data[k][id] !== data[k][pid]) {
|
||
if (!temp[data[k][pid]]['children']) {
|
||
temp[data[k][pid]]['children'] = []
|
||
}
|
||
if (!temp[data[k][pid]]['_level']) {
|
||
temp[data[k][pid]]['_level'] = 1
|
||
}
|
||
data[k]['_level'] = temp[data[k][pid]]._level + 1
|
||
data[k]['_parent'] = data[k][pid]
|
||
temp[data[k][pid]]['children'].push(data[k])
|
||
} else {
|
||
res.push(data[k])
|
||
}
|
||
}
|
||
|
||
return res
|
||
}
|
||
/**
|
||
* 获取字符串字节长度(中文算2个字符)
|
||
* @param {String} str 要获取长度的字符串
|
||
*/
|
||
export function getStringLength (str) {
|
||
return str.replace(/[\u4e00-\u9fa5\uff00-\uffff]/g, '**').length
|
||
}
|
||
/**
|
||
* 获取uuid
|
||
*/
|
||
export function getUUID () {
|
||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
|
||
return (c === 'x' ? (Math.random() * 16 | 0) : ('r&0x3' | '0x8')).toString(16)
|
||
});
|
||
}
|
||
/**
|
||
* 大小驼峰变换函数
|
||
* @param name 要转换的字符串
|
||
* @param type 转换的类型0:转换成小驼峰,1:转换成大驼峰
|
||
*/
|
||
export function nameTranslate (name, type) {
|
||
name = name.toLowerCase();
|
||
let nameArray = name.split('_');
|
||
nameArray.forEach((item, index) => {
|
||
if (index === 0) {
|
||
name = type === 1 ? item.slice(0, 1).toUpperCase() + item.slice(1) : item;
|
||
} else {
|
||
name = name + item.slice(0, 1).toUpperCase() + item.slice(1);
|
||
}
|
||
});
|
||
|
||
nameArray = name.split('-');
|
||
nameArray.forEach((item, index) => {
|
||
if (index === 0) {
|
||
name = type === 1 ? item.slice(0, 1).toUpperCase() + item.slice(1) : item;
|
||
} else {
|
||
name = name + item.slice(0, 1).toUpperCase() + item.slice(1);
|
||
}
|
||
});
|
||
return name;
|
||
}
|
||
/**
|
||
* 通过id从树中获取指定的节点
|
||
* @param {Object} node 根节点
|
||
* @param {String|Nubmer} id 键值
|
||
* @param {Array} list 保存查询路径
|
||
* @param {String} idKey 主键字段名
|
||
* @param {String} childKey 子节点字段名
|
||
*/
|
||
function findNode (node, id, list, idKey = 'id', childKey = 'children') {
|
||
if (Array.isArray(list)) list.push(node[idKey]);
|
||
if (node[idKey] === id) {
|
||
return node;
|
||
}
|
||
|
||
if (node[childKey] != null && Array.isArray(node[childKey])) {
|
||
for (let i = 0; i < node[childKey].length; i++) {
|
||
let tempNode = findNode(node[childKey][i], id, list, idKey, childKey);
|
||
if (tempNode) return tempNode;
|
||
}
|
||
}
|
||
|
||
if (Array.isArray(list)) list.pop();
|
||
}
|
||
/**
|
||
* 通过id返回从根节点到指定节点的路径
|
||
* @param {Array} treeRoot 树根节点数组
|
||
* @param {*} id 要查询的节点的id
|
||
* @param {*} idKey 主键字段名
|
||
* @param {*} childKey 子节点字段名
|
||
*/
|
||
export function findTreeNodePath (treeRoot, id, idKey = 'id', childKey = 'children') {
|
||
let tempList = [];
|
||
for (let i = 0; i < treeRoot.length; i++) {
|
||
if (findNode(treeRoot[i], id, tempList, idKey, childKey)) {
|
||
return tempList;
|
||
}
|
||
}
|
||
|
||
return [];
|
||
}
|
||
/**
|
||
* 通过id从树中查找节点
|
||
* @param {Array} treeRoot 根节点数组
|
||
* @param {*} id 要查找的节点的id
|
||
* @param {*} idKey 主键字段名
|
||
* @param {*} childKey 子节点字段名
|
||
*/
|
||
export function findTreeNode (treeRoot, id, idKey = 'id', childKey = 'children') {
|
||
for (let i = 0; i < treeRoot.length; i++) {
|
||
let tempNode = findNode(treeRoot[i], id, undefined, idKey, childKey);
|
||
if (tempNode) return tempNode;
|
||
}
|
||
}
|
||
/**
|
||
* 把Object转换成query字符串
|
||
* @param {Object} params 要转换的Object
|
||
*/
|
||
export function objectToQueryString (params) {
|
||
if (params == null) {
|
||
return null;
|
||
} else {
|
||
return Object.keys(params).map((key) => {
|
||
return `${key}=${params[key]}`;
|
||
}).join('&');
|
||
}
|
||
}
|
||
/**
|
||
* 从数组中查找某一项
|
||
* @param {Array} list 要查找的数组
|
||
* @param {String} id 要查找的节点id
|
||
* @param {String} idKey 主键字段名(如果为null则直接比较)
|
||
* @param {Boolean} removeItem 是否从数组中移除查找到的节点
|
||
* @returns {Object} 找到返回节点,没找到返回undefined
|
||
*/
|
||
export function findItemFromList (list, id, idKey, removeItem = false) {
|
||
if (Array.isArray(list) && list.length > 0 && id != null) {
|
||
for (let i = 0; i < list.length; i++) {
|
||
let item = list[i];
|
||
if (((idKey == null || idKey === '') && item === id) || (idKey != null && item[idKey] === id)) {
|
||
if (removeItem) list.splice(i, 1);
|
||
return item;
|
||
}
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
/**
|
||
* 将数据保存到sessionStorage
|
||
* @param {*} key sessionStorage的键值
|
||
* @param {*} value 要保存的数据
|
||
*/
|
||
export function setObjectToSessionStorage (key, value) {
|
||
if (key == null || key === '') return false;
|
||
if (value == null) {
|
||
window.sessionStorage.removeItem(key);
|
||
return true;
|
||
} else {
|
||
let jsonObj = {
|
||
data: value
|
||
}
|
||
window.sessionStorage.setItem(key, JSON.stringify(jsonObj));
|
||
return true;
|
||
}
|
||
}
|
||
/**
|
||
* 从sessionStorage里面获取数据
|
||
* @param {String} key 键值
|
||
* @param {*} defaultValue 默认值
|
||
*/
|
||
export function getObjectFromSessionStorage (key, defaultValue) {
|
||
let jsonObj = null;
|
||
try {
|
||
jsonObj = JSON.parse(window.sessionStorage.getItem(key));
|
||
jsonObj = (jsonObj || {}).data;
|
||
} catch (e) {
|
||
jsonObj = defaultValue;
|
||
};
|
||
return (jsonObj != null) ? jsonObj : defaultValue;
|
||
}
|
||
/**
|
||
* 判读字符串是否一个数字
|
||
* @param {String} str 要判断的字符串
|
||
*/
|
||
export function isNumber (str) {
|
||
let num = Number.parseFloat(str);
|
||
if (Number.isNaN(num)) return false;
|
||
return num.toString() === str;
|
||
}
|
||
/**
|
||
* 生成随机数
|
||
* @param {Integer} min 随机数最小值
|
||
* @param {Integer} max 随机数最大值
|
||
*/
|
||
export function random (min, max) {
|
||
let base = Math.random();
|
||
return min + base * (max - min);
|
||
}
|
||
/**
|
||
* 加密
|
||
* @param {*} value 要加密的字符串
|
||
*/
|
||
const publicKey = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCpC4QMnbTrQOFriJJCCFFWhlruBJThAEBfRk7pRx1jsAhyNVL3CqJb0tRvpnbCnJhrRAEPdgFHXv5A0RrvFp+5Cw7QoFH6O9rKB8+0H7+aVQeKITMUHf/XMXioymw6Iq4QfWd8RhdtM1KM6eGTy8aU7SO2s69Mc1LXefg/x3yw6wIDAQAB';
|
||
export function encrypt (value) {
|
||
if (value == null || value === '') return null;
|
||
let encrypt = new JSEncrypt();
|
||
encrypt.setPublicKey(publicKey);
|
||
return encodeURIComponent(encrypt.encrypt(value));
|
||
}
|