import type { RouteLocationNormalized, RouteRecordNormalized } from 'vue-router'; import type { App, Component } from 'vue'; import { intersectionWith, isEqual, mergeWith, unionWith } from 'lodash-es'; import { unref } from 'vue'; import { isArray, isObject } from '@voya-kit/utils'; export const noop = () => {}; /** * @description: Set ui mount node */ export function getPopupContainer(node?: HTMLElement): HTMLElement { return (node?.parentNode as HTMLElement) ?? document.body; } /** * Add the object as a parameter to the URL * @param baseUrl url * @param obj * @returns {string} * eg: * let obj = {a: '3', b: '4'} * setObjToUrlParams('www.baidu.com', obj) * ==>www.baidu.com?a=3&b=4 */ export function setObjToUrlParams(baseUrl: string, obj: any): string { let parameters = ''; for (const key in obj) { parameters += key + '=' + encodeURIComponent(obj[key]) + '&'; } parameters = parameters.replace(/&$/, ''); return /\?$/.test(baseUrl) ? baseUrl + parameters : baseUrl.replace(/\/?$/, '?') + parameters; } /** * Recursively merge two objects. * 递归合并两个对象。 * * @param source The source object to merge from. 要合并的源对象。 * @param target The target object to merge into. 目标对象,合并后结果存放于此。 * @param mergeArrays How to merge arrays. Default is "replace". * 如何合并数组。默认为replace。 * - "union": Union the arrays. 对数组执行并集操作。 * - "intersection": Intersect the arrays. 对数组执行交集操作。 * - "concat": Concatenate the arrays. 连接数组。 * - "replace": Replace the source array with the target array. 用目标数组替换源数组。 * @returns The merged object. 合并后的对象。 */ export function deepMerge( source: T, target: U, mergeArrays: 'union' | 'intersection' | 'concat' | 'replace' = 'replace', ): T & U { if (!target) { return source as T & U; } if (!source) { return target as T & U; } return mergeWith({}, source, target, (sourceValue, targetValue) => { if (isArray(targetValue) && isArray(sourceValue)) { switch (mergeArrays) { case 'union': return unionWith(sourceValue, targetValue, isEqual); case 'intersection': return intersectionWith(sourceValue, targetValue, isEqual); case 'concat': return sourceValue.concat(targetValue); case 'replace': return targetValue; default: throw new Error(`Unknown merge array strategy: ${mergeArrays as string}`); } } if (isObject(targetValue) && isObject(sourceValue)) { return deepMerge(sourceValue, targetValue, mergeArrays); } return undefined; }); } export function openWindow( url: string, opt?: { target?: TargetContext | string; noopener?: boolean; noreferrer?: boolean }, ) { const { target = '__blank', noopener = true, noreferrer = true } = opt || {}; const feature: string[] = []; noopener && feature.push('noopener=yes'); noreferrer && feature.push('noreferrer=yes'); window.open(url, target, feature.join(',')); } export function openWindowByA(url: string, opt?: { target?: TargetContext | string }) { const { target = '_blank' } = opt || {}; const aTag = document.createElement('a'); aTag.href = url; aTag.target = target || '_blank'; aTag.click(); } // dynamic use hook props export function getDynamicProps, U>(props: T): Partial { const ret: Recordable = {}; Object.keys(props).map((key) => { ret[key] = unref((props as Recordable)[key]); }); return ret as Partial; } export function getRawRoute(route: RouteLocationNormalized): RouteLocationNormalized { if (!route) return route; const { matched, ...opt } = route; return { ...opt, matched: (matched ? matched.map((item) => ({ meta: item.meta, name: item.name, path: item.path, })) : undefined) as RouteRecordNormalized[], }; } // https://github.com/vant-ui/vant/issues/8302 type EventShim = { new (...args: any[]): { $props: { onClick?: (...args: any[]) => void; }; }; }; export type WithInstall = T & { install(app: App): void; } & EventShim; export type CustomComponent = Component & { displayName?: string }; export const withInstall = (component: T, alias?: string) => { (component as Record).install = (app: App) => { const compName = component.name || component.displayName; if (!compName) return; app.component(compName, component); if (alias) { app.config.globalProperties[alias] = component; } }; return component as WithInstall; }; // 手机邮箱脱敏 export function maskSensitiveInfo(str: string): string { // 正则表达式匹配手机号 const phoneRegex = /(\d{3})\d{4}(\d{4})/; // 正则表达式匹配邮箱 const emailRegex = /(\w{2})\w+@(\w+)\.(\w+)/; // 如果是手机号 if (phoneRegex.test(str)) { return str.replace(phoneRegex, '$1****$2'); } // 如果是邮箱 else if (emailRegex.test(str)) { return str.replace(emailRegex, '$1****@$2.$3'); } // 如果不是手机号或邮箱,则直接返回原字符串 else { return str; } } // 获取文本占页面的长度 export function getTextWidth(text) { const span = document.createElement('span'); span.style.visibility = 'hidden'; span.style.whiteSpace = 'nowrap'; span.style.position = 'absolute'; span.innerHTML = text; document.body.appendChild(span); const width = span.offsetWidth; document.body.removeChild(span); return width; } export function deepEqual(a: T, b: any): boolean { if (typeof a !== 'object' || a === null || typeof b !== 'object' || b === null) { return a === b; } const aIsArray = Array.isArray(a); const bIsArray = Array.isArray(b); if (aIsArray !== bIsArray) { return false; } if (aIsArray) { if (a.length !== b.length) { return false; } for (let i = 0; i < a.length; i++) { if (!deepEqual(a[i], b[i])) { return false; } } return true; } const aKeys = Reflect.ownKeys(a) as (string | symbol)[]; const bKeys = Reflect.ownKeys(b) as (string | symbol)[]; if (aKeys.length !== bKeys.length) { return false; } for (const key of aKeys) { const aValue = Reflect.get(a, key); const bValue = Reflect.get(b, key); if (!deepEqual(aValue, bValue)) { return false; } } return true; } // 深度比较两个对象,获取不同的属性及其值 export const diffTwoObj = (obj1: any, obj2: any) => { const result: any[] = []; const keys = new Set([...Object.keys(obj1), ...Object.keys(obj2)]); for (const key of keys) { if (obj1[key] === 'null') { obj1[key] = null; } if (obj2[key] === 'null') { obj2[key] = null; } if (obj1[key] === 'undefined') { obj1[key] = undefined; } if (obj2[key] === 'undefined') { obj2[key] = undefined; } result.push({ key, value1: obj1[key], value2: obj2[key], isDiff: !deepEqual(obj1[key], obj2[key]), }); } result.sort((a, b): number => { if (a.isDiff && b.isDiff) { return 0; } if (a.isDiff) { return -1; } if (b.isDiff) { return 1; } return 0; }); return result; }; export function recursiveDecode(str: string) { // 正则表达式匹配&amp; const pattern = /&amp;/g; // 检查字符串中是否还存在需要解码的模式 if (pattern.test(str)) { // 替换并递归调用直到没有匹配项为止 return recursiveDecode(str.replace(pattern, '&')); } else { // 当没有更多&amp;时,使用DOMParser解码剩余的HTML实体 const parser = new DOMParser(); return parser.parseFromString('
' + str + '
', 'text/html').body.textContent; } } export function bytesToGigabytes(bytes: number | string): number { if (typeof bytes === 'string') { bytes = Number(bytes) || 0; } const gigabyte = 1024 * 1024 * 1024; // 1 GB in bytes return parseFloat((bytes / gigabyte).toFixed(4)); } export function gigabytesToBytes(gigabytes: number | string): number { if (typeof gigabytes === 'string') { gigabytes = Number(gigabytes) || 0; } const gigabyte = 1024 * 1024 * 1024; // 1 GB in bytes return gigabytes * gigabyte; }