1
0

index.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import type { RouteLocationNormalized, RouteRecordNormalized } from 'vue-router';
  2. import type { App, Component } from 'vue';
  3. import { unref } from 'vue';
  4. import { isArray, isObject } from '/@/utils/is';
  5. import { cloneDeep, isEqual, mergeWith, unionWith } from 'lodash-es';
  6. export const noop = () => {};
  7. /**
  8. * @description: Set ui mount node
  9. */
  10. export function getPopupContainer(node?: HTMLElement): HTMLElement {
  11. return (node?.parentNode as HTMLElement) ?? document.body;
  12. }
  13. /**
  14. * Add the object as a parameter to the URL
  15. * @param baseUrl url
  16. * @param obj
  17. * @returns {string}
  18. * eg:
  19. * let obj = {a: '3', b: '4'}
  20. * setObjToUrlParams('www.baidu.com', obj)
  21. * ==>www.baidu.com?a=3&b=4
  22. */
  23. export function setObjToUrlParams(baseUrl: string, obj: any): string {
  24. let parameters = '';
  25. for (const key in obj) {
  26. parameters += key + '=' + encodeURIComponent(obj[key]) + '&';
  27. }
  28. parameters = parameters.replace(/&$/, '');
  29. return /\?$/.test(baseUrl) ? baseUrl + parameters : baseUrl.replace(/\/?$/, '?') + parameters;
  30. }
  31. /**
  32. 递归合并两个对象。
  33. Recursively merge two objects.
  34. @param target 目标对象,合并后结果存放于此。The target object to merge into.
  35. @param source 要合并的源对象。The source object to merge from.
  36. @returns 合并后的对象。The merged object.
  37. */
  38. export function deepMerge<T extends object | null | undefined, U extends object | null | undefined>(
  39. target: T,
  40. source: U,
  41. ): T & U {
  42. return mergeWith(cloneDeep(target), source, (objValue, srcValue) => {
  43. if (isObject(objValue) && isObject(srcValue)) {
  44. return mergeWith(cloneDeep(objValue), srcValue, (prevValue, nextValue) => {
  45. // 如果是数组,合并数组(去重) If it is an array, merge the array (remove duplicates)
  46. return isArray(prevValue) ? unionWith(prevValue, nextValue, isEqual) : undefined;
  47. });
  48. }
  49. });
  50. }
  51. export function openWindow(
  52. url: string,
  53. opt?: { target?: TargetContext | string; noopener?: boolean; noreferrer?: boolean },
  54. ) {
  55. const { target = '__blank', noopener = true, noreferrer = true } = opt || {};
  56. const feature: string[] = [];
  57. noopener && feature.push('noopener=yes');
  58. noreferrer && feature.push('noreferrer=yes');
  59. window.open(url, target, feature.join(','));
  60. }
  61. // dynamic use hook props
  62. export function getDynamicProps<T extends Record<string, unknown>, U>(props: T): Partial<U> {
  63. const ret: Recordable = {};
  64. Object.keys(props).map((key) => {
  65. ret[key] = unref((props as Recordable)[key]);
  66. });
  67. return ret as Partial<U>;
  68. }
  69. export function getRawRoute(route: RouteLocationNormalized): RouteLocationNormalized {
  70. if (!route) return route;
  71. const { matched, ...opt } = route;
  72. return {
  73. ...opt,
  74. matched: (matched
  75. ? matched.map((item) => ({
  76. meta: item.meta,
  77. name: item.name,
  78. path: item.path,
  79. }))
  80. : undefined) as RouteRecordNormalized[],
  81. };
  82. }
  83. // https://github.com/vant-ui/vant/issues/8302
  84. type EventShim = {
  85. new (...args: any[]): {
  86. $props: {
  87. onClick?: (...args: any[]) => void;
  88. };
  89. };
  90. };
  91. export type WithInstall<T> = T & {
  92. install(app: App): void;
  93. } & EventShim;
  94. export type CustomComponent = Component & { displayName?: string };
  95. export const withInstall = <T extends CustomComponent>(component: T, alias?: string) => {
  96. (component as Record<string, unknown>).install = (app: App) => {
  97. const compName = component.name || component.displayName;
  98. if (!compName) return;
  99. app.component(compName, component);
  100. if (alias) {
  101. app.config.globalProperties[alias] = component;
  102. }
  103. };
  104. return component as WithInstall<T>;
  105. };