| | |
| | | // 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([]) |
| | |
| | | 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, // 通常,顶级项的parentId可能是null或特定的根ID,这里设为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 |
| | |
| | | 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 { |
| | | // 非顶级菜单项添加到parentIdMap中 |
| | | 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); // 输出应默认选中的节点ID数组 |
| | | // } |
| | | // 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, // 注意:el-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, // 通常,顶级项的parentId可能是null或特定的根ID,这里设为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); |
| | | } |
| | | }; |