index.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import type { RouteLocationNormalized, RouteRecordNormalized } from 'vue-router';
  2. import type { App, Plugin } from 'vue';
  3. import { unref } from 'vue';
  4. import { isObject } from '/@/utils/is';
  5. // update-begin--author:sunjianlei---date:20220408---for: 【VUEN-656】配置外部网址打不开,原因是带了#号,需要替换一下
  6. export const URL_HASH_TAB = `__AGWE4H__HASH__TAG__PWHRG__`
  7. // update-end--author:sunjianlei---date:20220408---for: 【VUEN-656】配置外部网址打不开,原因是带了#号,需要替换一下
  8. export const noop = () => {};
  9. /**
  10. * @description: Set ui mount node
  11. */
  12. export function getPopupContainer(node?: HTMLElement): HTMLElement {
  13. return (node?.parentNode as HTMLElement) ?? document.body;
  14. }
  15. /**
  16. * Add the object as a parameter to the URL
  17. * @param baseUrl url
  18. * @param obj
  19. * @returns {string}
  20. * eg:
  21. * let obj = {a: '3', b: '4'}
  22. * setObjToUrlParams('www.baidu.com', obj)
  23. * ==>www.baidu.com?a=3&b=4
  24. */
  25. export function setObjToUrlParams(baseUrl: string, obj: any): string {
  26. let parameters = '';
  27. for (const key in obj) {
  28. parameters += key + '=' + encodeURIComponent(obj[key]) + '&';
  29. }
  30. parameters = parameters.replace(/&$/, '');
  31. return /\?$/.test(baseUrl) ? baseUrl + parameters : baseUrl.replace(/\/?$/, '?') + parameters;
  32. }
  33. export function deepMerge<T = any>(src: any = {}, target: any = {}): T {
  34. let key: string;
  35. for (key in target) {
  36. src[key] = isObject(src[key]) ? deepMerge(src[key], target[key]) : (src[key] = target[key]);
  37. }
  38. return src;
  39. }
  40. export function openWindow(
  41. url: string,
  42. opt?: { target?: TargetContext | string; noopener?: boolean; noreferrer?: boolean }
  43. ) {
  44. const { target = '__blank', noopener = true, noreferrer = true } = opt || {};
  45. const feature: string[] = [];
  46. noopener && feature.push('noopener=yes');
  47. noreferrer && feature.push('noreferrer=yes');
  48. window.open(url, target, feature.join(','));
  49. }
  50. // dynamic use hook props
  51. export function getDynamicProps<T, U>(props: T): Partial<U> {
  52. const ret: Recordable = {};
  53. Object.keys(props).map((key) => {
  54. ret[key] = unref((props as Recordable)[key]);
  55. });
  56. return ret as Partial<U>;
  57. }
  58. /**
  59. * 获取表单字段值数据类型
  60. * @param props
  61. * @param field
  62. * @updateBy:zyf
  63. */
  64. export function getValueType(props,field){
  65. let formSchema = unref(unref(props)?.schemas)
  66. let valueType = "string";
  67. if (formSchema) {
  68. let schema = formSchema.filter((item) => item.field === field)[0];
  69. valueType = schema.componentProps&&schema.componentProps.valueType ? schema.componentProps.valueType : valueType;
  70. }
  71. return valueType;
  72. }
  73. export function getRawRoute(route: RouteLocationNormalized): RouteLocationNormalized {
  74. if (!route) return route;
  75. const { matched, ...opt } = route;
  76. return {
  77. ...opt,
  78. matched: (matched
  79. ? matched.map((item) => ({
  80. meta: item.meta,
  81. name: item.name,
  82. path: item.path,
  83. }))
  84. : undefined) as RouteRecordNormalized[],
  85. };
  86. }
  87. /**
  88. * 深度克隆对象、数组
  89. * @param obj 被克隆的对象
  90. * @return 克隆后的对象
  91. */
  92. export function cloneObject(obj) {
  93. return JSON.parse(JSON.stringify(obj))
  94. }
  95. export const withInstall = <T>(component: T, alias?: string) => {
  96. const comp = component as any;
  97. comp.install = (app: App) => {
  98. app.component(comp.name || comp.displayName, component);
  99. if (alias) {
  100. app.config.globalProperties[alias] = component;
  101. }
  102. };
  103. return component as T & Plugin;
  104. };
  105. /**
  106. * 获取url地址参数
  107. * @param paraName
  108. */
  109. export function getUrlParam(paraName) {
  110. let url = document.location.toString();
  111. let arrObj = url.split("?");
  112. if (arrObj.length > 1) {
  113. let arrPara = arrObj[1].split("&");
  114. let arr;
  115. for (let i = 0; i < arrPara.length; i++) {
  116. arr = arrPara[i].split("=");
  117. if (arr != null && arr[0] == paraName) {
  118. return arr[1];
  119. }
  120. }
  121. return "";
  122. }
  123. else {
  124. return "";
  125. }
  126. }
  127. /**
  128. * 休眠(setTimeout的promise版)
  129. * @param ms 要休眠的时间,单位:毫秒
  130. * @param fn callback,可空
  131. * @return Promise
  132. */
  133. export function sleep(ms: number, fn?: Fn) {
  134. return new Promise<void>(resolve => setTimeout(() => {
  135. fn && fn()
  136. resolve()
  137. }, ms))
  138. }
  139. /**
  140. * 不用正则的方式替换所有值
  141. * @param text 被替换的字符串
  142. * @param checker 替换前的内容
  143. * @param replacer 替换后的内容
  144. * @returns {String} 替换后的字符串
  145. */
  146. export function replaceAll(text, checker, replacer) {
  147. let lastText = text
  148. text = text.replace(checker, replacer)
  149. if (lastText !== text) {
  150. return replaceAll(text, checker, replacer)
  151. }
  152. return text
  153. }