util.ts 1012 B

12345678910111213141516171819202122232425262728293031
  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. };