From 2800fa4f32f3900509cb4d6eefaf2bfaf54efdd7 Mon Sep 17 00:00:00 2001
From: 罗广辉 <guanghui.luo@foxmail.com>
Date: Mon, 21 Apr 2025 18:29:09 +0800
Subject: [PATCH] fix: 天气显示

---
 src/utils/cesium/kmz.js |  388 +++++++++++++++++++++++++++++--------------------------
 1 files changed, 204 insertions(+), 184 deletions(-)

diff --git a/src/utils/cesium/kmz.js b/src/utils/cesium/kmz.js
index 66e63a8..a6cf77f 100644
--- a/src/utils/cesium/kmz.js
+++ b/src/utils/cesium/kmz.js
@@ -1,91 +1,82 @@
-import axios from 'axios';
-import JsZip from 'jszip';
+import axios from 'axios'
+import JsZip from 'jszip'
 
-const noWmpl = ['Folder', 'Placemark', 'Point', 'coordinates'];
+const noWmpl = ['Folder', 'Placemark', 'Point', 'coordinates']
 
 /**
  * @description: 读取kmz文件解析出kml文件
  * @param {string} filePath 文件地址
  * @return {*}
  */
-export const analyzeKmzFile = (filePath) => {
-  return (
-    axios({
-      url: filePath,
-      method: 'get',
-      responseType: 'arraybuffer',
-      authorization: false
-    })
-      // .get(filePath, { responseType: 'arraybuffer' },)
-      .then((fileRes) => fileRes.data)
-      .then((kmzData) => JsZip.loadAsync(kmzData)) // 解压kmz文件
-      .then((zipFile) => {
-        const files = {};
-        Object.keys(zipFile.files).forEach((key) => {
-          files[key] = zipFile.files[key].async('text');
-        });
-        return {
-          zip: zipFile,
-          fileInfoObj: files,
-        };
-      })
-  );
-};
+export const analyzeKmzFile = filePath => {
+	return (
+		axios({
+			url: filePath,
+			method: 'get',
+			responseType: 'arraybuffer',
+			authorization: false,
+		})
+			// .get(filePath, { responseType: 'arraybuffer' },)
+			.then(fileRes => fileRes.data)
+			.then(kmzData => JsZip.loadAsync(kmzData)) // 解压kmz文件
+			.then(zipFile => {
+				const files = {}
+				Object.keys(zipFile.files).forEach(key => {
+					files[key] = zipFile.files[key].async('text')
+				})
+				return {
+					zip: zipFile,
+					fileInfoObj: files,
+				}
+			})
+	)
+}
 
 export const getKmlParams = (source, isWpml, params) => {
-  let regx = null;
-  if (isWpml) {
-    if (params.mode) {
-      regx = new RegExp(
-        `<wpml:${params.name}>${params.findRegx}<\\/wpml:${params.name}>`,
-        params.mode,
-      );
-    } else {
-      regx = new RegExp(
-        `<wpml:${params.name}>${params.findRegx}<\\/wpml:${params.name}>`,
-      );
-    }
-  } else {
-    if (params.mode) {
-      regx = new RegExp(
-        `<${params.name}>${params.findRegx}<\\/${params.name}>`,
-        params.mode,
-      );
-    } else {
-      regx = new RegExp(
-        `<${params.name}>${params.findRegx}<\\/${params.name}>`,
-      );
-    }
-  }
-  return source?.match(regx);
-};
+	let regx = null
+	if (isWpml) {
+		if (params.mode) {
+			regx = new RegExp(
+				`<wpml:${params.name}>${params.findRegx}<\\/wpml:${params.name}>`,
+				params.mode
+			)
+		} else {
+			regx = new RegExp(`<wpml:${params.name}>${params.findRegx}<\\/wpml:${params.name}>`)
+		}
+	} else {
+		if (params.mode) {
+			regx = new RegExp(`<${params.name}>${params.findRegx}<\\/${params.name}>`, params.mode)
+		} else {
+			regx = new RegExp(`<${params.name}>${params.findRegx}<\\/${params.name}>`)
+		}
+	}
+	return source?.match(regx)
+}
 
-export const generateKmlFormat = (settingParmas) => {
-  let paramGroup = '';
-  Object.keys(settingParmas).forEach((key) => {
-    if (
-      Object.prototype.toString.call(settingParmas[key]) === '[object Object]'
-    ) {
-      let parmaStr = '';
-      Object.keys(settingParmas[key]).forEach((v) => {
-        const xml = noWmpl.includes(key)
-          ? `<${v}>${settingParmas[key][v]}</${v}>`
-          : `<wpml:${v}>${settingParmas[key][v]}</wpml:${v}>`;
-        parmaStr += xml;
-      });
-      const xml = noWmpl.includes(key)
-        ? `<${key}>${parmaStr}</${key}>`
-        : `<wpml:${key}>${parmaStr}</wpml:${key}>`;
-      paramGroup += xml;
-    } else {
-      const xml = noWmpl.includes(key)
-        ? `<${key}>${settingParmas[key]}</${key}>`
-        : `<wpml:${key}>${settingParmas[key]}</wpml:${key}>`;
-      paramGroup += xml;
-    }
-  });
-  return paramGroup;
-};
+export const generateKmlFormat = settingParmas => {
+	let paramGroup = ''
+	Object.keys(settingParmas).forEach(key => {
+		if (Object.prototype.toString.call(settingParmas[key]) === '[object Object]') {
+			let parmaStr = ''
+			Object.keys(settingParmas[key]).forEach(v => {
+				const xml = noWmpl.includes(key)
+					? `<${v}>${settingParmas[key][v]}</${v}>`
+					: `<wpml:${v}>${settingParmas[key][v]}</wpml:${v}>`
+				parmaStr += xml
+			})
+			const xml = noWmpl.includes(key)
+				? `<${key}>${parmaStr}</${key}>`
+				: `<wpml:${key}>${parmaStr}</wpml:${key}>`
+			paramGroup += xml
+		} else {
+			const xml = noWmpl.includes(key)
+				? `<${key}>${settingParmas[key]}</${key}>`
+				: `<wpml:${key}>${settingParmas[key]}</wpml:${key}>`
+			paramGroup += xml
+		}
+	})
+	return paramGroup
+}
 
 /**
  * @description: JSON转XML
@@ -94,125 +85,154 @@
  * @return {*} string
  */
 export const JSONToXML = (obj, rootName, isEnd) => {
-  let xml = '';
+	let xml = ''
 
-  const buildXml = (obj, rootName) => {
-    let xml = '';
-    if (Array.isArray(obj)) {
-      obj.forEach((item) => {
-        const str = !noWmpl.includes(rootName) && isEnd ? 'wpml:' : '';
-        xml += `<${str}${rootName}>${buildXml(item)}</${str}${rootName}>`;
-      });
-    } else if (typeof obj === 'object') {
-      Object.keys(obj).forEach((key) => {
-        if (key === '#text') {
-          xml += obj[key];
-        } else {
-          const str = !noWmpl.includes(key) && isEnd ? 'wpml:' : '';
-          if (key === 'Placemark') {
-            xml += `${buildXml(obj[key], key)}`;
-          } else if (key === 'action') {
-            if (Array.isArray(obj[key])) {
-              xml += `${buildXml(obj[key], key)}`;
-            } else {
-              xml += `<${str}${key}>${buildXml(obj[key], key)}</${str}${key}>`;
-            }
-          } else {
-            xml += `<${str}${key}>${buildXml(obj[key], key)}</${str}${key}>`;
-          }
-        }
-      });
-    } else {
-      xml += obj;
-    }
-    return xml;
-  };
+	const buildXml = (obj, rootName) => {
+		let xml = ''
+		if (Array.isArray(obj)) {
+			obj.forEach(item => {
+				const str = !noWmpl.includes(rootName) && isEnd ? 'wpml:' : ''
+				xml += `<${str}${rootName}>${buildXml(item)}</${str}${rootName}>`
+			})
+		} else if (typeof obj === 'object') {
+			Object.keys(obj).forEach(key => {
+				if (key === '#text') {
+					xml += obj[key]
+				} else {
+					const str = !noWmpl.includes(key) && isEnd ? 'wpml:' : ''
+					if (key === 'Placemark') {
+						xml += `${buildXml(obj[key], key)}`
+					} else if (key === 'action') {
+						if (Array.isArray(obj[key])) {
+							xml += `${buildXml(obj[key], key)}`
+						} else {
+							xml += `<${str}${key}>${buildXml(obj[key], key)}</${str}${key}>`
+						}
+					} else {
+						xml += `<${str}${key}>${buildXml(obj[key], key)}</${str}${key}>`
+					}
+				}
+			})
+		} else {
+			xml += obj
+		}
+		return xml
+	}
 
-  const header = isEnd ? '<?xml version="1.0" encoding="UTF-8"?>' : '';
-  xml += `${header}
+	const header = isEnd ? '<?xml version="1.0" encoding="UTF-8"?>' : ''
+	xml += `${header}
   <kml xmlns="http://www.opengis.net/kml/2.2" xmlns:wpml="http://www.dji.com/wpmz/1.0.5">
     <Document>
       ${buildXml(obj, rootName)}
     </Document>
-  </kml>`;
-  return xml;
-};
+  </kml>`
+	return xml
+}
 
-export const getTagNameFromXml = (xmlString) => {
-  const parser = new DOMParser();
-  const xmlDoc = parser.parseFromString(xmlString, 'text/xml');
-  const element = xmlDoc.documentElement;
-  if (element && element.tagName) {
-    return element.tagName; // 返回的是类似 "WPML:USEGLOBALHEADINGPARAM" 的全大写形式
-  }
-  return null;
-};
+export const getTagNameFromXml = xmlString => {
+	const parser = new DOMParser()
+	const xmlDoc = parser.parseFromString(xmlString, 'text/xml')
+	const element = xmlDoc.documentElement
+	if (element && element.tagName) {
+		return element.tagName // 返回的是类似 "WPML:USEGLOBALHEADINGPARAM" 的全大写形式
+	}
+	return null
+}
 
 // 将xml转为json
-const deepParse = (xml) => {
-  let obj = {};
-  // 如果是文档节点,遍历其子节点
-  if (xml.nodeType === 1) {
-    // element node
-    // 处理子节点
-    if (xml.childNodes.length > 0) {
-      for (let i = 0; i < xml.childNodes.length; i++) {
-        const item = xml.childNodes[i];
-        const nodeName = item.nodeName.replace('wpml:', '');
+const deepParse = xml => {
+	let obj = {}
+	// 如果是文档节点,遍历其子节点
+	if (xml.nodeType === 1) {
+		// element node
+		// 处理子节点
+		if (xml.childNodes.length > 0) {
+			for (let i = 0; i < xml.childNodes.length; i++) {
+				const item = xml.childNodes[i]
+				const nodeName = item.nodeName.replace('wpml:', '')
 
-        // 检查是否是文本节点
-        if (item.nodeType === 3) {
-          // text node
-          // 处理文本节点的内容
-          if (item.nodeValue.trim() !== '') {
-            obj[nodeName] = item.nodeValue.trim();
-          }
-        } else if (item.nodeType === 1) {
-          // element node
-          // 递归调用处理子节点
-          if (obj[nodeName] === undefined) {
-            const nodeValue = deepParse(item);
-            if (nodeValue[nodeName] === undefined) {
-              obj[nodeName] = nodeValue;
-            } else {
-              obj[nodeName] = nodeValue[nodeName];
-            }
-          } else {
-            if (!obj[nodeName].push) {
-              const old = obj[nodeName];
-              obj[nodeName] = [];
-              obj[nodeName].push(old);
-            }
-            const nodeValue = deepParse(item);
-            if (nodeValue[nodeName] === undefined) {
-              obj[nodeName].push(nodeValue);
-            }
-          }
-        }
-      }
-    }
-  } else if (xml.nodeType === 3) {
-    // text node
-    // 处理文本节点的内容
-    obj = xml.nodeValue.trim();
-  }
-  return obj;
-};
+				// 检查是否是文本节点
+				if (item.nodeType === 3) {
+					// text node
+					// 处理文本节点的内容
+					if (item.nodeValue.trim() !== '') {
+						obj[nodeName] = item.nodeValue.trim()
+					}
+				} else if (item.nodeType === 1) {
+					// element node
+					// 递归调用处理子节点
+					if (obj[nodeName] === undefined) {
+						const nodeValue = deepParse(item)
+						if (nodeValue[nodeName] === undefined) {
+							obj[nodeName] = nodeValue
+						} else {
+							obj[nodeName] = nodeValue[nodeName]
+						}
+					} else {
+						if (!obj[nodeName].push) {
+							const old = obj[nodeName]
+							obj[nodeName] = []
+							obj[nodeName].push(old)
+						}
+						const nodeValue = deepParse(item)
+						if (nodeValue[nodeName] === undefined) {
+							obj[nodeName].push(nodeValue)
+						}
+					}
+				}
+			}
+		}
+	} else if (xml.nodeType === 3) {
+		// text node
+		// 处理文本节点的内容
+		obj = xml.nodeValue.trim()
+	}
+	return obj
+}
 
 /**
  * @description: XML转JSON
  * @param {string} xmlStr xml字符串
  * @return {*} string
  */
-export const XMLToJSON = (xmlStr) => {
-  const parser = new DOMParser();
-  const xmlDoc = parser.parseFromString(xmlStr, 'text/xml');
-  const xmlObj = deepParse(xmlDoc.documentElement);
-  if (Reflect.has(xmlObj, 'Document')) {
-    return xmlObj;
-  } else {
-    return {
-      Document: xmlObj,
-    };
-  }
-};
+export const XMLToJSON = xmlStr => {
+	const parser = new DOMParser()
+	const xmlDoc = parser.parseFromString(xmlStr, 'text/xml')
+	const xmlObj = deepParse(xmlDoc.documentElement)
+	if (Reflect.has(xmlObj, 'Document')) {
+		return xmlObj
+	} else {
+		return {
+			Document: xmlObj,
+		}
+	}
+}
+
+
+export function removeTextKey(obj) {
+	if (typeof obj !== 'object' || obj === null) {
+		// 如果是数值字符串,转换为数值
+		if (typeof obj === 'string' && /^-?\d+(\.\d+)?$/.test(obj)) {
+			return Number(obj);
+		}
+		return obj;
+	}
+
+	if (Array.isArray(obj)) {
+		return obj.map(item => removeTextKey(item));
+	}
+
+	if (Object.prototype.hasOwnProperty.call(obj, '#text')) {
+		// 如果 #text 的值是数值字符串,转换为数值
+		const textValue = obj['#text'];
+		return typeof textValue === 'string' && /^-?\d+(\.\d+)?$/.test(textValue) ? Number(textValue) : textValue;
+	}
+
+	const newObj = {};
+	for (const key in obj) {
+		if (Object.prototype.hasOwnProperty.call(obj, key)) {
+			newObj[key] = Object.keys(obj[key]).length === 0 ? '' : removeTextKey(obj[key]);
+		}
+	}
+	return newObj;
+}

--
Gitblit v1.9.3