| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- import moment from 'moment';
- import type { Field, FieldDataSource } from '@formily/core';
- import { action } from '@formily/reactive';
- import Token from '@/utils/token';
- /**
- * 下载文件
- * @param url 下载链接
- * @param params 参数
- */
- export const downloadFile = (url: string, params?: Record<string, any>) => {
- const formElement = document.createElement('form');
- formElement.style.display = 'display:none;';
- formElement.method = 'GET';
- formElement.action = url;
- // 添加参数
- if (params) {
- Object.keys(params).forEach((key: string) => {
- const inputElement = document.createElement('input');
- inputElement.type = 'hidden';
- inputElement.name = key;
- inputElement.value = params[key];
- formElement.appendChild(inputElement);
- });
- }
- const inputElement = document.createElement('input');
- inputElement.type = 'hidden';
- inputElement.name = ':X_Access_Token';
- inputElement.value = Token.get();
- formElement.appendChild(inputElement);
- document.body.appendChild(formElement);
- formElement.submit();
- document.body.removeChild(formElement);
- };
- /**
- * 把数据下载成JSON
- * @param record
- * @param fileName
- */
- export const downloadObject = (record: Record<string, unknown>, fileName: string) => {
- // 创建隐藏的可下载链接
- const ghostLink = document.createElement('a');
- ghostLink.download = `${fileName}-${
- record?.name || moment(new Date()).format('YYYY/MM/DD HH:mm:ss')
- }.json`;
- ghostLink.style.display = 'none';
- //字符串内容转成Blob地址
- const blob = new Blob([JSON.stringify(record)]);
- ghostLink.href = URL.createObjectURL(blob);
- //触发点击
- document.body.appendChild(ghostLink);
- ghostLink.click();
- //移除
- document.body.removeChild(ghostLink);
- };
- export const useAsyncDataSource =
- (services: (arg0: Field) => Promise<FieldDataSource>) => (field: Field) => {
- field.loading = true;
- services(field).then(
- action.bound!((resp: any) => {
- field.dataSource = resp;
- field.loading = false;
- }),
- );
- };
- export const getDateFormat = (
- date: moment.MomentInput,
- format: string = 'YYYY-MM-DD HH:mm:ss',
- ): string => {
- return date ? moment(date).format(format) : '-';
- };
- /**
- * 扁平化树数组
- */
- export const flattenArray: any = (arr: any[]) => {
- return arr.reduce((result, item) => {
- return result.concat(item, Array.isArray(item.children) ? flattenArray(item.children) : []);
- }, []);
- };
- /**
- * 判断是否为正确的IP地址
- */
- export const testIP = (str: string) => {
- const re =
- /^([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])$/;
- return re.test(str);
- };
|