util.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import moment from 'moment';
  2. import type { Field, FieldDataSource } from '@formily/core';
  3. import { action } from '@formily/reactive';
  4. export const downObject = (record: Record<string, unknown>, fileName: string) => {
  5. // 创建隐藏的可下载链接
  6. const ghostLink = document.createElement('a');
  7. ghostLink.download = `${fileName}-${
  8. record?.name || moment(new Date()).format('YYYY/MM/DD HH:mm:ss')
  9. }.json`;
  10. ghostLink.style.display = 'none';
  11. //字符串内容转成Blob地址
  12. const blob = new Blob([JSON.stringify(record)]);
  13. ghostLink.href = URL.createObjectURL(blob);
  14. //触发点击
  15. document.body.appendChild(ghostLink);
  16. ghostLink.click();
  17. //移除
  18. document.body.removeChild(ghostLink);
  19. };
  20. export const useAsyncDataSource =
  21. (services: (arg0: Field) => Promise<FieldDataSource>) => (field: Field) => {
  22. field.loading = true;
  23. services(field).then(
  24. action.bound!((resp: any) => {
  25. field.dataSource = resp;
  26. field.loading = false;
  27. }),
  28. );
  29. };
  30. export const getDateFormat = (
  31. date: moment.MomentInput,
  32. format: string = 'YYYY-MM-DD HH:mm:ss',
  33. ): string => {
  34. return date ? moment(date).format(format) : '-';
  35. };