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) => { 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, 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) => (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); };