From b724ca2917856c81221dfcd06fbe46a0a2d943b2 Mon Sep 17 00:00:00 2001 From: xingyu4j Date: Tue, 6 May 2025 21:35:10 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=88=A0=E9=99=A4=E5=B7=B2=E7=BB=8F?= =?UTF-8?q?=E8=BF=81=E7=A7=BBvben/utils=20=E4=B8=8B=E7=9A=84=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/web-antd/src/utils/TimeUtils.ts | 47 ---------- apps/web-antd/src/utils/date.ts | 42 --------- apps/web-antd/src/utils/download.ts | 127 --------------------------- apps/web-antd/src/utils/tree.ts | 71 --------------- 4 files changed, 287 deletions(-) delete mode 100644 apps/web-antd/src/utils/TimeUtils.ts delete mode 100644 apps/web-antd/src/utils/date.ts delete mode 100644 apps/web-antd/src/utils/download.ts delete mode 100644 apps/web-antd/src/utils/tree.ts diff --git a/apps/web-antd/src/utils/TimeUtils.ts b/apps/web-antd/src/utils/TimeUtils.ts deleted file mode 100644 index 8c45beda..00000000 --- a/apps/web-antd/src/utils/TimeUtils.ts +++ /dev/null @@ -1,47 +0,0 @@ -// 迁移至 packages/@core/base/shared/src/utils/time.ts -import dayjs from 'dayjs'; - -/** 时间段选择器拓展 */ -export const rangePickerExtend = () => { - return { - showTime: { - format: 'HH:mm:ss', - defaultValue: [ - dayjs('00:00:00', 'HH:mm:ss'), - dayjs('23:59:59', 'HH:mm:ss'), - ], - }, - // 如果需要10位时间戳(秒级)可以使用 valueFormat: 'X' - valueFormat: 'YYYY-MM-DD HH:mm:ss', - format: 'YYYY-MM-DD HH:mm:ss', // 显示格式 - placeholder: ['开始时间', '结束时间'], - ranges: { - 今天: [dayjs().startOf('day'), dayjs().endOf('day')], - - 昨天: [ - dayjs().subtract(1, 'day').startOf('day'), - dayjs().subtract(1, 'day').endOf('day'), - ], - - 本周: [dayjs().startOf('week'), dayjs().endOf('day')], - - 本月: [dayjs().startOf('month'), dayjs().endOf('day')], - - 最近7天: [ - dayjs().subtract(7, 'day').startOf('day'), - dayjs().endOf('day'), - ], - - 最近30天: [ - dayjs().subtract(30, 'day').startOf('day'), - dayjs().endOf('day'), - ], - }, - transformDateFunc: (dates: any) => { - if (dates && dates.length === 2) { - return [dates.createTime[0], dates.createTime[1]].join(','); // 格式化为后台支持的时间格式 - } - return {}; - }, - }; -}; diff --git a/apps/web-antd/src/utils/date.ts b/apps/web-antd/src/utils/date.ts deleted file mode 100644 index 227cf762..00000000 --- a/apps/web-antd/src/utils/date.ts +++ /dev/null @@ -1,42 +0,0 @@ -import dayjs from 'dayjs'; - -// TODO @芋艿:后续整理下 迁移至 packages/core/base/shared/src/utils/date.ts,后续删除 使用 @vben/utils 的 getRangePickerDefaultProps - -/** 时间段选择器拓展 */ -export function getRangePickerDefaultProps(): any { - return { - showTime: { - format: 'HH:mm:ss', - defaultValue: [ - dayjs('00:00:00', 'HH:mm:ss'), - dayjs('23:59:59', 'HH:mm:ss'), - ], - }, - valueFormat: 'YYYY-MM-DD HH:mm:ss', - format: 'YYYY-MM-DD HH:mm:ss', - placeholder: ['开始时间', '结束时间'], - ranges: { - 今天: [dayjs().startOf('day'), dayjs().endOf('day')], - 昨天: [ - dayjs().subtract(1, 'day').startOf('day'), - dayjs().subtract(1, 'day').endOf('day'), - ], - 本周: [dayjs().startOf('week'), dayjs().endOf('day')], - 本月: [dayjs().startOf('month'), dayjs().endOf('day')], - '最近 7 天': [ - dayjs().subtract(7, 'day').startOf('day'), - dayjs().endOf('day'), - ], - '最近 30 天': [ - dayjs().subtract(30, 'day').startOf('day'), - dayjs().endOf('day'), - ], - }, - transformDateFunc: (dates: any) => { - if (dates && dates.length === 2) { - return [dates.createTime[0], dates.createTime[1]].join(','); // 格式化为后台支持的时间格式 - } - return {}; - }, - }; -} diff --git a/apps/web-antd/src/utils/download.ts b/apps/web-antd/src/utils/download.ts deleted file mode 100644 index 2fc2989b..00000000 --- a/apps/web-antd/src/utils/download.ts +++ /dev/null @@ -1,127 +0,0 @@ -// TODO @芋艿:需要优化下每个方法 -// TODO @芋艿:是不是可以共用么? -// 后续使用 packages/core/base/shared/src/utils/download.ts 下的方法 -import { dataURLtoBlob, urlToBase64 } from './base64Conver'; - -/** - * Download online pictures - * @param url - * @param filename - * @param mime - * @param bom - */ -export function downloadByOnlineUrl( - url: string, - filename: string, - mime?: string, - bom?: BlobPart, -) { - urlToBase64(url).then((base64) => { - downloadByBase64(base64, filename, mime, bom); - }); -} - -/** - * Download pictures based on base64 - * @param buf - * @param filename - * @param mime - * @param bom - */ -export function downloadByBase64( - buf: string, - filename: string, - mime?: string, - bom?: BlobPart, -) { - const base64Buf = dataURLtoBlob(buf); - downloadByData(base64Buf, filename, mime, bom); -} - -/** - * Download according to the background interface file stream - * @param {*} data - * @param {*} filename - * @param {*} mime - * @param {*} bom - */ -export function downloadByData( - data: BlobPart, - filename: string, - mime?: string, - bom?: BlobPart, -) { - const blobData = bom === undefined ? [data] : [bom, data]; - const blob = new Blob(blobData, { type: mime || 'application/octet-stream' }); - - const blobURL = window.URL.createObjectURL(blob); - const tempLink = document.createElement('a'); - tempLink.style.display = 'none'; - tempLink.href = blobURL; - tempLink.setAttribute('download', filename); - if (tempLink.download === undefined) - tempLink.setAttribute('target', '_blank'); - - document.body.append(tempLink); - tempLink.click(); - tempLink.remove(); - window.URL.revokeObjectURL(blobURL); -} - -/** - * Download file according to file address - * @param {*} sUrl - */ -export function downloadByUrl({ - url, - target = '_blank', - fileName, -}: { - fileName?: string; - target?: '_blank' | '_self'; - url: string; -}): boolean { - const isChrome = window.navigator.userAgent.toLowerCase().includes('chrome'); - const isSafari = window.navigator.userAgent.toLowerCase().includes('safari'); - - if (/iP/.test(window.navigator.userAgent)) { - console.error('Your browser does not support download!'); - return false; - } - if (isChrome || isSafari) { - const link = document.createElement('a'); - link.href = url; - link.target = target; - - if (link.download !== undefined) - link.download = fileName || url.slice(url.lastIndexOf('/') + 1); - - if (document.createEvent) { - const e = document.createEvent('MouseEvents'); - e.initEvent('click', true, true); - link.dispatchEvent(e); - return true; - } - } - if (!url.includes('?')) url += '?download'; - - openWindow(url, { target }); - return true; -} - -export function openWindow( - url: string, - opt?: { - noopener?: boolean; - noreferrer?: boolean; - target?: '_blank' | '_self' | string; - }, -) { - const { noopener = true, noreferrer = true, target = '__blank' } = opt || {}; - const feature: string[] = []; - - noopener && feature.push('noopener=yes'); - noreferrer && feature.push('noreferrer=yes'); - - window.open(url, target, feature.join(',')); -} diff --git a/apps/web-antd/src/utils/tree.ts b/apps/web-antd/src/utils/tree.ts deleted file mode 100644 index 3dda8965..00000000 --- a/apps/web-antd/src/utils/tree.ts +++ /dev/null @@ -1,71 +0,0 @@ -// todo @芋艿:公用逻辑 -// 已迁移,后续使用 packages/core/base/shared/src/utils/tree.ts 下的方法 -interface TreeNode { - [key: string]: any; - children?: TreeNode[]; -} - -/** - * 构造树型结构数据 - * - * @param {*} data 数据源 - * @param {*} id id字段 默认 'id' - * @param {*} parentId 父节点字段 默认 'parentId' - * @param {*} children 孩子节点字段 默认 'children' - */ -export const handleTree = ( - data: TreeNode[], - id: string = 'id', - parentId: string = 'parentId', - children: string = 'children', -): TreeNode[] => { - if (!Array.isArray(data)) { - console.warn('data must be an array'); - return []; - } - const config = { - id, - parentId, - childrenList: children, - }; - const childrenListMap: Record = {}; - const nodeIds: Record = {}; - const tree: TreeNode[] = []; - - // 1. 数据预处理 - // 1.1 第一次遍历,生成 childrenListMap 和 nodeIds 映射 - for (const d of data) { - const pId = d[config.parentId]; - if (childrenListMap[pId] === undefined) { - childrenListMap[pId] = []; - } - nodeIds[d[config.id]] = d; - childrenListMap[pId].push(d); - } - // 1.2 第二次遍历,找出根节点 - for (const d of data) { - const pId = d[config.parentId]; - if (nodeIds[pId] === undefined) { - tree.push(d); - } - } - - // 2. 构建树结:递归构建子节点 - const adaptToChildrenList = (node: TreeNode): void => { - const nodeId = node[config.id]; - if (childrenListMap[nodeId]) { - node[config.childrenList] = childrenListMap[nodeId]; - // 递归处理子节点 - for (const child of node[config.childrenList]) { - adaptToChildrenList(child); - } - } - }; - - // 3. 从根节点开始构建完整树 - for (const rootNode of tree) { - adaptToChildrenList(rootNode); - } - - return tree; -};