/** * 根据行政区划代码查找对应的行政区划名称 * @param {string|number} areaCode - 行政区划代码 * @param {Array} regionalData - 行政区划树形数据 * @param {boolean} [needFullPath=false] - 是否需要完整路径 * @returns {string} 行政区划名称或完整路径 */ export const findAreaName = (areaCode, regionalData = [], needFullPath = false) => { if (!regionalData || regionalData.length === 0) return areaCode; const normalizeCode = code => { if (!code) return ''; const strCode = String(code).replace(/0+$/, ''); return strCode.length >= 6 ? strCode : ''; }; const targetCode = normalizeCode(areaCode); const findInTree = (treeNodes, parentPath = []) => { for (const node of treeNodes) { const currentPath = [...parentPath, node.name]; if (normalizeCode(node.id) === targetCode) { return needFullPath ? currentPath.join('/') : node.name; } if (node.childrens?.length > 0) { const found = findInTree(node.childrens, currentPath); if (found) return found; } } return null; }; return findInTree(regionalData) || areaCode; };