util.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. /**
  6. * 下载文件
  7. * @param url 下载链接
  8. * @param params 参数
  9. */
  10. export const downloadFile = (url: string, params?: Record<string, any>) => {
  11. const formElement = document.createElement('form');
  12. formElement.style.display = 'display:none;';
  13. formElement.method = 'GET';
  14. formElement.action = url;
  15. // 添加参数
  16. if (params) {
  17. Object.keys(params).forEach((key: string) => {
  18. const inputElement = document.createElement('input');
  19. inputElement.type = 'hidden';
  20. inputElement.name = key;
  21. inputElement.value = params[key];
  22. formElement.appendChild(inputElement);
  23. });
  24. }
  25. const inputElement = document.createElement('input');
  26. inputElement.type = 'hidden';
  27. inputElement.name = ':X_Access_Token';
  28. inputElement.value = Token.get();
  29. formElement.appendChild(inputElement);
  30. document.body.appendChild(formElement);
  31. formElement.submit();
  32. document.body.removeChild(formElement);
  33. };
  34. /**
  35. * 把数据下载成JSON
  36. * @param record
  37. * @param fileName
  38. */
  39. export const downloadObject = (record: Record<string, unknown>, fileName: string) => {
  40. // 创建隐藏的可下载链接
  41. const ghostLink = document.createElement('a');
  42. ghostLink.download = `${fileName}-${
  43. record?.name || moment(new Date()).format('YYYY/MM/DD HH:mm:ss')
  44. }.json`;
  45. ghostLink.style.display = 'none';
  46. //字符串内容转成Blob地址
  47. const blob = new Blob([JSON.stringify(record)]);
  48. ghostLink.href = URL.createObjectURL(blob);
  49. //触发点击
  50. document.body.appendChild(ghostLink);
  51. ghostLink.click();
  52. //移除
  53. document.body.removeChild(ghostLink);
  54. };
  55. export const useAsyncDataSource =
  56. (services: (arg0: Field) => Promise<FieldDataSource>) => (field: Field) => {
  57. field.loading = true;
  58. services(field).then(
  59. action.bound!((resp: any) => {
  60. field.dataSource = resp;
  61. field.loading = false;
  62. }),
  63. );
  64. };
  65. export const getDateFormat = (
  66. date: moment.MomentInput,
  67. format: string = 'YYYY-MM-DD HH:mm:ss',
  68. ): string => {
  69. return date ? moment(date).format(format) : '-';
  70. };
  71. /**
  72. * 扁平化树数组
  73. */
  74. export const flattenArray: any = (arr: any[]) => {
  75. return arr.reduce((result, item) => {
  76. return result.concat(item, Array.isArray(item.children) ? flattenArray(item.children) : []);
  77. }, []);
  78. };
  79. /**
  80. * 判断是否为正确的IP地址
  81. */
  82. export const testIP = (str: string) => {
  83. const re =
  84. /^([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])$/;
  85. return re.test(str);
  86. };