/* eslint-disable @typescript-eslint/no-explicit-any */ import { Menu } from "./router-new"; export function getUrlTicket(): string | undefined { const r = window.location.search.substring(1).match(/ticket=([^?&#]*)/g); if (r !== null && r.length > 0) { const ticketStr = r.pop()?.replace("ticket=", ""); if (ticketStr !== undefined) { return decodeURI(ticketStr); } } } /** * 树转成数组 * @param node 树 * @returns */ export function treeToArray(node?: Menu[]): Menu[] { if (!node) { return []; } // 遍历数组 return node.reduce((res: Menu[], item) => { // 合并新数组并递归子数据 return res.concat(item, treeToArray(item.childs)); }, []); } /** * 获取url?后面的参数 * @param url * @returns */ export function getParams(url: string) { const params: Record = {}; const urlSearchParams = new URLSearchParams(url.split("?")[1]); urlSearchParams.forEach(function (value, key) { params[key] = value; }); return params; } /** * 字符串前面追加/ * @param str * @returns */ export function addLeadingSlash(str: string): string { if (!str.startsWith("/")) { return `/${str}`; } return str; } /** * 将json对象中的下划线转为驼峰 * @param obj * @returns */ export function convertKeysToCamelCase( obj: Record ): Record { if (typeof obj !== "object" || obj === null) { return obj; } if (Array.isArray(obj)) { return obj.map((item) => convertKeysToCamelCase(item)); } const camelCaseObject: Record = {}; for (const key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { const camelCaseKey = key.replace(/_([a-z])/g, (_, match) => match.toUpperCase() ); camelCaseObject[camelCaseKey] = convertKeysToCamelCase(obj[key]); } } return camelCaseObject; }