hunshcn
2022-11-11 5539190c3975bb0fc9b9c56d3b1662db79861a1a
提交 | 用户 | age
e12c58 1 import type { RouteLocationNormalized, RouteRecordNormalized } from 'vue-router';
55e9d9 2 import type { App, Plugin } from 'vue';
V 3
a305e5 4 import { unref } from 'vue';
498278 5 import { isObject } from '/@/utils/is';
df0e0c 6 import { cloneDeep } from 'lodash-es';
e12c58 7
35d2bf 8 export const noop = () => {};
8a9ca4 9
2f6253 10 /**
11  * @description:  Set ui mount node
12  */
13 export function getPopupContainer(node?: HTMLElement): HTMLElement {
116a1f 14   return (node?.parentNode as HTMLElement) ?? document.body;
2f6253 15 }
ba068b 16
2f6253 17 /**
18  * Add the object as a parameter to the URL
19  * @param baseUrl url
20  * @param obj
21  * @returns {string}
22  * eg:
23  *  let obj = {a: '3', b: '4'}
24  *  setObjToUrlParams('www.baidu.com', obj)
25  *  ==>www.baidu.com?a=3&b=4
26  */
27 export function setObjToUrlParams(baseUrl: string, obj: any): string {
28   let parameters = '';
29   for (const key in obj) {
30     parameters += key + '=' + encodeURIComponent(obj[key]) + '&';
31   }
32   parameters = parameters.replace(/&$/, '');
da0491 33   return /\?$/.test(baseUrl) ? baseUrl + parameters : baseUrl.replace(/\/?$/, '?') + parameters;
2f6253 34 }
35
19dc88 36 // 深度合并
4ff1c4 37 export function deepMerge<T = any>(src: any = {}, target: any = {}): T {
2f6253 38   let key: string;
df0e0c 39   const res: any = cloneDeep(src)
2f6253 40   for (key in target) {
df0e0c 41     res[key] = isObject(res[key]) ? deepMerge(res[key], target[key]) : (res[key] = target[key]);
2f6253 42   }
df0e0c 43   return res;
2f6253 44 }
45
ba068b 46 export function openWindow(
V 47   url: string,
56a966 48   opt?: { target?: TargetContext | string; noopener?: boolean; noreferrer?: boolean },
ba068b 49 ) {
V 50   const { target = '__blank', noopener = true, noreferrer = true } = opt || {};
51   const feature: string[] = [];
52
53   noopener && feature.push('noopener=yes');
54   noreferrer && feature.push('noreferrer=yes');
55
56   window.open(url, target, feature.join(','));
57 }
a305e5 58
V 59 // dynamic use hook props
60 export function getDynamicProps<T, U>(props: T): Partial<U> {
61   const ret: Recordable = {};
62
63   Object.keys(props).map((key) => {
64     ret[key] = unref((props as Recordable)[key]);
65   });
66
67   return ret as Partial<U>;
144ab5 68 }
581007 69
e12c58 70 export function getRawRoute(route: RouteLocationNormalized): RouteLocationNormalized {
V 71   if (!route) return route;
72   const { matched, ...opt } = route;
73   return {
74     ...opt,
75     matched: (matched
76       ? matched.map((item) => ({
77           meta: item.meta,
78           name: item.name,
79           path: item.path,
80         }))
81       : undefined) as RouteRecordNormalized[],
82   };
83 }
55e9d9 84
V 85 export const withInstall = <T>(component: T, alias?: string) => {
86   const comp = component as any;
87   comp.install = (app: App) => {
88     app.component(comp.name || comp.displayName, component);
89     if (alias) {
90       app.config.globalProperties[alias] = component;
91     }
92   };
93   return component as T & Plugin;
94 };