Browse Source

选择用户,性能优化

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

520
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<{ const emits = defineEmits<{
(e: 'update:modelValue', value: string): void (e: 'update:modelValue', value: string): void
}>() }>()
const value = ref([]) const value = ref([])
const loading = ref(true)
watch(value,(newValue)=>{ watch(value,(newValue)=>{
if(newValue.length > 0){ if(newValue.length > 0){
let str = "" let str = ""
@ -56,6 +43,8 @@ watch(value,(newValue)=>{
} }
},{ deep: true }) },{ deep: true })
function parseStringToArray(str:string) { function parseStringToArray(str:string) {
try { try {
// JSON // JSON
@ -73,17 +62,14 @@ function parseStringToArray(str:string) {
} }
} }
onBeforeMount(() => { onBeforeMount(() => {
// checkorgAndManTree()
//checkReady()
//getTree()
setTimeout(()=>{ setTimeout(()=>{
value.value = parseStringToArray(props.modelValue) value.value = parseStringToArray(props.modelValue)
},500 ) },500 )
}) })
const url = "/javasys/lowCode/manCont/getManContsByKeys"
//console.log(attrs.value)
const keys = computed(()=>{ const keys = computed(()=>{
if(attrs.queryBy == 'role'){ if(attrs.queryBy == 'role'){
return attrs.roleRange 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) { const treeData = {
if (!node) { "id": "102",
//console.log(`: ${node}`); "label": "企管部",
return; "parentId": "309",
} "children": [
// 便 {
//console.log(` [${level}]: id=${node.id}, label=${node.label}`); "id": "284148167597887488",
// id "label": "荣潇亭",
if (node.id !== undefined && node.id !== null) { "parentId": "102",
nodeMap[node.id] = node; "children": null,
} else { "value": "303086",
//console.warn(`id:`, node); "treeAttrs": null,
} "disabled": null,
// "type": null
if (Array.isArray(node.children)) { },
node.children.forEach((child, index) => { {
collectNodes(child, level + 1); // +1便 "id": "272",
}); "label": "企管",
} else if (node.children !== null && node.children !== undefined) { "parentId": "102",
//console.warn(`children:`, node.children); "children": [
{
"id": "284148287831805952",
"label": "吴可楠",
"parentId": "272",
"children": null,
"value": "303122",
"treeAttrs": null,
"disabled": null,
"type": null
}
],
"value": null,
"treeAttrs": null,
"disabled": null,
"type": null
} }
],
"value": null,
"treeAttrs": null,
"disabled": null,
"type": null
};
const idList = ["284148167597887488", "284148287831805952"];
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))
resData.value = arr
}else{
setTimeout(()=>{
checkorgAndManTree()
},100)
} }
})
//
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(){
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
}
/**
@ 作者: 秦东
@ 时间: 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)
data1.children = data2
/* const resData = computed(()=>{
loading.value = false if(props.orgAndManTree&&props.orgAndManTree.children&&props.orgAndManTree.children.length>0){
return enrichTreeWithEmployeeInfo(options.value, data2) console.log(11111)
let arr = []
arr.push(modifyTreeData(props.orgAndManTree,keys.value))
return arr
}else{ }else{
return [] 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();
// const resultExample = [{
function processNode(node: { id: any; value: unknown; children: string | any[] | null; }) { "id": "102",
// 使id "label": "企管部",
const nodeId = node.id; "parentId": "309",
if (visitedNodes.has(nodeId)) { "children": [
return false; // 访false {
"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,
visitedNodes.add(nodeId); "treeAttrs": null,
"disabled": null,
// "type": null
const isDirectMatch = node.value && numberSet.has(node.value); }]
// childrennull
if (!node.children || node.children === null) {
return isDirectMatch;
function modifyTreeData(treeData, idList) {
console.log("modifyTreeData 执行了")
//
const newTreeData = JSON.parse(JSON.stringify(treeData));
// 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; //
//
if (!nodeMap.has(node.id)) {
nodeMap.set(node.id, node);
} }
// //
const childrenToKeep = []; if (parentMap.has(node.id)) {
//
// if (parentMap.get(node.id) !== parentId) {
for (let i = 0; i < node.children.length; i++) { multiParentIds.add(node.id);
const child = node.children[i];
//
if (processNode(child)) {
// childrenToKeep
childrenToKeep.push(child);
} }
} else {
parentMap.set(node.id, parentId); //
} }
// children // idid
node.children = childrenToKeep; if (node.children && Array.isArray(node.children)) {
node.children.forEach(child => collectNodes(child, node.id));
//
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);
} }
} }
return result; //
} if (Array.isArray(newTreeData)) {
newTreeData.forEach(root => collectNodes(root, null)); // idnull
} else {
collectNodes(newTreeData, null);
/**
* 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
});
//
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 (Array.isArray(node.children)) {
traverse(node.children);
}
});
} }
// "" //
if (treeData && treeData.length > 0) { if (multiParentIds.size > 0) {
traverse(treeData); console.warn(`以下节点存在多个父节点,可能导致重复:${Array.from(multiParentIds).join(', ')}`);
} }
return treeData; // 2. ID+parentMap
} const keepIds = new Set();
idArray.forEach(targetId => {
function processTreeData(node: any) { let currentId = targetId;
// parentIdnull
// id4 while (currentId !== null && nodeMap.has(currentId)) {
function hasLongIdNode(node: { id: string | any[]; children: string | any[]; }) { keepIds.add(currentId);
// id currentId = parentMap.get(currentId); //
if (node.id && node.id.length > 4) {
return true;
} }
});
// childrennull
if (node.children && Array.isArray(node.children) && node.children.length > 0) { // 3. keepIdsparentId
for (const child of node.children) { function filterTree(node) {
if (hasLongIdNode(child)) { if (!node || typeof node !== 'object' || !keepIds.has(node.id)) {
return true; return null;
}
//
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) { let filteredTree;
// id if (Array.isArray(newTreeData)) {
if (!hasLongIdNode(node)) { // keepIdsnull
return null; 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;
}
// 4. labelnumber
function modifyLabels(node) {
if (!node || typeof node !== 'object') return;
if (idArray.includes(node.id)) {
const newLabel = `${node.label}(${node.value || ''})`;
node.label = newLabel;
node.number = newLabel;
} }
// if (node.children && Array.isArray(node.children)) {
const newNode = { ...node }; node.children.forEach(child => modifyLabels(child));
//
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);
// childrennull
if (newNode.children.length === 0) {
newNode.children = null;
}
} }
return newNode;
} }
return processNode(node); //
if (Array.isArray(filteredTree)) {
filteredTree.forEach(root => modifyLabels(root));
} else {
modifyLabels(filteredTree);
}
return filteredTree;
} }
</script> </script>
<template> <template>

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

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

Loading…
Cancel
Save