From 0159386060edb946f29b5adcd9659dbfac06d6e1 Mon Sep 17 00:00:00 2001
From: wujingjing <gersonwu@qq.com>
Date: 星期一, 10 三月 2025 18:17:32 +0800
Subject: [PATCH] 指标附件

---
 src/utils/util.ts |  171 ++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 151 insertions(+), 20 deletions(-)

diff --git a/src/utils/util.ts b/src/utils/util.ts
index 5821a21..8c3aeff 100644
--- a/src/utils/util.ts
+++ b/src/utils/util.ts
@@ -1,15 +1,50 @@
-import type { Ref } from 'vue';
-import JSONbig from 'json-bigint';
 import { ElMessage, ElMessageBox } from 'element-plus';
+import JSONbig from 'json-bigint';
+import { storeToRefs } from 'pinia';
+import type { Ref } from 'vue';
 import { useTagsViewRoutes } from '/@/stores/tagsViewRoutes';
 import { useThemeConfig } from '/@/stores/themeConfig';
-import { storeToRefs } from 'pinia';
 import request from '/@/utils/request';
 // 瀵煎叆渚濊禆
+import axios from 'axios';
 import FileSaver from 'file-saver';
 import * as XLSX from 'xlsx';
-import axios from 'axios';
 import { MAIN_URL } from '../constants';
+import { saveAs } from 'file-saver';
+
+/**
+ * 鏅�氬璞¤浆涓� formData
+ * @param obj
+ */
+export const toFormData = (obj: any) => {
+	const formData = new FormData();
+
+	const addFormData = (subObj, prePrefix = '', isArray = false) => {
+		for (const key in subObj) {
+			if (Object.prototype.hasOwnProperty.call(subObj, key)) {
+				const value = subObj[key];
+				let currentKey = '';
+				if (prePrefix === '') {
+					currentKey = key;
+				} else if (isArray) {
+					currentKey = `${prePrefix}[${key}]`;
+				} else {
+					currentKey = `${prePrefix}.${key}`;
+				}
+
+				if (value != null && Array.isArray(value) && value.length > 0) {
+					addFormData(value, currentKey, true);
+				} else if (value != null && typeof value === 'object' && Object.values(value).length > 0) {
+					addFormData(value, currentKey, false);
+				} else {
+					formData.append(currentKey, value);
+				}
+			}
+		}
+	};
+	addFormData(obj);
+	return formData;
+};
 
 /**
  * @description 褰撶鍒� JSON 涓瓨鍦ㄨ繃闀跨殑鏁板瓧鏃讹紝浣跨敤 JSONbigString 瑙f瀽锛屾暟瀛椾細杞负瀛楃涓插鐞�
@@ -250,11 +285,11 @@
 	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;
-	
+
 	for (let index = 0; index < treeData.length; index++) {
 		const value = treeData[index] as any;
 		if (markParent) {
@@ -503,28 +538,37 @@
 
 	themeConfig.value.isTagsview = !isHide;
 };
-
 /**
  * 鏈�杩� n 澶╃殑 startDate銆乪ndDate
  * @param dates
  */
-export const getRecentDateRange = (dates: number) => {
+export const getRecentDateRange = (dates: number, includesCurrent = true) => {
+	dates = includesCurrent ? dates - 1 : dates;
 	// 鑾峰彇褰撳墠鏃ユ湡
-	const currentDate = new Date();
-
+	const endDate = new Date();
 	const startDate = new Date();
-	startDate.setDate(currentDate.getDate() - dates + 1);
-	// 灏嗘椂鍒嗙璁句负0
-	startDate.setHours(0);
-	startDate.setMinutes(0);
-	startDate.setSeconds(0);
-	// 鑾峰彇褰撳墠鏃ユ湡浣滀负缁堟鑺傜偣
-	const endDate = currentDate;
-	return { startDate, endDate };
+	startDate.setTime(startDate.getTime() - 3600 * 1000 * 24 * dates);
+	endDate.setHours(23, 59, 59, 59);
+	startDate.setHours(0, 0, 0, 0);
+	return [startDate, endDate];
+};
+
+/**
+ * 鏈�杩� n 澶╃殑 date
+ * @param dates
+ */
+export const getRecentDate = (dates: number) => {
+	// 鑾峰彇褰撳墠鏃ユ湡
+	const recentDate = new Date();
+	recentDate.setTime(recentDate.getTime() - 3600 * 1000 * 24 * dates);
+	recentDate.setHours(0, 0, 0, 0);
+	return recentDate;
 };
 
 //#region ====================== 鏈�杩戞椂闂� ======================
-export const getAWeek = () => getRecentDateRange(7);
+export const getAWeek = () => {
+	return getRecentDateRange(7);
+};
 export const getHalfMonth = () => getRecentDateRange(15);
 export const getAMonth = () => getRecentDateRange(30);
 export const getThreeMonth = () => getRecentDateRange(90);
@@ -578,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 + '';
 };
 
 /**
@@ -656,3 +715,75 @@
 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;
+}
+
+/**
+ * 浼戠湢鎸囧畾绉掓暟
+ * @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