wangjuncheng
7 天以前 bf0eb543e2deab8a1629dd2a46f8e1cd191531e1
src/views/Home.vue
@@ -60,7 +60,7 @@
import { showDeviceDetail } from "@/store";
import { setupTokenRefresh, getDangerPoint, getAllCode } from "@/api/hpApi.js";
import { convertToWKT } from "@/utils/wktUtils";
import { getDeviceInfoSHG, getSafePoint } from "@/api/hpApi";
import { getDeviceInfoSHG, getWeather } from "@/api/hpApi";
const route = useRoute();
const simStore = useSimStore();
@@ -100,35 +100,52 @@
// 计算属性
const showDetail = computed(() => showDeviceDetail.value);
function extractAllChildrenInfoUnique(dataArray) {
  const map = new Map();
function groupTopWithLeafNodes(dataArray) {
  const result = {};
  function traverse(nodes) {
  function traverse(nodes, topLevelName = null) {
    if (!Array.isArray(nodes)) return;
    for (const node of nodes) {
      const key = node.code;
      const isLeaf = !node.children || node.children.length === 0;
      // 检查是否是最后一层(没有子节点或子节点为空数组)
      if (!node.children || node.children.length === 0) {
        if (key && !map.has(key)) {
          map.set(key, { name: node.nameChn, code: key });
      // 如果是顶层节点,记录它的名字作为 key
      if (!topLevelName && !isLeaf) {
        topLevelName = node.nameChn;
        if (!result[topLevelName]) {
          result[topLevelName] = [];
        }
      } else {
        // 如果有子节点,继续递归遍历
        traverse(node.children);
      }
      // 如果是叶子节点,加入对应数组
      if (isLeaf && topLevelName) {
        result[topLevelName].push({
          name: node.nameChn,
          code: node.code,
        });
      }
      // 继续递归子节点(保持当前顶层名称)
      if (node.children && node.children.length > 0) {
        traverse(node.children, topLevelName);
      }
    }
  }
  traverse(dataArray);
  return Array.from(map.values());
  // 遍历整个大数组中的每一项(即每一个区域)
  for (const item of dataArray) {
    if (item && item.children && Array.isArray(item.children)) {
      traverse([item]); // 把当前项包装成数组,方便统一处理
    }
}
  return result;
}
onMounted(async () => {
  getAllCode().then((res) => {
    // 北京市所有村以及街道code
    simStore.townCodeAll = extractAllChildrenInfoUnique(res.data[0].children);
    simStore.townCodeAll = groupTopWithLeafNodes(res.data[0].children);
    console.log(simStore.townCodeAll, "中科软接口获取乡镇code");
  });
  setupTokenRefresh(); // 获取宏图token
  // getSimData(); //测试tr后端