From f15c8fa05e3e4eae8ef093ba6ba827ad74e38fec Mon Sep 17 00:00:00 2001 From: yangyin <1850366751@qq.com> Date: 星期二, 05 十一月 2024 10:32:27 +0800 Subject: [PATCH] 常用语联调接口 --- src/utils/util.ts | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/utils/util.ts b/src/utils/util.ts index 13bfb8d..4b07f4e 100644 --- a/src/utils/util.ts +++ b/src/utils/util.ts @@ -541,11 +541,13 @@ * 鏈�杩� n 澶╃殑 startDate銆乪ndDate * @param dates */ -export const getRecentDateRange = (dates: number) => { +export const getRecentDateRange = (dates: number, includesCurrent = true) => { + dates = includesCurrent ? dates - 1 : dates; // 鑾峰彇褰撳墠鏃ユ湡 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]; }; @@ -619,7 +621,9 @@ 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 += '%'; } @@ -697,3 +701,37 @@ export const arrayIsEmpty = (arr: any) => { return !arr || arr.length === 0; }; + +type GetTextWidthOption = { + size?: string; + family?: string; +}; + +export function getTextWidth(text: string, option: GetTextWidthOption) { + if (!text) return 0; + const { size = '14px', family = 'Microsoft YaHei' } = option; + const spanEle = document.createElement('span'); + document.body.appendChild(spanEle); + + spanEle.style.font = 'times new roman'; + spanEle.style.fontSize = size; + spanEle.style.height = 'auto'; + spanEle.style.width = 'auto'; + spanEle.style.position = 'absolute'; + spanEle.style.whiteSpace = 'no-wrap'; + spanEle.innerHTML = text; + + const width = spanEle.clientWidth; + + 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; +} -- Gitblit v1.9.3