From c6d8ea02ade42a78e9f4a2304e8e1c5f67853d91 Mon Sep 17 00:00:00 2001 From: wujingjing <gersonwu@qq.com> Date: 星期四, 06 三月 2025 16:17:06 +0800 Subject: [PATCH] 创建工单 --- src/utils/util.ts | 68 +++++++++++++++++++++++++++++++++- 1 files changed, 66 insertions(+), 2 deletions(-) diff --git a/src/utils/util.ts b/src/utils/util.ts index 093d5d7..8c3aeff 100644 --- a/src/utils/util.ts +++ b/src/utils/util.ts @@ -10,6 +10,7 @@ import FileSaver from 'file-saver'; import * as XLSX from 'xlsx'; import { MAIN_URL } from '../constants'; +import { saveAs } from 'file-saver'; /** * 鏅�氬璞¤浆涓� formData @@ -284,7 +285,7 @@ callback: (value: T, index?, array?, parent?) => any, parent: any = null, markParent = false, - childrenKey = 'Children' + childrenKey = 'children' ) => { if (!treeData || treeData.length === 0) return; if (!parent) parent = treeData; @@ -547,6 +548,7 @@ const endDate = new Date(); const startDate = new Date(); startDate.setTime(startDate.getTime() - 3600 * 1000 * 24 * dates); + endDate.setHours(23, 59, 59, 59); startDate.setHours(0, 0, 0, 0); return [startDate, endDate]; }; @@ -620,11 +622,26 @@ export const toPercent = (num: number, havePercentSymbol = true, decimalPlaces = 1, defaultValue = '-') => { if (num == null) return `${defaultValue} %`; - let percent = Number(num * 100).toFixed(decimalPlaces); + const factor = Math.pow(10, decimalPlaces); + + let percent = Math.round(Number(num) * 100 * factor) / factor + ''; if (havePercentSymbol) { percent += '%'; } return percent; +}; + +/** + * 淇濈暀鎸囧畾绮惧害灏忔暟浣嶏紝涓斾笉琛ラ浂 + * @param num + * @param precision + * @returns + */ +export const toMyFixed = (num, precision) => { + if (num == null) return ''; + if (!precision) return num + ''; + const factor = Math.pow(10, precision); + return Math.round(Number(num) * factor) / factor + ''; }; /** @@ -723,3 +740,50 @@ document.body.removeChild(spanEle); return width; } + +export function decodeFormData(formDataString) { + const params = new URLSearchParams(formDataString); + const decodedData = {}; + for (const [key, value] of params) { + decodedData[key] = decodeURIComponent(value); + } + return decodedData; +} + +/** + * 浼戠湢鎸囧畾绉掓暟 + * @param seconds 浼戠湢绉掓暟 + * @returns Promise + */ +export const sleep = (seconds: number): Promise<void> => { + return new Promise((resolve) => { + setTimeout(resolve, seconds * 1000); + }); +}; + + +/** + * 涓嬭浇鏂囦欢 + * @param url 鏂囦欢鍦板潃 + * [{ + 绫诲瀷: item.OTYPE, + 鍚嶇О: item.ONAME, + 浣嶇疆: item.WKT, +}] + * @param fileName 鏂囦欢鍚� + */ +export const downloadExcel = (tableData: any[], fileName: string) => { + // 鍒涘缓宸ヤ綔绨� + const wb = XLSX.utils.book_new(); + // 鍒涘缓宸ヤ綔琛� + const ws = XLSX.utils.json_to_sheet(tableData); + // 灏嗗伐浣滆〃娣诲姞鍒板伐浣滅翱 + XLSX.utils.book_append_sheet(wb, ws, '鏌ヨ缁撴灉'); + + // 鐢熸垚 Excel 鏂囦欢 + const excelBuffer = XLSX.write(wb, { bookType: 'xlsx', type: 'array' }); + const blob = new Blob([excelBuffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }); + + // 涓嬭浇鏂囦欢 + saveAs(blob, `${fileName}_${new Date().toLocaleDateString()}.xlsx`); +}; -- Gitblit v1.9.3