ke_junjie
2025-06-04 84620534eb627e95811b971a4b552b6a177829bf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<template>
    <el-dialog
        custom-class="sy-modal"
        
        :title="title"
        :close-on-click-modal="false"
        width="600px"
        :before-close="onClose"
    >
        <div class="roles-management-authority-modal-conatinter" v-loading="loading">
            <el-scrollbar>
                <el-tree
                    ref="tree"
                    :data="roleTree"
                    default-expand-all
                    show-checkbox
                    node-key="value"
                />
            </el-scrollbar>
        </div>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="onClose">取&nbsp;消</el-button>
                <el-button type="primary" @click="onSubmit">确&nbsp;定</el-button>
            </span>
        </template>
    </el-dialog>
</template>
 
<script>
export default {
    name:'rolesManagementAuthorityModalCompontent',
    props:{
        visible:{
            type:Boolean,
            default:false
        },
        role:{
            type:Object,
            default:function(){
                return {}
            }
        }
    },
    data(){
        return {
            title:'',
            loading:false,
            roleTree:[],
            treeLoaded:false
        }
    },
    watch:{
        visible(newVal,oldVal){
            if (newVal!==oldVal) {
                if (newVal) {
                    this.initForm();
                } else {
                    
                }
            }
        }
    },
    methods:{
        initForm(){
            this.title = '角色['+this.role.Name+']的权限'
            if (!this.role.Id) return false;
            /* 一下加载数据 */
            this.loading = true;
            this.getRolesTree((f1)=>{
                if (f1) {
                    this.$nextTick(()=>{
                        this.getRolePermissions(()=>{
                            this.loading = false;
                        })
                    })
                } else {
                    this.loading = false;
                }
            })
        },
        getRolesTree(callback){
            if (this.treeLoaded) {
                callback && callback(true)
            } else {
                this.$api.get('GetPermissionTree',{needbtn:false},{block:'permission'}).then((d)=>{
                    this.roleTree = d.children || []
                    this.treeLoaded = true;
                    callback && callback(true)
                }).catch(err=>{
                    callback && callback(false)
                })
            }
        },
        getRolePermissions(callback){
            this.$api.get('GetPermissionIdByRoleId',{rid:this.role.Id},{block:'permission'}).then((d)=>{
                let _arr = this.filterRealCheckedNodes(d.permissionids)
                this.$refs.tree.setCheckedKeys(_arr)
                /* permissionids */
                callback && callback(true)
            }).catch(err=>{
                callback && callback(false)
            })
        },
        /* 过滤节点,将非全选子节点的父节点过滤出去,剩下的是节点数显示勾选真正需要的 */
        /* 之所以要再次重新计算树的勾选节点,一是为了清理垃圾数据,二是处理原先全选子节点的父节点在新增子节点后显示不正确的问题 */
        filterRealCheckedNodes(nodeKeys){
            let res = [];
            for (let i=0;i<nodeKeys.length;i++) {
                let obj = this.getTreeFullNodeByNodekey(nodeKeys[i],this.roleTree);
                if (this.judgeRealCheckedNode(obj,nodeKeys)) {
                    res.push(nodeKeys[i])
                }
            }
            return res;
        },
        /* 根据节点key获取该key在节点树中的完整节点 */
        getTreeFullNodeByNodekey(nodeKey,tree){
            let res = null;
            for (let i=0;i<tree.length;i++) {
                if (tree[i].value === nodeKey) {
                    res = tree[i];
                    break;
                } 
                if (tree[i].children && (tree[i].children instanceof Array) && tree[i].children.length>0) {
                    res = this.getTreeFullNodeByNodekey(nodeKey,tree[i].children);
                    if (res) {
                        break;
                    }
                }
            }
            return res;
        },
        /* 判断是否是半节点树显示真正需要的节点,排除非全选子节点的父节点 */
        judgeRealCheckedNode(node,nodeKeys){
            let res = true;
            if (node) {
                if (node.children && (node.children instanceof Array) && node.children.length>0) {
                    for (let i=0;i<node.children.length;i++) {
                        let val = node.children[i].value;
                        if (nodeKeys.indexOf(val)<0) {
                            res = false;
                            break;
                        }
                    }
                } 
            } else {
                res = false;
            }
            return res;
        },
        close(){
            this.$emit('update:visible',false)
        },
        onClose(){
            this.close();
        },
        onSubmit(){
            let checkeds = this.$refs.tree.getCheckedKeys()
            let halfs = this.$refs.tree.getHalfCheckedKeys();
            let params = {
                pids:[...checkeds,...halfs],
                rid:this.role.Id
            }
            this.loading = true;
            this.$api.post('Assign',params,{block:'permission'}).then((d)=>{
                this.loading = false;
                this.$message.success('角色权限保存成功!')
                this.close()
            }).catch(err=>{
                this.loading = false;
            })
        }
    }
}
</script>
 
<style scoped lang="scss">
.roles-management-authority-modal-conatinter{
    height: 500px;
    box-sizing: border-box;
    padding: 12px;
}
</style>