From e91a950c1a085087559cd2a86a76384efb78b995 Mon Sep 17 00:00:00 2001 From: fengyuan_yang <1976974459@qq.com> Date: Tue, 31 Mar 2026 09:31:18 +0800 Subject: [PATCH] =?UTF-8?q?2026-03-31=20=E8=A7=92=E8=89=B2=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E9=A1=B5=E9=9D=A2=E3=80=90=E6=9D=83=E9=99=90=E6=8E=88?= =?UTF-8?q?=E6=9D=83=E3=80=91=E5=AF=B9=E8=AF=9D=E6=A1=86=E6=A0=B7=E5=BC=8F?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/views/modules/sys/role-add-or-update.vue | 168 ++++++++++++++++--- 1 file changed, 149 insertions(+), 19 deletions(-) diff --git a/src/views/modules/sys/role-add-or-update.vue b/src/views/modules/sys/role-add-or-update.vue index a92e22d..c1d57fb 100644 --- a/src/views/modules/sys/role-add-or-update.vue +++ b/src/views/modules/sys/role-add-or-update.vue @@ -1,30 +1,56 @@ @@ -38,6 +64,7 @@ export default { data() { return { visible: false, + filterText: '', menuList: [], menuListTreeProps: { label: 'name', @@ -73,19 +100,93 @@ export default { tempKey: -666666 // 临时key, 用于解决tree半选中状态项不能传给后台接口问题. # 待优化 } }, - + watch: { + filterText(val) { + this.$refs.menuListTree.filter(val); + } + }, methods: { - + filterNode(value, data) { + if (!value) return true; + let matchButton = data.buttonList && data.buttonList.some(btn => btn.name.indexOf(value) !== -1); + return data.name.indexOf(value) !== -1 || matchButton; + }, + processMenuTree(treeList) { + for (let i = 0; i < treeList.length; i++) { + let node = treeList[i] + this.$set(node, 'buttonList', []) + this.$set(node, 'checkedButtons', []) + if (node.children && node.children.length > 0) { + let normalChildren = [] + for (let child of node.children) { + if (child.specificType === 'button' || child.type === 2) { + node.buttonList.push(child) + } else { + normalChildren.push(child) + } + } + node.children = normalChildren + this.processMenuTree(node.children) + } + } + }, + populateCheckedButtons(treeList, checkedIds, treeCheckedKeys) { + for (let node of treeList) { + if (checkedIds.includes(Number(node.menuId))) { + if (!node.children || node.children.length === 0) { + treeCheckedKeys.push(Number(node.menuId)) + } + } + if (node.buttonList && node.buttonList.length > 0) { + node.checkedButtons = node.buttonList + .filter(btn => checkedIds.includes(Number(btn.menuId))) + .map(btn => Number(btn.menuId)) + } + if (node.children) { + this.populateCheckedButtons(node.children, checkedIds, treeCheckedKeys) + } + } + }, + handleTreeCheck(data, checkedInfo) { + let isChecked = checkedInfo.checkedKeys.includes(data.menuId) + this.toggleButtons(data, isChecked) + }, + toggleButtons(node, isChecked) { + if (isChecked) { + node.checkedButtons = node.buttonList ? node.buttonList.map(b => Number(b.menuId)) : [] + } else { + node.checkedButtons = [] + } + if (node.children) { + node.children.forEach(child => this.toggleButtons(child, isChecked)) + } + }, + handleButtonChange(data) { + if (data.checkedButtons.length > 0) { + this.$refs.menuListTree.setChecked(data.menuId, true, false) + } + }, + getAllCheckedButtons(treeList, result) { + for (let node of treeList) { + if (node.checkedButtons && node.checkedButtons.length > 0) { + result.push(...node.checkedButtons) + } + if (node.children) { + this.getAllCheckedButtons(node.children, result) + } + } + }, init (row) { let id = row ? row.roleId : 0 - // this.getFunctionButtonList() this.dataForm.id = id || 0 this.$http({ url: this.$http.adornUrl('/sys/menu/list'), method: 'get', params: this.$http.adornParams() }).then(({data}) => { - this.menuList = treeDataTranslate(data, 'menuId') + let treeData = treeDataTranslate(data, 'menuId') + this.processMenuTree(treeData) + this.menuList = treeData }).then(() => { this.visible = true this.$nextTick(() => { @@ -107,10 +208,10 @@ export default { data.role.menuIdList.splice(idx, data.role.menuIdList.length - idx) } let x1 = data.role.menuIdList.map(Number) + let treeCheckedKeys = [] + this.populateCheckedButtons(this.menuList, x1, treeCheckedKeys) this.$nextTick(() => { - for (let x1Element of x1) { - this.$refs.menuListTree.setChecked(x1Element, true, false) - } + this.$refs.menuListTree.setCheckedKeys(treeCheckedKeys) }) } }) @@ -122,6 +223,14 @@ export default { dataFormSubmit () { this.$refs['dataForm'].validate((valid) => { if (valid) { + let buttonIds = [] + this.getAllCheckedButtons(this.menuList, buttonIds) + let treeKeys = this.$refs.menuListTree.getCheckedKeys() + let halfKeys = this.$refs.menuListTree.getHalfCheckedKeys() + let finalMenuIds = [...new Set([...treeKeys, ...halfKeys, ...buttonIds])] + if (this.tempKey) { + finalMenuIds.push(this.tempKey) + } this.$http({ url: this.$http.adornUrl(`/sys/role/${!this.dataForm.id ? 'save' : 'update'}`), method: 'post', @@ -129,7 +238,7 @@ export default { 'roleId': this.dataForm.id || undefined, 'roleName': this.dataForm.roleName, 'remark': this.dataForm.remark, - 'menuIdList': [].concat(this.$refs.menuListTree.getCheckedKeys(), [this.tempKey], this.$refs.menuListTree.getHalfCheckedKeys()) + 'menuIdList': finalMenuIds }) }).then(({data}) => { if (data && data.code === 0) { @@ -165,8 +274,29 @@ export default {