Your Name
2022-04-08 1f2a259f8cbb7eb0b12d12895116c309eb8cd609
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
<template>
    <div class="app-container">
        <div class="filter-container">
            <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-refresh" @click="refreshHandle">刷新</el-button>
            <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-plus" @click="showCreateHandle">新增</el-button>
            <el-button class="filter-item" type="primary" icon="el-icon-search" @click="queryHandle" />
        </div>
        <div class="table_content">
            <el-table
                v-loading="listLoading"
                :key="tableKey"
                :data="roleData"
                border
                fit
                highlight-current-row
                style="width: 100%;"
            >
                <el-table-column label="角色名" prop="name" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.name }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="按钮权限" prop="btnaccess" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.btnaccess }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="创建时间" prop="createddate" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.createddate | parseTime('{y}-{m}-{d}') }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" width="280" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="showEditHandle(scope.row)">编辑</el-button>
                        <el-button type="primary" size="mini" style="width:100px;" @click="showPermission(scope.row)">配置菜单权限</el-button>
                        <el-button type="danger" size="mini" @click="deleteHandle(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <br>
        </div>
        <el-dialog :title="dialogStatus==='create'?'新增':'编辑'" :visible.sync="dialogFormVisible" :modal-append-to-body="false" :close-on-click-modal="false" width="700px">
            <el-form ref="dataForm" :rules="dataFormRules" :model="dataForm" label-position="right" label-width="100px" style="margin-left:50px;width:500px;" element-loading-text="保存中...">
                <el-form-item label="角色名" prop="name">
                    <el-input v-model.trim="dataForm.name"/>
                </el-form-item>
                <el-form-item label="按钮权限:" prop="btnaccess">
                    <el-select v-model.trim="dataForm.btnaccess" placeholder="请选择按钮权限" multiple style="width:100%;" clearable>
                        <el-option value="all" label="所有"/>
                        <el-option value="add" label="新增"/>
                        <el-option value="edit" label="编辑"/>
                        <el-option value="delete" label="删除"/>
                        <el-option value="review" label="审核"/>
                        <el-option value="import" label="导入"/>
                        <el-option value="export" label="导出"/>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取消</el-button>
                <el-button type="primary" @click="dialogStatus==='create'?createHandle():updateHandle()">确认</el-button>
            </div>
        </el-dialog>
        <!-- 菜单权限 -->
        <el-dialog :title="menuPermissionTitle" :visible.sync="permModalVisible" width="500" >
            <el-tree
                ref="menuTree"
                :data="menuTree"
                :check-strictly="checkStrictly"
                :props="defaultProps"
                :default-checked-keys="defaultCheckKeys"
                node-key="id"
                default-expand-all
                show-checkbox
                @node-click="handleNodeClick"
                @check-change="handleNodeCheckChange"
                @check="handleNodeCheck"
            />
            <!-- <Tree ref="tree" :data="permData" multiple></Tree> -->
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancelPermEdit">取消</el-button>
                <!-- <el-button @click="selectTreeAll">全选/反选</el-button> -->
                <el-button type="primary" @click="submitPermEdit">提交</el-button>
            </div>
        </el-dialog>
        <div style="clear: both;"/>
    </div>
</template>
 
<script>
    import { mapGetters } from 'vuex'
    import { roleList, createRole, deleteRole, updateRole, getRole, updateRolePermission} from '@/api/role'
    import { permissionList } from '@/api/permission'
    import { parseTime, computePageCount } from '@/utils'
    import { parseError } from '@/utils/messageDialog'
    import Cookies from 'js-cookie'
    import { Message, MessageBox } from 'element-ui'
    import { initRouter } from '@/utils/router'
    export default {
        name: 'RoleTable',
        filters: {
        },
        computed: {
            ...mapGetters([
                'userType'
            ])
        },
        data() {
            return {
                tableKey: 0,
                roleData: null,
                listLoading: true,
                dataForm: {
                    name: '',
                    id: '',
                    btnaccess: []
                },
                queryForm: {
                    name: ''
                },
                dialogFormVisible: false,
                dialogStatus: '',
                dataFormRules: {
                    name: [{ required: true, message: '角色名不能为空', trigger: 'blur' }]
                },
                menuPermissionTitle: '分配菜单角色',
                permModalVisible: false,
                treeLoading: false,
                selectAllFlag: false,
                menuTree: [],
                hasMenuTree: [],
                editRolePermId: '',
                defaultProps: {
                    children: 'children',
                    label: 'title',
                    id: 'id'
                },
                defaultCheckKeys: [],
                checkStrictly: false,
            }
        },
        created() {
            this.getRoleList()
        },
        methods: {
            getRoleList() {
                const _this = this
                const params = {}
                const userName = Cookies.get('userName')
                params['operator'] = userName
                for (const i in _this.queryForm) {
                    if (_this.queryForm[i] != undefined && _this.queryForm[i].toString() != '') {
                        params[i] = _this.queryForm[i]
                    }
                }
                _this.listLoading = true
                roleList(params).then(response => {
                    const res = response.data
                    if (res.code == 200) {
                        _this.roleData = res.result
                    } else {
                        parseError({ error: res.message, vm: _this })
                    }
                    _this.listLoading = false
                }).catch(error => {
                    _this.listLoading = false
                    parseError({ error: error, vm: _this })
                })
            },
            createHandle: function() {
                const _this = this
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        const userName = Cookies.get('userName')
                        _this.dataForm['operator'] =userName
                        _this.dataForm['btnaccess'] = _this.dataForm['btnaccess'].join(',')
                        createRole(_this.dataForm).then(response => {
                            const res = response.data
                            if (res.code == 200) {
                                _this.dialogFormVisible = false
                                _this.$message({
                                    message: '创建成功',
                                    type: 'success'
                                })
                                _this.getRoleList()
                            } else {
                                parseError({ error: res.message, vm: _this })
                            }
                            _this.dataForm['btnaccess'] = []
                        }).catch(error => {
                            _this.dataForm['btnaccess'] = []
                            parseError({ error: error, vm: _this })
                        })
                    }
                })
            },
            deleteHandle: function(row) {
                const _this = this
                MessageBox.confirm('确定删除角色' + row.name + '信息', '确定删除', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    _this.deleteAction(row)
                }).catch(() => {
                    console.log('已取消删除')
                })
            },
            deleteAction: function(row) {
                const _this = this
                const params = {}
                params['id'] = row.id
                const userName = Cookies.get('userName')
                params['operator'] = userName
                deleteRole(params).then(response => {
                    const res = response.data
                    if (res.code == 200) {
                        _this.$message({
                            message: '删除成功',
                            type: 'success'
                        })
                        _this.getRoleList()
                    } else {
                        parseError({ error: res.message, vm: _this })
                    }
                }).catch(error => {
                    parseError({ error: error, vm: _this })
                })
            },
            refreshHandle: function() {
                this.getRoleList()
            },
            queryHandle: function() {
                this.getRoleList()
            },
            resetDataForm() {
                this.dataForm = {
                    name: '',
                    id: '',
                    btnaccess: [],
                }
            },
            showCreateHandle() {
                this.resetDataForm()
                this.dialogStatus = 'create'
                this.dialogFormVisible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].clearValidate()
                })
            },
            updateHandle() {
                this.$refs['dataForm'].validate((valid) => {
                    const _this = this
                    if (valid) {
                        const userName = Cookies.get('userName')
                        _this.dataForm['operator'] = userName
                        _this.dataForm['btnaccess'] = _this.dataForm['btnaccess'].join(',')
                        updateRole(_this.dataForm).then(response => {
                            const res = response.data
                            if (res.code == 200) {
                                _this.dialogFormVisible = false
                                _this.$message({
                                    message: '更新成功',
                                    type: 'success'
                                })
                                _this.getRoleList()
                            } else {
                                parseError({ error: res.message, vm: _this })
                            }
                            _this.dataForm['btnaccess'] = []
                        }).catch(error => {
                            _this.dataForm['btnaccess'] = []
                            parseError({ error: error, vm: _this })
                        })
                    }
                })
            },
            showEditHandle: function(row) {
                this.resetDataForm()
                this.dialogStatus = 'editor'
                this.dataForm.name = row.name
                this.dataForm.id = row.id
                this.dataForm.btnaccess = row.btnaccess ? row.btnaccess.split(',') : []
                this.dialogFormVisible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].clearValidate()
                })
            },
            handleSizeChange: function(val) {
                this.getRoleList()
            },
            handleCurrentChange: function(val) {
                this.getRoleList()
            },
            showPermission(row) {
                this.permModalVisible = true
                this.menuPermissionTitle = '分配' + row.name + '菜单权限'
                this.getPermissionList()
                this.sethasMenuTree(row.id)
                this.editRolePermId = row.id
            },
            getPermissionList() {
                const _this = this
                const params = {}
                params['userId'] = Cookies.get('userId')
                permissionList(params).then(response => {
                    const res = response.data
                    if (res.code == 200) {
                        _this.menuTree = res.result
                    } else {
                        parseError({ error: res.message, vm: _this })
                    }
                })
            },
            async sethasMenuTree(roleId) {
                const _this = this
                _this.defaultCheckKeys = []
                _this.checkStrictly=true
                const params = {}
                params['roleId'] = roleId;
                let response = await getRole(params)
                const res = response.data
                    if (res.code === '200') {
                        const hasMenuTree = res.result
                        if (hasMenuTree && hasMenuTree.length > 0) {
                            const checkKeys = []
                            for (const menu of hasMenuTree) {
                                checkKeys.push(menu.id)
                            }
                            _this.defaultCheckKeys = checkKeys
                            setTimeout( ()=>{
                                this.$refs.menuTree.setCheckedKeys(checkKeys)
                            })
                        } else {
                            _this.defaultCheckKeys = []
                        }
                        _this.checkStrictly=true
                    } else {
                        parseError({ error: res.message, vm: _this })
                        _this.checkStrictly=false
                    }
                // getRole(params).then(response => {
                //     const res = response.data
                //     if (res.code == 200) {
                //         debugger
                //         const hasMenuTree = res.result
                //         if (hasMenuTree && hasMenuTree.length > 0) {
                //             const checkKeys = []
                //             for (const menu of hasMenuTree) {
                //                 checkKeys.push(menu.id)
                //             }
                //             _this.defaultCheckKeys = checkKeys
                //         } else {
                //             _this.defaultCheckKeys = []
                //         }
                //         _this.checkStrictly=false
                //     } else {
                //         parseError({ error: res.message, vm: _this })
                //         _this.checkStrictly=false
                //     }
                // })
                console.log(_this.defaultCheckKeys);
            },
            cancelPermEdit() {
                this.permModalVisible = false
            },
            selectTreeAll() {
                this.selectAllFlag = !this.selectAllFlag
                if (this.selectAllFlag) {
                    this.$refs.menuTree.setCheckedNodes(this.menuTree)
                } else {
                    this.$refs.menuTree.setCheckedNodes([])
                }
            },
            submitPermEdit() {
                const _this = this
                const checkKeys = this.$refs.menuTree.getCheckedKeys()
                const checkNodes = this.$refs.menuTree.getCheckedNodes()
                for (const node of checkNodes) {
                    if (node.type == 1 && node.level == 1) {
                        let childFlag = false
                        for (const item of checkNodes) {
                            if (item.id !== node.id) {
                                if (item.parentid == node.id) {
                                    childFlag = true
                                    break
                                }
                            }
                        }
                        if (!childFlag) {
                            parseError({ error: '一级菜单下面必须选择子菜单', vm: _this })
                            return
                        }
                    }
                }
 
                const authNode = []
                for (const node of checkNodes) {
                    if (node.type == 4 && node.level == 3) {
                        for (const auth of authNode) {
                            if (auth.parentid == node.parentid) {
                                parseError({ error: '同一个节点下面的数据查看权限只能选择一项', vm: _this })
                                return
                            }
                        }
                        authNode.push(node)
                    }
                }
                updateRolePermission({ permissionIds: checkKeys, id: this.editRolePermId }).then(response => {
                    const res = response.data
                    if (res.code == 200) {
                        _this.$message({
                            message: '操作成功',
                            type: 'success'
                        })
                        initRouter(this)
                        _this.getRoleList()
                    } else {
                        parseError({ error: res.message, vm: _this })
                    }
                    this.permModalVisible = false
                })
            },
            handleNodeClick() {
 
            },
            handleNodeCheckChange(data, checked, indeterminate) {
 
            },
            handleNodeCheck(data, checkData) {
                const nodeId = data.id
                const node = this.$refs.menuTree.getNode(nodeId)
                if (node.checked) {
                    if (data.parentid == 0 || data.parentid) {
                        this.setNodeChecked(data.parentid)
                    }
                } else {
                    if (data.type == 4 && data.level == 3) {
                        return
                    }
                    this.cancelNodeChecked(data.id)
                }
            },
            setNodeChecked(nodeId) {
                const node = this.$refs.menuTree.getNode(nodeId)
                if (!node.checked) {
                    this.$refs.menuTree.setChecked(nodeId, true)
                }
                if (node) {
                    if (node.data.parentid == 0 || node.data.parentid) {
                        this.setNodeChecked(node.data.parentid)
                    }
                }
            },
            cancelNodeChecked(nodeId) {
                const checkNodes = this.$refs.menuTree.getCheckedNodes()
                const childNode = []
                for (const checkNode of checkNodes) {
                    if (checkNode.parentid == nodeId) {
                        childNode.push(checkNode)
                    }
                }
                if (childNode.length > 0) {
                    for (const child of childNode) {
                        this.$refs.menuTree.setChecked(child.id, false)
                        this.cancelNodeChecked(child.id)
                    }
                }
            }
        }
    }
</script>