gaoluyang
2 天以前 d6723c2a0b2d5262ae156d71eadde66a884084eb
src/utils/standardTree.js
@@ -1,7 +1,8 @@
/**
 * 标准库树通用工具
 *
 * 层级不固定:children 为 null / undefined / 空数组的节点即为「标准」叶子。
 * 标准库固定两级:code=[1] 为行业,code=[2] 为标准。
 * 兼容没有 code 的旧接口数据时,再回退到 children 判断叶子。
 * 树路径统一由 " - " 连接,格式与历史实现逐字节保持一致:
 * 顶层真实节点的 parent 是 Element UI 的虚拟根,因此根节点会进入路径。
 */
@@ -9,6 +10,8 @@
// 是否叶子(标准)
export function isLeaf(data) {
  if (!data) return true;
  if (data.code === '[1]') return false;
  if (data.code === '[2]') return true;
  const children = data.children;
  return children === null || children === undefined || children.length === 0;
}
@@ -33,7 +36,10 @@
  let cur = node;
  while (cur && cur.parent) {
    if (isLeaf(cur.data)) {
      tokens.push(cur.label, leafMarker);
      const leafValue = cur.data && cur.data.standardMethodId
        ? (cur.data.value || cur.label)
        : cur.label;
      tokens.push(leafValue, leafMarker);
    } else {
      tokens.push(cur.label);
    }
@@ -72,6 +78,14 @@
// 去掉路径末段,用于「当前是叶子但查不到标准」时回退到父级查询
export function dropLastLabel(path) {
  if (!path) return '';
  const idx = path.lastIndexOf(' - ');
  return idx === -1 ? '' : path.slice(0, idx);
  const tokens = path.split(' - ');
  if (tokens.length <= 1) return '';
  // 叶子路径格式为“行业 - null - 标准”或“行业 -  - 标准”。
  // 去掉标准名后还要去掉仅用于标识叶子的占位段,父路径才是真实的行业路径。
  tokens.pop();
  const marker = tokens[tokens.length - 1];
  if (marker === '' || marker === 'null') {
    tokens.pop();
  }
  return tokens.join(' - ');
}