Browse Source

选择用户,性能优化

lwx_v27
liwenxuan 1 month ago
parent
commit
8f9bc91292
  1. 478
      src/components/DesignForm/public/expand/rangedUserTree.vue
  2. 36
      src/components/DesignForm/public/form/form.vue

478
src/components/DesignForm/public/expand/rangedUserTree.vue

@ -15,24 +15,11 @@ const props = withDefaults(
)
interface User {
id: number
name: string
number: string
key: string
icon: string
department: string
role: string
label: string
value: string
}
const emits = defineEmits<{
(e: 'update:modelValue', value: string): void
}>()
const value = ref([])
const loading = ref(true)
watch(value,(newValue)=>{
if(newValue.length > 0){
let str = ""
@ -56,6 +43,8 @@ watch(value,(newValue)=>{
}
},{ deep: true })
function parseStringToArray(str:string) {
try {
// JSON
@ -73,17 +62,14 @@ function parseStringToArray(str:string) {
}
}
onBeforeMount(() => {
//
//checkReady()
//getTree()
checkorgAndManTree()
setTimeout(()=>{
value.value = parseStringToArray(props.modelValue)
},500 )
})
const url = "/javasys/lowCode/manCont/getManContsByKeys"
//console.log(attrs.value)
const keys = computed(()=>{
if(attrs.queryBy == 'role'){
return attrs.roleRange
@ -92,295 +78,273 @@ const keys = computed(()=>{
}
})
const options = ref([])
const convertToAFormatComplate = ref(false)
function convertToAFormat(bTree, idArray) {
const nodeMap = {};
//
function collectNodes(node, level = 0) {
if (!node) {
//console.log(`: ${node}`);
return;
}
// 便
//console.log(` [${level}]: id=${node.id}, label=${node.label}`);
// id
if (node.id !== undefined && node.id !== null) {
nodeMap[node.id] = node;
} else {
//console.warn(`id:`, node);
}
//
if (Array.isArray(node.children)) {
node.children.forEach((child, index) => {
collectNodes(child, level + 1); // +1便
});
} else if (node.children !== null && node.children !== undefined) {
//console.warn(`children:`, node.children);
// 使
const treeData = {
"id": "102",
"label": "企管部",
"parentId": "309",
"children": [
{
"id": "284148167597887488",
"label": "荣潇亭",
"parentId": "102",
"children": null,
"value": "303086",
"treeAttrs": null,
"disabled": null,
"type": null
},
{
"id": "272",
"label": "企管",
"parentId": "102",
"children": [
{
"id": "284148287831805952",
"label": "吴可楠",
"parentId": "272",
"children": null,
"value": "303122",
"treeAttrs": null,
"disabled": null,
"type": null
}
],
"value": null,
"treeAttrs": null,
"disabled": null,
"type": null
}
}
//
if (Array.isArray(bTree)) {
//console.log(`bTree${bTree.length}`);
bTree.forEach((root, index) => {
//console.log(`${index + 1}`);
collectNodes(root);
});
} else {
//console.log(`bTree`);
collectNodes(bTree);
}
// id
//console.log(`id:`, Object.keys(nodeMap));
// idArray
const result = idArray.map(id => {
// idnodeMap
//console.log(`id: ${id}: ${id in nodeMap}`);
const targetNode = nodeMap[id];
if (!targetNode) return null;
return {
id: targetNode.id,
number: targetNode.value ?? '', // null/undefined
name: targetNode.label ?? '',
icon: null,
key: String(targetNode.id), // keya
role: '',
deparment: '',
parentId: targetNode.parentId ?? ''
};
}).filter(Boolean);
//console.log(`:`, result);
convertToAFormatComplate.value = true
return result;
}
function getManConts(){
],
"value": null,
"treeAttrs": null,
"disabled": null,
"type": null
};
const idList = ["284148167597887488", "284148287831805952"];
let data = convertToAFormat(props.orgAndManTree,keys.value)
data.forEach((element: any) => {
element.icon = element.name+"("+element.number+")"
element.deparment = ""+element.deparment+">"+element.icon
element.label = element.deparment
element.value = element.icon
});
options.value = data
const resData = ref()
const checkorgAndManTree = (()=>{
if(props.orgAndManTree&&props.orgAndManTree.children&&props.orgAndManTree.children.length>0){
//alert(1)
let arr = []
arr.push(modifyTreeData(props.orgAndManTree,keys.value))
}
/**
@ 作者: 秦东
@ 时间: 2025-06-10 11:13:06
@ 功能: 输出结果
*/
const valPrint = (val:any) => {
if(Array.isArray(val)){
return val.join("、")
}
}
const resData = computed(()=>{
if(props.orgAndManTree){
let data1 = processTreeData(props.orgAndManTree)
//console.log(data1)
getManConts()
let data2 = filterTreeByList(data1.children, options.value)
resData.value = arr
}else{
setTimeout(()=>{
checkorgAndManTree()
},100)
}
})
data1.children = data2
loading.value = false
return enrichTreeWithEmployeeInfo(options.value, data2)
/* const resData = computed(()=>{
if(props.orgAndManTree&&props.orgAndManTree.children&&props.orgAndManTree.children.length>0){
console.log(11111)
let arr = []
arr.push(modifyTreeData(props.orgAndManTree,keys.value))
return arr
}else{
return []
}
})
/**
* 根据列表数据过滤树形数据保留value值在列表number中匹配的节点及其祖先
* @param {Array} treeData - 树形数据
* @param {Array} listData - 列表数据
* @returns {Array} 过滤后的树形数据
*/
function filterTreeByList(treeData: string | any[], listData: any[]) {
// number
const numberSet = new Set();
listData.forEach((item: { number: unknown; }) => {
numberSet.add(item.number);
});
// 访
const visitedNodes = new Set();
//
function processNode(node: { id: any; value: unknown; children: string | any[] | null; }) {
// 使id
const nodeId = node.id;
if (visitedNodes.has(nodeId)) {
return false; // 访false
}
// 访
visitedNodes.add(nodeId);
//
const isDirectMatch = node.value && numberSet.has(node.value);
// childrennull
if (!node.children || node.children === null) {
return isDirectMatch;
const resultExample = [{
"id": "102",
"label": "企管部",
"parentId": "309",
"children": [
{
"id": "284148167597887488",
"label": "荣潇亭(303086)",
"parentId": "102",
"children": null,
"value": "303086",
"treeAttrs": null,
"disabled": null,
"type": null,
"number": "荣潇亭(303086)"
},
{
"id": "272",
"label": "企管",
"parentId": "102",
"children": [
{
"id": "284148287831805952",
"label": "吴可楠(303122)",
"parentId": "272",
"children": null,
"value": "303122",
"treeAttrs": null,
"disabled": null,
"type": null,
"number": "吴可楠(303122)"
}
],
"value": null,
"treeAttrs": null,
"disabled": null,
"type": null
}
],
"value": null,
"treeAttrs": null,
"disabled": null,
"type": null
}]
//
const childrenToKeep = [];
//
for (let i = 0; i < node.children.length; i++) {
const child = node.children[i];
//
if (processNode(child)) {
// childrenToKeep
childrenToKeep.push(child);
}
}
// children
node.children = childrenToKeep;
//
return isDirectMatch || childrenToKeep.length > 0;
}
//
const result = [];
for (let i = 0; i < treeData.length; i++) {
//
const nodeCopy = JSON.parse(JSON.stringify(treeData[i]));
if (processNode(nodeCopy)) {
result.push(nodeCopy);
}
}
function modifyTreeData(treeData, idList) {
console.log("modifyTreeData 执行了")
//
const newTreeData = JSON.parse(JSON.stringify(treeData));
return result;
}
// idList
const idArray = Array.isArray(idList) ? idList : [idList];
if (idArray.length === 0) return Array.isArray(newTreeData) ? [] : null;
// 1. ididid
const nodeMap = new Map(); // id
const parentMap = new Map(); // id id
const multiParentIds = new Set(); // id
function collectNodes(node, parentId) {
if (!node || typeof node !== 'object' || !node.id) return; //
/**
* a 类型员工数据映射到树形结构中为匹配的节点添加 numberroledeparmentempType 属性
* @param {Array} employees - a 类型员工数组每个元素包含 number, name, role, deparment, empType, key 等字段
* @param {Array} treeData - 树形结构数据可能包含嵌套的 children
* @returns {Array} - 更新后的树形结构
*/
function enrichTreeWithEmployeeInfo(employees: any[], treeData: string | any[]) {
// value key 便
const employeeMap = new Map();
employees.forEach((emp: { number: any; }) => {
employeeMap.set(emp.number, emp); // value a number
});
//
if (!nodeMap.has(node.id)) {
nodeMap.set(node.id, node);
}
//
function traverse(nodes: any[]) {
if (!Array.isArray(nodes)) return;
nodes.forEach(node => {
// value
if (node.value !== null && node.value !== undefined) {
const emp = employeeMap.get(node.value);
if (emp) {
// a
node.label = node.label + "(" + emp.number + ")";
node.number = node.label
node.role = emp.role;
node.deparment = emp.deparment;
node.empType = emp.empType;
// namekey
// node.name = emp.name;
// node.key = emp.key;
}
//
if (parentMap.has(node.id)) {
//
if (parentMap.get(node.id) !== parentId) {
multiParentIds.add(node.id);
}
} else {
parentMap.set(node.id, parentId); //
}
//
if (Array.isArray(node.children)) {
traverse(node.children);
}
});
// idid
if (node.children && Array.isArray(node.children)) {
node.children.forEach(child => collectNodes(child, node.id));
}
}
// ""
if (treeData && treeData.length > 0) {
traverse(treeData);
//
if (Array.isArray(newTreeData)) {
newTreeData.forEach(root => collectNodes(root, null)); // idnull
} else {
collectNodes(newTreeData, null);
}
return treeData;
}
//
if (multiParentIds.size > 0) {
console.warn(`以下节点存在多个父节点,可能导致重复:${Array.from(multiParentIds).join(', ')}`);
}
function processTreeData(node: any) {
// 2. ID+parentMap
const keepIds = new Set();
idArray.forEach(targetId => {
let currentId = targetId;
// parentIdnull
while (currentId !== null && nodeMap.has(currentId)) {
keepIds.add(currentId);
currentId = parentMap.get(currentId); //
}
});
// id4
function hasLongIdNode(node: { id: string | any[]; children: string | any[]; }) {
// id
if (node.id && node.id.length > 4) {
return true;
// 3. keepIdsparentId
function filterTree(node) {
if (!node || typeof node !== 'object' || !keepIds.has(node.id)) {
return null;
}
// childrennull
if (node.children && Array.isArray(node.children) && node.children.length > 0) {
for (const child of node.children) {
if (hasLongIdNode(child)) {
return true;
//
const filteredNode = { ...node };
// keepIdsparentIdid
if (filteredNode.children && Array.isArray(filteredNode.children)) {
//
const validChildren = new Map(); // id
filteredNode.children.forEach(child => {
// keepIdsidid
if (child && typeof child === 'object' && child.id &&
keepIds.has(child.id) && parentMap.get(child.id) === filteredNode.id) {
const filteredChild = filterTree(child);
if (filteredChild) {
validChildren.set(filteredChild.id, filteredChild); // id
}
}
});
// null
filteredNode.children = Array.from(validChildren.values());
if (filteredNode.children.length === 0) {
filteredNode.children = null;
}
} else {
filteredNode.children = null;
}
return false;
return filteredNode;
}
//
function processNode(node: any) {
// id
if (!hasLongIdNode(node)) {
return null;
}
//
let filteredTree;
if (Array.isArray(newTreeData)) {
// keepIdsnull
filteredTree = newTreeData
.map(root => {
if (root && keepIds.has(root.id) && parentMap.get(root.id) === null) {
return filterTree(root);
}
return null;
})
.filter(Boolean);
} else {
filteredTree = (keepIds.has(newTreeData.id) && parentMap.get(newTreeData.id) === null)
? filterTree(newTreeData)
: null;
}
//
const newNode = { ...node };
// 4. labelnumber
function modifyLabels(node) {
if (!node || typeof node !== 'object') return;
//
if (newNode.children && Array.isArray(newNode.children) && newNode.children.length > 0) {
//
newNode.children = newNode.children
.map((child: any) => processNode(child))
.filter((child: null) => child !== null);
if (idArray.includes(node.id)) {
const newLabel = `${node.label}(${node.value || ''})`;
node.label = newLabel;
node.number = newLabel;
}
// childrennull
if (newNode.children.length === 0) {
newNode.children = null;
}
if (node.children && Array.isArray(node.children)) {
node.children.forEach(child => modifyLabels(child));
}
}
return newNode;
//
if (Array.isArray(filteredTree)) {
filteredTree.forEach(root => modifyLabels(root));
} else {
modifyLabels(filteredTree);
}
return processNode(node);
return filteredTree;
}
</script>
<template>

36
src/components/DesignForm/public/form/form.vue

@ -1738,6 +1738,7 @@ function getTree1() {
const asfs: any[] = [];
const rangedUsers: any[] = [];
const tables: any[] = [];
function getAsfs() {
setTimeout(() => {
@ -1748,7 +1749,9 @@ function getAsfs() {
for (let i = 0; i < dataList.value.length; i++) {
if (dataList.value[i].type == "associatedForms") {
asfs.push(dataList.value[i]);
} else if (
} else if(dataList.value[i].type == "expand-user"){
rangedUsers.push(dataList.value[i]);
}else if (
dataList.value[i].type == "card" ||
dataList.value[i].type == "flex" ||
dataList.value[i].type == "div" ||
@ -1761,7 +1764,9 @@ function getAsfs() {
dataList.value[i].list.forEach((element: any) => {
if (element.type == "associatedForms") {
asfs.push(element);
}
}else if(element.type == "expand-user"){
rangedUsers.push(element);
}
});
} else if (dataList.value[i].type == "grid") {
let columns = JSON.parse(JSON.stringify(dataList.value[i].columns));
@ -1773,6 +1778,8 @@ function getAsfs() {
if (a.type == "associatedForms") {
asfs.push(a);
}else if(a.type == "expand-user"){
rangedUsers.push(a);
}
}
}
@ -1787,7 +1794,9 @@ function getAsfs() {
if (a.type == "associatedForms") {
asfs.push(a);
} else if (a.type == "flex" || a.type == "table") {
} else if(a.type == "expand-user"){
rangedUsers.push(a);
}else if (a.type == "flex" || a.type == "table") {
if (a.type == "table") {
tables.push(dataList.value[i]);
}
@ -1798,6 +1807,8 @@ function getAsfs() {
if (q.type == "associatedForms") {
asfs.push(q);
}else if(q.type == "expand-user"){
rangedUsers.push(q);
}
}
}
@ -1816,6 +1827,22 @@ const resetFields = () => {
ruleForm.value.resetFields();
// setValue(Object.assign(model.value, obj || {})) //
};
function checkAndGetTree(){
if(rangedUsers.length>0){
getTree1();
}else{
setTimeout(()=>{
checkAndGetTree()
},100)
}
}
onMounted(() => {
// console.log("--->",props.formData)
getInitModel();
@ -1824,7 +1851,8 @@ onMounted(() => {
});
showOrHide("kong");
getAsfs();
getTree1();
checkAndGetTree();
});
onUnmounted(() => {
if (eventName) {

Loading…
Cancel
Save