import enquireJs from "enquire.js"; import CryptoJS from "crypto-js"; export function isDef(v) { return v !== undefined && v !== null; } /** * Remove an item from an array. */ export function remove(arr, item) { if (arr.length) { const index = arr.indexOf(item); if (index > -1) { return arr.splice(index, 1); } } } export function isRegExp(v) { return _toString.call(v) === "[object RegExp]"; } export function enquireScreen(call) { const handler = { match: function() { call && call(true); }, unmatch: function() { call && call(false); }, }; enquireJs.register("only screen and (max-width: 767.99px)", handler); } const _toString = Object.prototype.toString; // 扁平化树形结构 export const extractTree = (arrs, childs, attrArr) => { let attrList = []; if (!Array.isArray(arrs) && !arrs.length) return []; if (typeof childs !== "string") return []; if (!Array.isArray(attrArr) || (Array.isArray(attrArr) && !attrArr.length)) { attrList = Object.keys(arrs[0]); attrList.splice(attrList.indexOf(childs), 1); attrList.splice(attrList.indexOf("isLeaf"), 1); } else { attrList = attrArr; } let list = []; const getObj = (arr) => { arr.forEach(function(row) { let obj = {}; attrList.forEach((item) => { obj[item] = row[item]; }); list.push(obj); if (row[childs]) { getObj(row[childs]); } }); return list; }; return getObj(arrs); }; /** * 加密存储临时数据并解析对象 */ const aseKey = "**_FXxx_1234_KEY"; const KEY = "KEY_EXTRA"; export class SessionCrypto { // 加密 static setItem(key = KEY, value = "") { if (typeof key === "string") { const stringify = JSON.stringify(value); const encrypt = CryptoJS.AES.encrypt( stringify, CryptoJS.enc.Utf8.parse(aseKey), { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7, } ).toString(); window.sessionStorage.setItem(key, encrypt); return encrypt; } } // 解密 static getItem(key = KEY) { const ssStr = window.sessionStorage.getItem(key) || ""; try { if (ssStr) { const decrypt = CryptoJS.AES.decrypt( ssStr, CryptoJS.enc.Utf8.parse(aseKey), { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7, } ).toString(CryptoJS.enc.Utf8); const parseStr = JSON.parse(decrypt); return parseStr; } return ""; } catch (e) { return ""; } } // 删除 static remove(key) { window.sessionStorage.removeItem(key); } }