2026-04-03 16:04:16 +08:00
|
|
|
|
import { defineStore } from 'pinia';
|
|
|
|
|
|
import { ref } from 'vue';
|
|
|
|
|
|
|
|
|
|
|
|
export const useMapStore = defineStore('map', () => {
|
2026-04-22 17:53:20 +08:00
|
|
|
|
const layerData = ref([]);
|
|
|
|
|
|
const legendData = ref([]);
|
2026-04-03 17:04:34 +08:00
|
|
|
|
const legendDataSelected: any = ref([]);
|
2026-04-22 17:53:20 +08:00
|
|
|
|
|
|
|
|
|
|
const setLayerData = (data: any) => {
|
|
|
|
|
|
layerData.value = data;
|
|
|
|
|
|
};
|
|
|
|
|
|
const setLegendData = (data: any) => {
|
|
|
|
|
|
legendData.value = data;
|
|
|
|
|
|
};
|
2026-04-03 17:04:34 +08:00
|
|
|
|
// 递归过滤 childrenList(同时处理 ifShow 和 layerCode 匹配)
|
|
|
|
|
|
const filterChildrenList = (
|
|
|
|
|
|
childrenList: any[],
|
|
|
|
|
|
checkKeys: string[]
|
|
|
|
|
|
): any[] => {
|
|
|
|
|
|
return childrenList
|
|
|
|
|
|
.filter(child => child.ifShow !== 0) // 过滤 ifShow == 0
|
|
|
|
|
|
.map(child => {
|
|
|
|
|
|
if (child.childrenList?.length > 0) {
|
|
|
|
|
|
const filtered = filterChildrenList(child.childrenList, checkKeys);
|
|
|
|
|
|
return filtered.length > 0
|
|
|
|
|
|
? { ...child, childrenList: filtered }
|
|
|
|
|
|
: null;
|
2026-04-03 16:04:16 +08:00
|
|
|
|
}
|
2026-04-03 17:04:34 +08:00
|
|
|
|
return checkKeys.includes(child.layerCode) ? child : null;
|
|
|
|
|
|
})
|
|
|
|
|
|
.filter(Boolean); // 移除 null 项
|
|
|
|
|
|
};
|
2026-04-03 16:04:16 +08:00
|
|
|
|
|
2026-04-03 17:04:34 +08:00
|
|
|
|
// 获取图例数据(去重 + 过滤)
|
|
|
|
|
|
const getlegendData = (checkKeys: string[], addedParents: Set<string>) => {
|
|
|
|
|
|
return legendData.value
|
2026-04-22 17:53:20 +08:00
|
|
|
|
.filter((legendItem: any) => {
|
2026-04-03 17:04:34 +08:00
|
|
|
|
// 检查是否包含匹配的 layerCode(任意层级)
|
|
|
|
|
|
const hasMatch = (list: any[]): boolean => {
|
|
|
|
|
|
return list.some(
|
|
|
|
|
|
child =>
|
|
|
|
|
|
child.ifShow !== 0 &&
|
|
|
|
|
|
(checkKeys.includes(child.layerCode) ||
|
|
|
|
|
|
hasMatch(child.childrenList || []))
|
|
|
|
|
|
);
|
|
|
|
|
|
};
|
|
|
|
|
|
return hasMatch(legendItem.childrenList);
|
|
|
|
|
|
})
|
2026-04-22 17:53:20 +08:00
|
|
|
|
.filter((legendItem: any) => {
|
2026-04-03 17:04:34 +08:00
|
|
|
|
// 去重
|
|
|
|
|
|
if (addedParents.has(legendItem.name)) return false;
|
|
|
|
|
|
addedParents.add(legendItem.name);
|
2026-04-03 16:04:16 +08:00
|
|
|
|
return true;
|
2026-04-03 17:04:34 +08:00
|
|
|
|
})
|
2026-04-22 17:53:20 +08:00
|
|
|
|
.map((legendItem: any) => ({
|
2026-04-03 17:04:34 +08:00
|
|
|
|
...legendItem,
|
|
|
|
|
|
childrenList: filterChildrenList(legendItem.childrenList, checkKeys)
|
|
|
|
|
|
}));
|
|
|
|
|
|
};
|
2026-04-03 16:04:16 +08:00
|
|
|
|
|
2026-04-03 17:04:34 +08:00
|
|
|
|
// 更新图层数据
|
|
|
|
|
|
const updateLayerData = (checkKeys: string[]) => {
|
|
|
|
|
|
const addedParents: Set<string> = new Set();
|
|
|
|
|
|
const selectedLegend: any[] = [];
|
2026-04-03 16:04:16 +08:00
|
|
|
|
|
2026-04-03 17:04:34 +08:00
|
|
|
|
// 扁平化所有图层项
|
|
|
|
|
|
const allItems = layerData.value.flatMap((item: any) =>
|
|
|
|
|
|
item.children?.length > 0 ? item.children : [item]
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// 更新 checked 状态并收集选中的 key
|
|
|
|
|
|
allItems.forEach((item: any) => {
|
|
|
|
|
|
item.checked = checkKeys.includes(item.key) ? 1 : 0;
|
|
|
|
|
|
if (
|
|
|
|
|
|
item.key !== '-' &&
|
|
|
|
|
|
item.key !== 'customBaseLayer' &&
|
|
|
|
|
|
item.key !== 'powerBaseStation' &&
|
|
|
|
|
|
item.checked === 1
|
|
|
|
|
|
) {
|
|
|
|
|
|
selectedLegend.push(...getlegendData([item.key], addedParents));
|
2026-04-03 16:04:16 +08:00
|
|
|
|
}
|
2026-04-03 17:04:34 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
legendDataSelected.value = selectedLegend;
|
|
|
|
|
|
};
|
2026-04-03 16:04:16 +08:00
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
layerData,
|
2026-04-22 17:53:20 +08:00
|
|
|
|
setLayerData,
|
2026-04-03 16:04:16 +08:00
|
|
|
|
legendData,
|
2026-04-22 17:53:20 +08:00
|
|
|
|
setLegendData,
|
2026-04-03 16:04:16 +08:00
|
|
|
|
legendDataSelected,
|
|
|
|
|
|
updateLayerData
|
|
|
|
|
|
};
|
|
|
|
|
|
});
|