import Vue from "vue"; import router from "@/router"; import store from "@/store"; /** * 获取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 {*} key */ export function isAuth(key) { return ( JSON.parse(sessionStorage.getItem("permissions") || "[]").indexOf(key) !== -1 || false ); } /** * 树形数据转换 * @param {*} data * @param {*} id * @param {*} pid */ 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; temp[data[k][pid]]["children"].push(data[k]); } else { res.push(data[k]); } } return res; } /** * 清除登录信息 */ export function clearLoginInfo() { Vue.cookie.delete("token"); store.commit("resetStore"); sessionStorage.removeItem("password"); sessionStorage.removeItem("role"); sessionStorage.removeItem("userInfo"); router.options.isAddDynamicMenuRoutes = false; } // 深拷贝对象 export function deepClone(obj) { const _toString = Object.prototype.toString; // null, undefined, non-object, function if (!obj || typeof obj !== "object") { return obj; } // DOM Node if (obj.nodeType && "cloneNode" in obj) { return obj.cloneNode(true); } // Date if (_toString.call(obj) === "[object Date]") { return new Date(obj.getTime()); } // RegExp if (_toString.call(obj) === "[object RegExp]") { const flags = []; if (obj.global) { flags.push("g"); } if (obj.multiline) { flags.push("m"); } if (obj.ignoreCase) { flags.push("i"); } return new RegExp(obj.source, flags.join("")); } const result = Array.isArray(obj) ? [] : obj.constructor ? new obj.constructor() : {}; for (const key in obj) { result[key] = deepClone(obj[key]); } return result; } /** * 获取时间 * @param {*} min * @param {*} max */ export function getDay(day) { let today = new Date(); let targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day; today.setTime(targetday_milliseconds); //注意,这行是关键代码 let tYear = today.getFullYear(); let tMonth = today.getMonth(); let tDate = today.getDate(); tMonth = doHandleMonth(tMonth + 1); tDate = doHandleMonth(tDate); return tYear + "-" + tMonth + "-" + tDate; } function doHandleMonth(month) { let m = month; if (month.toString().length === 1) { m = "0" + month; } return m; } export function isNumberStr(str) { return /^[+-]?(0|([1-9]\d*))(\.\d+)?$/g.test(str); }