吉安感知网项目-前端
shuishen
2026-02-03 89380e6260a75d1d3b94de687ebcc2f50d50659d
applications/task-work-order/src/views/orderView/organizational/zoningManagement/FormDiaLog.vue
@@ -22,7 +22,7 @@
      </el-col>
      <el-col :span="12">
        <div class="label">区划级别</div>
        <div class="val"> {{ getDictLabel(formData.regionLevel, dictObj.divisionLevel) }}</div>
        <div class="val"> {{ getDictLabel(formData.regionLevel, dictObj.regionLevel) }}</div>
      </el-col>
      <el-col :span="12">
        <div class="label">区划排序</div>
@@ -56,24 +56,10 @@
        </el-col>
        <el-col :span="12">
          <el-form-item label="上级区划" prop="parentCode">
            <el-tree-select
            <RegionSelect
              class="gd-select"
              popper-class="gd-tree-select-popper"
              v-model="formData.parentCode"
              node-key="id"
              :default-expanded-keys="expandedKeys"
              :props="{
                value: 'id',
                label: 'name',
                children: 'children'
              }"
              placeholder="请选择"
              clearable
              filterable
              :check-strictly="true"
              lazy
              :load="loadRegionNode"
              :render-after-expand="false"
            />
          </el-form-item>
        </el-col>
@@ -86,7 +72,7 @@
              placeholder="请选择"
              clearable
            >
              <el-option v-for="item in dictObj.divisionLevel" :key="item.dictKey"
              <el-option v-for="item in dictObj.regionLevel" :key="item.dictKey"
                        :label="item.dictValue"
                        :value="item.dictKey" />
            </el-select>
@@ -136,9 +122,9 @@
import { fieldRules } from '@ztzf/utils'
import {
  zoningSubmitApi,
  zoningDetailApi,
  regionLazyTreeApi
  zoningDetailApi
} from './zoningApi'
import RegionSelect from '@/components/RegionSelect/main.vue'
// 初始化表单数据
const initForm = () => ({
@@ -163,8 +149,6 @@
const submitting = ref(false) // 提交中
const dialogReadonly = computed(() => dialogMode.value === 'view')
const titleEnum = ref({ edit: '编辑', view: '查看', add: '新增' })
const expandedKeys = ref([]) // 树形选择器展开的节点
const regionNodeCache = ref(new Map()) // 缓存区域节点及其父节点关系
const rules = {
  code: fieldRules(true),
@@ -207,62 +191,6 @@
    data.parentCode = String(data.parentCode)
  }
  formData.value = data
  // 如果是编辑模式,获取区域节点的路径以展开树形选择器
  if (dialogMode.value === 'edit' && data.parentCode) {
    await loadRegionPath(data.parentCode)
  }
}
// 加载区域节点路径
async function loadRegionPath(parentCode) {
  try {
    const path = []
    const queue = [{ code: '360800000000', currentPath: ['360800000000'] }]
    let found = false
    // 广度优先搜索,逐层加载节点直到找到目标节点
    while (queue.length > 0 && !found) {
      const { code, currentPath } = queue.shift()
      // 获取当前节点的子节点
      const res = await regionLazyTreeApi({ parentCode: code })
      const nodes = res?.data?.data || []
      for (const node of nodes) {
        const nodeId = node.id || node.value
        // 缓存节点及其父节点关系
        regionNodeCache.value.set(nodeId, {
          id: nodeId,
          name: node.name || node.title || node.label,
          parentId: code,
          hasChildren: node.hasChildren || false
        })
        // 检查是否找到目标节点
        if (nodeId === parentCode) {
          path.push(...currentPath, nodeId)
          found = true
          break
        }
        // 如果有子节点,加入队列继续搜索
        if (node.hasChildren) {
          queue.push({
            code: nodeId,
            currentPath: [...currentPath, nodeId]
          })
        }
      }
    }
    if (path.length > 0) {
      expandedKeys.value = path
    }
  } catch (error) {
    console.error('加载区域路径失败:', error)
  }
}
// 打开弹框
@@ -273,62 +201,9 @@
    await loadDetail()
  }
}
// 懒加载获取区域节点数据
async function loadRegionNode(node, resolve) {
  try {
    // 初始化加载时传递 code 参数,加载子节点时传递 parentCode 参数
    const params = node.data?.id ? { parentCode: node.data.id } : { code: '360800000000' }
    const parentCode = node.data?.id || '360800000000'
    const res = await regionLazyTreeApi(params)
    const nodes = res?.data?.data || []
    // 处理返回的节点数据
    const processedNodes = nodes.map(item => {
      const nodeId = item.id || item.value
      // 缓存节点及其父节点关系
      regionNodeCache.value.set(nodeId, {
        id: nodeId,
        name: item.name || item.title || item.label,
        parentId: parentCode,
        hasChildren: item.hasChildren || false
      })
      return {
        id: nodeId,
        name: item.name || item.title || item.label,
        hasChildren: item.hasChildren || false
      }
    })
    resolve(processedNodes)
  } catch (error) {
    console.error('获取区域数据失败:', error)
    resolve([])
  }
}
// 获取初始区域数据(根节点)
async function getRegionList() {
  try {
    // 懒加载模式下,只需要初始化根节点
    const res = await regionLazyTreeApi({ code: '360800000000' })
    const nodes = res?.data?.data || []
    // 处理根节点数据
    regionOptions.value = nodes.map(item => {
      return {
        id: item.id || item.value,
        name: item.name || item.title || item.label,
        hasChildren: item.hasChildren || false
      }
    })
  } catch (error) {
    console.error('获取区域数据失败:', error)
  }
}
defineExpose({ open })
onMounted(() => {
  getRegionList()
})
</script>