index.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. export const noop = () => {};
  6. /**
  7. * @description: Set ui mount node
  8. */
  9. export function getPopupContainer(node?: HTMLElement): HTMLElement {
  10. return (node?.parentNode as HTMLElement) ?? document.body;
  11. }
  12. /**
  13. * Add the object as a parameter to the URL
  14. * @param baseUrl url
  15. * @param obj
  16. * @returns {string}
  17. * eg:
  18. * let obj = {a: '3', b: '4'}
  19. * setObjToUrlParams('www.baidu.com', obj)
  20. * ==>www.baidu.com?a=3&b=4
  21. */
  22. export function setObjToUrlParams(baseUrl: string, obj: any): string {
  23. let parameters = '';
  24. for (const key in obj) {
  25. parameters += key + '=' + encodeURIComponent(obj[key]) + '&';
  26. }
  27. parameters = parameters.replace(/&$/, '');
  28. return /\?$/.test(baseUrl) ? baseUrl + parameters : baseUrl.replace(/\/?$/, '?') + parameters;
  29. }
  30. export function deepMerge<T = any>(src: any = {}, target: any = {}): T {
  31. let key: string;
  32. for (key in target) {
  33. src[key] = isObject(src[key]) ? deepMerge(src[key], target[key]) : (src[key] = target[key]);
  34. }
  35. return src;
  36. }
  37. export function openWindow(
  38. url: string,
  39. opt?: { target?: TargetContext | string; noopener?: boolean; noreferrer?: boolean },
  40. ) {
  41. const { target = '__blank', noopener = true, noreferrer = true } = opt || {};
  42. const feature: string[] = [];
  43. noopener && feature.push('noopener=yes');
  44. noreferrer && feature.push('noreferrer=yes');
  45. window.open(url, target, feature.join(','));
  46. }
  47. // dynamic use hook props
  48. export function getDynamicProps<T, U>(props: T): Partial<U> {
  49. const ret: Recordable = {};
  50. Object.keys(props).map((key) => {
  51. ret[key] = unref((props as Recordable)[key]);
  52. });
  53. return ret as Partial<U>;
  54. }
  55. export function getRawRoute(route: RouteLocationNormalized): RouteLocationNormalized {
  56. if (!route) return route;
  57. const { matched, ...opt } = route;
  58. return {
  59. ...opt,
  60. matched: (matched
  61. ? matched.map((item) => ({
  62. meta: item.meta,
  63. name: item.name,
  64. path: item.path,
  65. }))
  66. : undefined) as RouteRecordNormalized[],
  67. };
  68. }
  69. export const withInstall = <T>(component: T, alias?: string) => {
  70. const comp = component as any;
  71. comp.install = (app: App) => {
  72. app.component(comp.name || comp.displayName, component);
  73. if (alias) {
  74. app.config.globalProperties[alias] = component;
  75. }
  76. };
  77. return component as T & Plugin;
  78. };
  79. // 禁止输入表情包
  80. export const isEmojiCharacter = (substring) => {
  81. for ( var i = 0; i < substring.length; i++) {
  82. var hs = substring.charCodeAt(i);
  83. if (0xd800 <= hs && hs <= 0xdbff) {
  84. if (substring.length > 1) {
  85. var ls = substring.charCodeAt(i + 1);
  86. var uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
  87. if (0x1d000 <= uc && uc <= 0x1f77f) {
  88. return true;
  89. }
  90. }
  91. } else if (substring.length > 1) {
  92. var ls = substring.charCodeAt(i + 1);
  93. if (ls == 0x20e3) {
  94. return true;
  95. }
  96. } else {
  97. if (0x2100 <= hs && hs <= 0x27ff) {
  98. return true;
  99. } else if (0x2B05 <= hs && hs <= 0x2b07) {
  100. return true;
  101. } else if (0x2934 <= hs && hs <= 0x2935) {
  102. return true;
  103. } else if (0x3297 <= hs && hs <= 0x3299) {
  104. return true;
  105. } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030
  106. || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b
  107. || hs == 0x2b50) {
  108. return true;
  109. }
  110. }
  111. }
  112. }