import { defineStore } from 'pinia'; import { ref } from 'vue'; import { getMapList, getModuleMapLegendList } from '@/api/map'; type MapConfigLoadOptions = { systemId: string; moduleId: string; pageKey?: string; description?: string; }; const cloneConfigData = (data: T): T => { return JSON.parse(JSON.stringify(data)); }; export const useMapConfigStore = defineStore('map-config', () => { const layerConfigTree = ref([]); const layerConfigByKey = ref>({}); const legendConfigOriginal = ref([]); const legendConfigByNameEn = ref>({}); const legendConfigByLayerCode = ref>({}); const pageLegendConfig = ref([]); const configLoading = ref(false); const lastLoadOptions = ref(null); // 备注:统一规范图例 nameEn,避免 `_测试` 后缀影响索引命中。 const normalizeLegendNameEn = (nameEn?: string): string => { if (!nameEn) return ''; return nameEn.includes('_测试') ? nameEn.split('_测试')[0] : nameEn; }; // 备注:重建图层配置索引,后续按 key 读取图层配置时不再全量递归。 const rebuildLayerConfigIndex = (items: any[] = []) => { const nextIndex: Record = {}; const walk = (nodes: any[] = []) => { nodes.forEach(item => { if (item?.key) { nextIndex[item.key] = item; } if (item?.children?.length > 0) { walk(item.children); } }); }; walk(items); layerConfigByKey.value = nextIndex; }; // 备注:从图层配置中提取默认勾选项,作为页面初始化时的基础勾选状态。 const extractCheckedLayerKeys = (items: any[] = []): string[] => { const keys: string[] = []; const walk = (nodes: any[] = []) => { nodes.forEach(item => { if (item?.checked === 1 && item?.key) { keys.push(item.key); } if (item?.children?.length > 0) { walk(item.children); } }); }; walk(items); return keys; }; // 备注:重建图例配置索引,分别支持按 nameEn 和 layerCode 查询图例配置。 const rebuildLegendConfigIndexes = (items: any[] = []) => { const nextNameEnMap: Record = {}; const nextLayerCodeMap: Record = {}; const walk = (nodes: any[] = []) => { nodes.forEach(item => { if (item?.childrenList?.length > 0) { walk(item.childrenList); return; } const normalizedNameEn = normalizeLegendNameEn(item?.nameEn); if (normalizedNameEn) { nextNameEnMap[normalizedNameEn] = item; } if (item?.layerCode) { if (!nextLayerCodeMap[item.layerCode]) { nextLayerCodeMap[item.layerCode] = []; } nextLayerCodeMap[item.layerCode].push(item); } }); }; walk(items); legendConfigByNameEn.value = nextNameEnMap; legendConfigByLayerCode.value = nextLayerCodeMap; }; // 备注:设置图层树配置,并同步初始化图层索引与默认勾选状态。 const setLayerConfigTree = (data: any[] = []) => { const nextData = cloneConfigData(data); layerConfigTree.value = nextData; rebuildLayerConfigIndex(nextData); }; // 备注:设置全量原始图例配置,并同步构建图例索引。 const setLegendConfigOriginal = (data: any[] = []) => { const nextData = cloneConfigData(data); legendConfigOriginal.value = nextData; rebuildLegendConfigIndexes(nextData); }; // 备注:设置页面级图例配置,仅保留后端返回的页面图例结构,不在这里做运行态加工。 const setPageLegendConfig = (data: any[] = []) => { pageLegendConfig.value = cloneConfigData(data); }; // 备注:按图层 key 获取图层配置,供后续编排层和运行态 store 复用。 const getLayerConfigByKey = (layerKey: string) => { return layerConfigByKey.value[layerKey]; }; // 备注:按图层 layerCode 获取原始图例项列表,供图层勾选后派生图例使用。 const getLegendConfigByLayerCode = (layerCode: string): any[] => { return legendConfigByLayerCode.value[layerCode] || []; }; // 备注:按图例 nameEn 获取原始图例项,供运行态读取默认 checked 配置。 const getLegendConfigByNameEn = (nameEn: string) => { const normalizedNameEn = normalizeLegendNameEn(nameEn); return legendConfigByNameEn.value[normalizedNameEn]; }; // 备注:统一清理配置 store,便于菜单切换或地图销毁后重置配置状态。 const clearConfigState = () => { layerConfigTree.value = []; layerConfigByKey.value = {}; legendConfigOriginal.value = []; legendConfigByNameEn.value = {}; legendConfigByLayerCode.value = {}; pageLegendConfig.value = []; lastLoadOptions.value = null; }; // 备注:加载页面地图配置,只负责请求和保存配置数据,不处理锚点或地图渲染逻辑。 const loadPageMapConfig = async (options: MapConfigLoadOptions) => { configLoading.value = true; lastLoadOptions.value = { ...options }; try { const [layerRes, legendAllRes, legendPageRes] = await Promise.all([ getMapList({ systemId: options.systemId, moduleId: options.moduleId, description: options.description ?? 'true' }), getModuleMapLegendList(), getModuleMapLegendList( options.pageKey ? { moduleId: options.pageKey } : undefined ) ]); const layerConfig = layerRes?.data?.mapLayerVos || []; const legendOriginal = legendAllRes?.data || []; const pageLegend = legendPageRes?.data || []; setLayerConfigTree(layerConfig); setLegendConfigOriginal(legendOriginal); setPageLegendConfig(pageLegend); return { layerConfig, legendOriginal, pageLegend }; } finally { configLoading.value = false; } }; return { layerConfigTree, layerConfigByKey, legendConfigOriginal, legendConfigByNameEn, legendConfigByLayerCode, pageLegendConfig, configLoading, lastLoadOptions, normalizeLegendNameEn, rebuildLayerConfigIndex, extractCheckedLayerKeys, rebuildLegendConfigIndexes, setLayerConfigTree, setLegendConfigOriginal, setPageLegendConfig, getLayerConfigByKey, getLegendConfigByLayerCode, getLegendConfigByNameEn, clearConfigState, loadPageMapConfig }; });