递归创建下拉树数据

import { ref, onMounted } from 'vue'

import { ComponentOptions } from '@/types/components'

import { queryZsdt006TreeInfo } from '@/api/system-manage/base-data'

// 下拉树数据
const deptData = ref<ComponentOptions[]>([])

// 获取下拉树数据
const getTreeData = async (): Promise<void> => {
  type TreeData = {
    departmentName: string
    departmentCode: string
    childrenList: TreeData[]
  }

  // 创建建下拉树方法
  const generateTreeData = (arr: TreeData[]): ComponentOptions[] => {
    return arr.map(({ departmentCode, departmentName, childrenList }) => {
      return {
        label: departmentName,
        value: departmentCode,
        children: childrenList?.length ? generateTreeData(childrenList) : []
      }
    })
  }

  const { data } = await queryZsdt006TreeInfo()

  const tempDeptData = generateTreeData(data)

  deptData.value = tempDeptData
}

onMounted(getTreeData)