wujingjing
2024-12-17 1622a4d80dd86d0fde35af6908937f9be670fee3
src/utils/util.ts
@@ -541,11 +541,13 @@
 * 最近 n 天的 startDate、endDate
 * @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,11 +621,28 @@
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 + '';
};
/**
@@ -704,6 +723,7 @@
};
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);
@@ -721,3 +741,12 @@
   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;
}