import { runLogic } from '@af-mobile-client-vue3/services/api/common' import { METHOD, request } from '@af-mobile-client-vue3/utils/http/index' function getLeafNodes(nodes) { // console.log(nodes) // 确保 nodes 是数组 const nodeArray = Array.isArray(nodes) ? nodes : [nodes] return nodeArray.reduce((leaves, node) => { if (node.children && node.children.length) { // 递归处理子节点并合并结果 leaves.push(...getLeafNodes(node.children)) } else { // 当前节点是叶子节点时,直接加入结果 leaves.push(node) } return leaves }, []) } function getLeafNodesByCondition(type, nodes, parent = null) { // console.log('nodes------',nodes) // 确保 nodes 是数组 const nodeArray = Array.isArray(nodes) ? nodes : [nodes] return nodeArray.reduce((leaves, node) => { const isValidNode = node.resourcetype === type && node.name !== '组织机构' const updatedParent = node.name === '组织机构' ? null : node.name if (node.children && node.children.length) { // 当前节点符合条件时,加入叶子节点列表 if (isValidNode && node.resourcetype === 'organization') { leaves.push({ ...node, name: parent ? `${node.name}` : node.name, children: [], }) } if (isValidNode && node.resourcetype === 'department') { leaves.push({ ...node, name: parent ? `${node.name}-${parent}` : node.name, children: [], }) } // 递归处理子节点 leaves.push(...getLeafNodesByCondition(type, node.children, updatedParent)) } else if (isValidNode) { // 无子节点但符合条件时,直接加入叶子节点列表 if (node.resourcetype === 'organization') { leaves.push({ ...node, name: parent ? `${node.name}-${parent}` : node.name, }) } else if (node.resourcetype === 'department' && !nodeArray.includes(node.name)) { // console.log('数组',nodeArray.includes(node.name)) leaves.push({ ...node, name: parent ? `${node.name}-${parent}` : node.name, }) } } return leaves }, []) } function transformData(inputData) { if (!inputData || !Array.isArray(inputData)) return [] function transform(node) { if (!node || typeof node !== 'object') return {} let children = [] if (node.children) { if (Array.isArray(node.children)) { children = node.children.map(transform) } else if (typeof node.children === 'object') { // 检查是否是空标记 if (node.children.empty === true) { children = [] } else if (node.children.id || node.children.name) { // 如果是单个节点对象 children = [transform(node.children)] } } } return { label: node.name || '', value: node.id ?? null, f_organization_id: node.f_organization_id, f_department_id: node.f_department_id, parentid: node.parentid || node.parentId || node.parent_id || null, orgid: node.orgid, children, } } return inputData.map(transform) } function getResData(params, toCallback) { const data = { userId: params.userid, roleName: params.roleName, filter: params.filter, filterType: params.filterType } if (params.source === '获取分公司') { runLogic('getOrgBySearch', data, 'af-system').then(res => toCallback(res)) } else if (params.source === '获取部门') { runLogic('getDepBySearch', data, 'af-system').then(res => toCallback(res)) } else if (params.source === '获取人员') { runLogic('getUserBySearch', data, 'af-system').then(res => toCallback(res)) } else if (params.source === '根据角色获取人员') { runLogic('getUserBySearchRole', data, 'af-system').then(res => toCallback(res)) } else { return search(params).then(res => toCallback(res)) } } export async function searchToOption(params, callback) { function toCallback(res) { if (res.length) { if (res[0].children && res[0].children.length) { if (res[0].children[0].children) { callback(transformData(res[0].children[0].children)) } else { callback(transformData(res[0].children)) } } else { callback(res[0].children) } } else { callback(res) } } await getResData(params, toCallback) } export async function searchToListOption(params, callback) { function toCallback(res) { if (params.source.includes('人员')) { // console.log('ren---------',res) callback(transformData(getLeafNodes(res))) } else { const type = params.source.includes('公司') ? 'organization' : 'department' // console.log('bumenpgonngsi',res) callback(transformData(getLeafNodesByCondition(type, res))) } } await getResData(params, toCallback) } export async function search(params) { return request('/rs/search', METHOD.POST, params, params.config) } export default { searchToOption, search }