Blame view

src/utils/index.ts 2.98 KB
1
import type { RouteLocationNormalized, RouteRecordNormalized } from 'vue-router';
luocong2016 authored
2
import type { App, Component } from 'vue';
3
vben authored
4
import { unref } from 'vue';
5
import { isObject } from '/@/utils/is';
6
import { cloneDeep } from 'lodash-es';
7
vben authored
8
export const noop = () => {};
Vben authored
9
陈文彬 authored
10
11
12
13
/**
 * @description:  Set ui mount node
 */
export function getPopupContainer(node?: HTMLElement): HTMLElement {
14
  return (node?.parentNode as HTMLElement) ?? document.body;
陈文彬 authored
15
}
vben authored
16
陈文彬 authored
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
 * 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(/&$/, '');
33
  return /\?$/.test(baseUrl) ? baseUrl + parameters : baseUrl.replace(/\/?$/, '?') + parameters;
陈文彬 authored
34
35
}
36
// 深度合并
vben authored
37
export function deepMerge<T = any>(src: any = {}, target: any = {}): T {
陈文彬 authored
38
  let key: string;
39
  const res: any = cloneDeep(src);
陈文彬 authored
40
  for (key in target) {
41
    res[key] = isObject(res[key]) ? deepMerge(res[key], target[key]) : (res[key] = target[key]);
陈文彬 authored
42
  }
43
  return res;
陈文彬 authored
44
45
}
vben authored
46
47
export function openWindow(
  url: string,
vben authored
48
  opt?: { target?: TargetContext | string; noopener?: boolean; noreferrer?: boolean },
vben authored
49
50
51
52
53
54
55
56
57
) {
  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(','));
}
vben authored
58
59

// dynamic use hook props
luocong2016 authored
60
export function getDynamicProps<T extends Record<string, unknown>, U>(props: T): Partial<U> {
vben authored
61
62
63
64
65
66
67
68
  const ret: Recordable = {};

  Object.keys(props).map((key) => {
    ret[key] = unref((props as Recordable)[key]);
  });

  return ret as Partial<U>;
}
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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[],
  };
}
84
luocong2016 authored
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// https://github.com/vant-ui/vant/issues/8302
type EventShim = {
  new (...args: any[]): {
    $props: {
      onClick?: (...args: any[]) => void;
    };
  };
};

export type WithInstall<T> = T & {
  install(app: App): void;
} & EventShim;

export type CustomComponent = Component & { displayName?: string };

export const withInstall = <T extends CustomComponent>(component: T, alias?: string) => {
  (component as Record<string, unknown>).install = (app: App) => {
    const compName = component.name || component.displayName;
    if (!compName) return;
    app.component(compName, component);
105
106
107
108
    if (alias) {
      app.config.globalProperties[alias] = component;
    }
  };
luocong2016 authored
109
  return component as WithInstall<T>;
110
};