import { isDate } from 'date-fns'; import { AES, mode, pad, enc } from "crypto-js"; /** * @description 判断是否是时间戳 * @param {number} 时间戳 * @return {boolean} */ export const isTimestamp = (value) => { // 判断是否为数字 if (typeof value !== 'number') return false; // 判断是否为合理的时间戳(大于0) if (value <= 0) return false; // 判断是否为Math.floor(value),即是否为整数 if (value !== Math.floor(value)) return false; // 判断是否为日期对象 return isDate(new Date(value)); } /** * @description 生成连续的数字 * @param {number} start 起点数字 * @param {number} end 终点数字 * @return {array} */ export const generateNumberArray = (start, end) => { return Array.from(new Array(end + 1).keys()).slice(start); } /** * @description 登录加密 * @param {string} str 待加密的字符串 * @return {string} */ export const encryptByEnAESLogin = (str) => { str = enc.Utf8.parse(str); let Key = enc.Utf8.parse('Aes2Util666AQWER'); let tmpAES = AES.encrypt(str, Key, { mode: mode.ECB, padding: pad.Pkcs7, }); return tmpAES.toString(); } // 处理树数据(parent格式转为children格式) export const transform = (nodes, id, parentid) => { let parents = []; const idMapping = nodes.reduce((acc, el, i) => { acc[el[id]] = i; return acc; }, {}); nodes.forEach((el) => { if (el[parentid] === null || el[parentid] === undefined) { parents.push(el); } else { const parentEl = nodes[idMapping[el[parentid]]]; parentEl.children = [...(parentEl.children || []), el]; } }); return parents; }