From 867c818b7660373ab7ca410a923c47e4b6602e2e Mon Sep 17 00:00:00 2001
From: wuyouming666 <2265557248@qq.com>
Date: 星期三, 10 七月 2024 14:05:21 +0800
Subject: [PATCH] 增加原片仓储模块,

---
 UI-Project/src/views/User/rolelist.vue |  280 +++++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 228 insertions(+), 52 deletions(-)

diff --git a/UI-Project/src/views/User/rolelist.vue b/UI-Project/src/views/User/rolelist.vue
index 5dcbb0b..d5fd898 100644
--- a/UI-Project/src/views/User/rolelist.vue
+++ b/UI-Project/src/views/User/rolelist.vue
@@ -13,7 +13,7 @@
 // import { ref } from 'vue'
 import { ElMessage, ElMessageBox } from 'element-plus'
 //  import LanguageMixin from './lang/LanguageMixin'
-const selectedProjectNoa = ref(''); // 褰撳墠閫変腑鐨勮鑹� 
+const selectedProjectNoa = ref(null); // 褰撳墠閫変腑鐨勮鑹� 
 // const options = ref<any[]>([]); // 涓嬫媺閫夐」鍒楄〃  
   const name = ref('');
 const tableData = ref([])
@@ -53,26 +53,34 @@
     console.error('Error fetching options:', error);  
   }  
 }; 
-// 娣诲姞 
+// 娣诲姞
 const getTableRow = async () => {
   try {  
     let menuList = [];
-    selectedOptions.value.forEach(array => {
-      if (array.length >= 2) {
-        let id = array[0];
-        let parentId = array[1];
-        menuList.push({  
-          id: id,  
-          parentId: 0,  
-          children: [
-        {
-          id: parentId,
-          parentId: id,
-        }
-      ],
-        });  
+     let parentIdMap = {}; // 鐢ㄤ簬瀛樺偍宸茬粡娣诲姞鐨勭埗椤�  
+    selectedOptions.value.forEach(array => {  
+      if (array.length >= 2) {  
+        let id = array[0];  
+        let parentId = array[1];  
+        // 濡傛灉id杩樻湭浣滀负鐖堕」娣诲姞锛屽垯娣诲姞瀹�  
+        if (!parentIdMap[id]) {  
+          menuList.push({  
+            id: id,  
+            parentId: 0, // 閫氬父锛岄《绾ч」鐨刾arentId鍙兘鏄痭ull鎴栫壒瀹氱殑鏍笽D锛岃繖閲岃涓�0鍙兘鏄釜鐗规畩鐢ㄩ��  
+            children: []  
+          });  
+          parentIdMap[id] = menuList[menuList.length - 1]; // 鏇存柊鏄犲皠  
+        }  
+        // 鍚戝搴旂殑鐖堕」娣诲姞瀛愰」  
+        if (!parentIdMap[id].children.some(child => child.id === parentId)) {  
+          // 妫�鏌ユ槸鍚﹀凡瀛樺湪鐩稿悓鐨勫瓙椤癸紙鍩轰簬id锛夛紝閬垮厤閲嶅娣诲姞  
+          parentIdMap[id].children.push({  
+            id: parentId,  
+            parentId: id  
+          });  
+        }  
       }  
-    });
+    });  
     const dataToSend = {  
       name: name.value,
       menuList: menuList
@@ -91,61 +99,229 @@
     console.error(error);
   }
 };
-// 澶勭悊缂栬緫鎸夐挳鐐瑰嚮  
 function handleEdit(row) {  
-  name.value = row.name; 
-  selectedOptions.value = row.selectedOptions
-//  options.value = res.data.tree
- adda.value = true; // 鏄剧ず瀵硅瘽妗�  
-  window.localStorage.setItem('id', row.id)
+  name.value = row.name;  
+  adda.value = true;
+  window.localStorage.setItem('ids', row.id);
+  const parentIdMap = {};  
+  const menuItemsById = {}; // 蹇�熸煡鎵捐彍鍗曢」  
+  let topLevelItems = []; // 瀛樺偍椤剁骇鑿滃崟椤� 
+  row.menuList.forEach(item => {  
+    const parentId = item.parentId === 0 ? null : item.parentId;
+    const menuItem = {  
+      id: item.id,  
+      parentId: parentId,  
+      menuName: item.menuName,  
+      children: []  
+    };  
+    menuItemsById[item.id] = menuItem;  
+    // 鍒濆鍖栨垨鏇存柊parentIdMap涓殑鏁扮粍  
+    if (!parentIdMap[parentId]) {  
+      parentIdMap[parentId] = [];  
+    }  
+    if (parentId === null) {  
+      // 椤剁骇鑿滃崟椤圭洿鎺ユ坊鍔犲埌topLevelItems  
+      topLevelItems.push(menuItem);  
+    } else {  
+      // 闈為《绾ц彍鍗曢」娣诲姞鍒皃arentIdMap涓�  
+      parentIdMap[parentId].push(menuItem);  
+    }  
+  });  
+  for (const parentId in parentIdMap) {  
+    if (parentId !== 'null') { // 璺宠繃椤剁骇鑿滃崟椤�  
+      parentIdMap[parentId].forEach(child => {  
+        // 鏌ユ壘鐖堕」骞舵坊鍔犲瓙椤�  
+        if (menuItemsById[parentId]) {  
+          menuItemsById[parentId].children.push(child);  
+        }  
+      });  
+    }  
+  }  
+  if (selectedOptions.value !== topLevelItems) {  
+    selectedOptions.value = topLevelItems;  
+  }  
+
+  const topLevelItemsWithChildren = topLevelItems; // 鍖呭惈椤剁骇鑿滃崟椤瑰強鍏跺瓙鑿滃崟椤圭殑鏁扮粍  
+  let selectedPath = [];
+
+// 寰幆閬嶅巻 topLevelItemsWithChildren 鏁扮粍
+for (let i = 0; i < topLevelItemsWithChildren.length; i++) {
+    // 娣诲姞椤剁骇鑿滃崟椤圭殑 id
+    selectedPath.push(topLevelItemsWithChildren[i].id);
+
+    // 閬嶅巻璇ラ《绾ц彍鍗曢」鐨勫瓙鑿滃崟椤�
+    if (topLevelItemsWithChildren[i].children && topLevelItemsWithChildren[i].children.length > 0) {
+        for (let j = 0; j < topLevelItemsWithChildren[i].children.length; j++) {
+            // 娣诲姞姣忎釜瀛愯彍鍗曢」鐨� id
+            selectedPath.push(topLevelItemsWithChildren[i].children[j].id);
+        }
+    }
 }
+
+  console.log(selectedPath);
+selectedOptions.value = selectedPath;
+}
+// function handleEdit(row) {  
+//   name.value = row.name;  
+//   adda.value = true;  
+//   window.localStorage.setItem('ids', row.id);  
+//   // 鏋勫缓鑿滃崟鏍�  
+//   const buildMenuTree = (menuList) => {  
+//     const parentIdMap = {};  
+//     const menuItemsById = {};  
+//     menuList.forEach(item => {  
+//       const parentId = item.parentId === 0 ? null : item.parentId;  
+//       const menuItem = {  
+//         id: item.id,  
+//         parentId: parentId,  
+//         menuName: item.menuName,  
+//         children: []  
+//       };  
+//       menuItemsById[item.id] = menuItem;  
+  
+//       if (!parentIdMap[parentId]) {  
+//         parentIdMap[parentId] = [];  
+//       }  
+//       parentIdMap[parentId].push(menuItem);  
+//     });  
+//     // 鏋勫缓鐖跺瓙鍏崇郴  
+//     for (const parentId in parentIdMap) {  
+//       if (parentId !== 'null') {  
+//         parentIdMap[parentId].forEach(child => {  
+//           if (menuItemsById[parentId]) {  
+//             menuItemsById[parentId].children.push(child);  
+//           }  
+//         });  
+//       }  
+//     }   
+//     return parentIdMap[null] || [];  
+//   };  
+//   const options = buildMenuTree(row.menuList);  
+//   const selectedIds = row.menuList.filter(item => item.parentId === 0).map(item => item.id);  
+//   selectedOptions.value = selectedIds; 
+//   console.log(options); // 杈撳嚭鏁翠釜鑿滃崟鏍�  
+//   console.log(selectedIds); // 杈撳嚭搴旈粯璁ら�変腑鐨勮妭鐐笽D鏁扮粍  
+// }  
+// function handleEdit(row) {  
+//   name.value = row.name;  
+//   adda.value = true;
+//   window.localStorage.setItem('ids', row.id);
+//   // 鍒濆鍖� parentIdMap 鍜� menuItemsById  
+//   const parentIdMap = {};  
+//   const menuItemsById = {};  
+//   let topLevelItems = [];  
+  
+//   // 鏋勫缓鏍戝舰缁撴瀯  
+//   row.menuList.forEach(item => {  
+//     const parentId = item.parentId === 0 ? null : item.parentId;  
+//     const menuItem = {  
+//       id: item.id,  
+//       parentId: parentId,  
+//       label: item.menuName, // 娉ㄦ剰锛歟l-cascader 浣跨敤 label 鑰屼笉鏄� menuName  
+//       value: item.id, // 閫氬父 value 鏄敮涓�鏍囪瘑绗�  
+//       children: []  
+//     };  
+//     menuItemsById[item.id] = menuItem;  
+  
+//     if (!parentIdMap[parentId]) {  
+//       parentIdMap[parentId] = [];  
+//     }  
+  
+//     if (parentId === null) {  
+//       topLevelItems.push(menuItem);  
+//     } else {  
+//       parentIdMap[parentId].push(menuItem);  
+//     }  
+//   });  
+  
+//   // 鏋勫缓鐖跺瓙鍏崇郴  
+//   for (const parentId in parentIdMap) {  
+//     if (parentId !== 'null') {  
+//       parentIdMap[parentId].forEach(child => {  
+//         if (menuItemsById[parentId]) {  
+//           menuItemsById[parentId].children.push(child);  
+//         }  
+//       });  
+//     }  
+//   }  
+  
+//   // 璁剧疆 options  
+//   options.value = topLevelItems;  
+  
+//   // 鏋勫缓 selectedOptions  
+//   let selected = [];  
+//   // 鍋囪浣犳兂閫変腑鎵�鏈夎妭鐐癸紝浣犲彲浠ラ亶鍘� row.menuList 骞舵瀯寤鸿矾寰�  
+//   row.menuList.forEach(item => {  
+//     let path = [];  
+//     let currentParentId = item.parentId;  
+//     while (currentParentId !== null) {  
+//       const parentItem = menuItemsById[currentParentId];  
+//       if (parentItem) {  
+//         path.unshift(parentItem.value); // 浠庣埗鑺傜偣鍒板瓙鑺傜偣  
+//         currentParentId = parentItem.parentId;  
+//       } else {  
+//         break; // 濡傛灉鎵句笉鍒扮埗鑺傜偣锛屽垯鍋滄  
+//       }  
+//     }  
+//     // 娣诲姞褰撳墠鑺傜偣鐨� ID 鍒拌矾寰勬湯灏�  
+//     path.push(item.id);  
+//     // 濡傛灉璺緞涓嶄负绌猴紝鍒欐坊鍔犲埌 selectedOptions  
+//     if (path.length > 0) {  
+//       selected.push(path);  
+//     }  
+//   });  
+  
+//   // 鏇存柊 selectedOptions  
+//   selectedOptions.value = selected;  
+//   console.log(selected);
+//   console.log(topLevelItems);
+//   // 娉ㄦ剰锛氱‘淇� selectedOptions 鍜� options 鏄搷搴斿紡鐨�  
+// }
 // 缂栬緫
 const getTableRowa = async () => {
-  let id = window.localStorage.getItem('id')
+  let ids = window.localStorage.getItem('ids')
   try {  
     let menuList = [];
-    selectedOptions.value.forEach(array => {
-      if (array.length >= 2) {
-        let id = array[0];
-        let parentId = array[1];
-        menuList.push({  
-          id: id,  
-          parentId: 0,  
-          children: [
-        {
-          id: parentId,
-          parentId: id,
-        }
-      ],
-        });  
+     let parentIdMap = {}; // 鐢ㄤ簬瀛樺偍宸茬粡娣诲姞鐨勭埗椤�  
+    selectedOptions.value.forEach(array => {  
+      if (array.length >= 2) {  
+        let id = array[0];  
+        let parentId = array[1];  
+        // 濡傛灉id杩樻湭浣滀负鐖堕」娣诲姞锛屽垯娣诲姞瀹�  
+        if (!parentIdMap[id]) {  
+          menuList.push({  
+            id: id,  
+            parentId: 0, // 閫氬父锛岄《绾ч」鐨刾arentId鍙兘鏄痭ull鎴栫壒瀹氱殑鏍笽D锛岃繖閲岃涓�0鍙兘鏄釜鐗规畩鐢ㄩ��  
+            children: []  
+          });  
+          parentIdMap[id] = menuList[menuList.length - 1]; // 鏇存柊鏄犲皠  
+        }  
+        // 鍚戝搴旂殑鐖堕」娣诲姞瀛愰」  
+        if (!parentIdMap[id].children.some(child => child.id === parentId)) {  
+          // 妫�鏌ユ槸鍚﹀凡瀛樺湪鐩稿悓鐨勫瓙椤癸紙鍩轰簬id锛夛紝閬垮厤閲嶅娣诲姞  
+          parentIdMap[id].children.push({  
+            id: parentId,  
+            parentId: id  
+          });  
+        }  
       }  
-    });
+    });  
     const dataToSend = {  
-      id:id,
+      id:ids,
       name: name.value,
       menuList: menuList
     };  
     const response = await request.post('/loadGlass/sys/role/updateRole', dataToSend);
-  // try {
-  //   const response = await request.post('/loadGlass/sys/role/updateRole', {
-  //     id: id,
-  //     name: name.value,
-  //   }); 
- 
     if (response.code == 200) {
-      // 缁戝畾鎴愬姛锛屽鐞嗛�昏緫
       ElMessage.success(response.message);
       adda.value = false;
       name.value = '';
-       selectedOptions.value = '';
-      fetchOptionsa()
-      tableData.value = response.data;
+      selectedOptions.value = '';
+      fetchOptionsa();
     } else {
-      // 璇锋眰澶辫触锛屾樉绀洪敊璇秷鎭�
       ElMessage.error(response.message);
     }
   } catch (error) {
-    // 澶勭悊閿欒
     console.error(error);
   }
 };

--
Gitblit v1.8.0