util.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import moment from 'moment';
  2. import type { Field, FieldDataSource } from '@formily/core';
  3. import { action } from '@formily/reactive';
  4. import Token from '@/utils/token';
  5. import { message } from 'antd';
  6. import SystemConst from '@/utils/const';
  7. /**
  8. * 下载文件
  9. * @param url 下载链接
  10. * @param params 参数
  11. */
  12. export const downloadFile = (url: string, params?: Record<string, any>) => {
  13. const formElement = document.createElement('form');
  14. formElement.style.display = 'display:none;';
  15. formElement.method = 'GET';
  16. formElement.action = url;
  17. // 添加参数
  18. if (params) {
  19. Object.keys(params).forEach((key: string) => {
  20. const inputElement = document.createElement('input');
  21. inputElement.type = 'hidden';
  22. inputElement.name = key;
  23. inputElement.value = params[key];
  24. formElement.appendChild(inputElement);
  25. });
  26. }
  27. const inputElement = document.createElement('input');
  28. inputElement.type = 'hidden';
  29. inputElement.name = ':X_Access_Token';
  30. inputElement.value = Token.get();
  31. formElement.appendChild(inputElement);
  32. document.body.appendChild(formElement);
  33. formElement.submit();
  34. document.body.removeChild(formElement);
  35. };
  36. export const downloadFileByUrl = (url: string, name: string, type: string) => {
  37. const downNode = document.createElement('a');
  38. downNode.style.display = 'none';
  39. downNode.download = `${name}.${type}`;
  40. downNode.href = url;
  41. document.body.appendChild(downNode);
  42. downNode.click();
  43. document.body.removeChild(downNode);
  44. };
  45. /**
  46. * 把数据下载成JSON
  47. * @param record
  48. * @param fileName
  49. */
  50. export const downloadObject = (record: Record<string, any>, fileName: string) => {
  51. // 创建隐藏的可下载链接
  52. const ghostLink = document.createElement('a');
  53. ghostLink.download = `${record?.name}${fileName}_${moment(new Date()).format(
  54. 'YYYY/MM/DD HH:mm:ss',
  55. )}.json`;
  56. ghostLink.style.display = 'none';
  57. //字符串内容转成Blob地址
  58. const blob = new Blob([JSON.stringify(record)]);
  59. ghostLink.href = URL.createObjectURL(blob);
  60. //触发点击
  61. document.body.appendChild(ghostLink);
  62. ghostLink.click();
  63. //移除
  64. document.body.removeChild(ghostLink);
  65. };
  66. export const useAsyncDataSource =
  67. (services: (arg0: Field) => Promise<FieldDataSource>) => (field: Field) => {
  68. field.loading = true;
  69. services(field).then(
  70. action.bound!((resp: any) => {
  71. field.dataSource = resp;
  72. field.loading = false;
  73. }),
  74. );
  75. };
  76. export const getDateFormat = (
  77. date: moment.MomentInput,
  78. format: string = 'YYYY-MM-DD HH:mm:ss',
  79. ): string => {
  80. return date ? moment(date).format(format) : '-';
  81. };
  82. /**
  83. * 扁平化树数组
  84. */
  85. export const flattenArray: any = (arr: any[]) => {
  86. return arr.reduce((result, item) => {
  87. return result.concat(item, Array.isArray(item.children) ? flattenArray(item.children) : []);
  88. }, []);
  89. };
  90. /**
  91. * 判断是否为正确的IP地址
  92. */
  93. export const testIP = (str: string) => {
  94. const re =
  95. /^([0-9]|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.([0-9]|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.([0-9]|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.([0-9]|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])$/;
  96. return re.test(str);
  97. };
  98. export const testIPv6 = (str: string) => {
  99. const re =
  100. /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/;
  101. return re.test(str);
  102. };
  103. export const testDomain = (str: string) => {
  104. const re = /([0-9a-z-]{2,}\.[0-9a-z-]{2,3}\.[0-9a-z-]{2,3}|[0-9a-z-]{2,}\.[0-9a-z-]{2,3})$/i;
  105. return re.test(str);
  106. };
  107. // 生成随机数
  108. export const randomString = (length?: number) => {
  109. const tempLength = length || 32;
  110. const chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
  111. const maxPos = chars.length;
  112. let pwd = '';
  113. for (let i = 0; i < tempLength; i += 1) {
  114. pwd += chars.charAt(Math.floor(Math.random() * maxPos));
  115. }
  116. return pwd;
  117. };
  118. /**
  119. * 获取当前DOM元素需要撑满的高度
  120. * @param className
  121. * @param extraHeight 额外减去的高度
  122. */
  123. export const getDomFullHeight = (className: string, extraHeight: number = 0): number => {
  124. const dom = document.querySelector(`.${className}`);
  125. if (dom) {
  126. const bodyClient = document.body.getBoundingClientRect();
  127. const domClient = dom.getBoundingClientRect();
  128. if (domClient.y < 50) {
  129. return 100;
  130. }
  131. return bodyClient.height - domClient.y - 24 - extraHeight;
  132. }
  133. return 0;
  134. };
  135. export const onlyMessage = (
  136. msg: string,
  137. type: 'success' | 'error' | 'warning' = 'success',
  138. key: number = 1,
  139. ) =>
  140. message[type]({
  141. content: msg,
  142. key: key,
  143. });
  144. export const isNoCommunity = !(localStorage.getItem(SystemConst.Version_Code) === 'community');
  145. /**
  146. * 座机号+手机号校验
  147. * @param value
  148. * @returns {boolean}
  149. */
  150. export const phoneRegEx = (value: string) => {
  151. const phone = new RegExp(
  152. '^(((\\+86)|(\\+86-))|((86)|(86\\-))|((0086)|(0086\\-)))?1[3|5|7|8|9]\\d{9}$',
  153. );
  154. const mobile = /(0[0-9]{2,3})([2-9][0-9]{6,7})+([0-9]{8,11})?$/;
  155. return phone.test(value) || mobile.test(value);
  156. };