userPermission.vue
3.84 KB
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
<template>
<div>
<el-dialog v-if="dialogVisible" :visible.sync="dialogVisible" :close-on-click-modal="false" title="编辑账号权限" width="60%">
<div style="float: left;width: 50%">
<el-table :data="permissionData" @select="handleSelectionChange" @select-all="handSelectAll">
<el-table-column type="selection" width="55" />
<el-table-column prop="name" label="模板名称" />
<el-table-column prop="code" label="标识" />
</el-table>
</div>
<div style="float: left;width: 40%;margin-left: 5%;border: 1px solid #ccc;padding: 15px;border-radius: 10px;height: 380px;overflow: auto">
<premission-tree ref="userTree" :api-ids="apiIds" :is-show-check-box="true" :is-user-set="true" @handChecked="handChecked" />
</div>
<div style="clear: both" />
<div slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false">取 消</el-button>
<el-button type="primary" @click="confirmPermission">确 定</el-button>
</div>
</el-dialog>
</div>
</template>
<script>
import { getPremissionList, getpremissionById, getUserPremissionList, putUserPremission } from '@/api/system/permissionTemplate'
import premissionTree from '../permission_template/premissionTree'
export default {
name: 'UserPermission',
components: { premissionTree },
props: {
permissionObj: {
type: Object,
require: false,
default() {
return {}
}
}
},
data() {
return {
dialogVisible: false,
permissionData: [],
apiIds: [],
childrenData: [],
userPermissions: []
}
},
watch: {
dialogVisible() {
if (this.dialogVisible) {
this.apiIds = []
this.childrenData = []
this.userPermissions = []
this.getPermissionData()
} else {
this.apiIds = []
this.childrenData = []
this.userPermissions = []
}
}
},
// mounted() {
// this.$nextTick(() => {
// console.log(324234)
// })
// if (this.dialogVisible) {
// this.getPermissionData()
// }
// },
methods: {
getPermissionData() {
getPremissionList().then(res => {
this.permissionData = res.content
this.getUserPermission()
})
},
getUserPermission() {
getUserPremissionList(this.permissionObj.id).then(res => {
this.userPermissions = res
this.$refs.userTree.$refs['preTree'].setCheckedKeys(res)
})
},
handChecked(data) {
this.childrenData = data
},
handleSelectionChange(selection, row) {
this.handSelectAll(selection)
},
handSelectAll(selection) {
if (selection.length === 0) {
this.apiIds = this.childrenData.concat(this.userPermissions)
this.$refs.userTree.$refs['preTree'].setCheckedKeys(this.apiIds)
return
}
this.apiIds = []
for (let i = 0; i < selection.length; i++) {
const item = selection[i]
getpremissionById(item.id).then(res => {
this.apiIds.push(...res)
this.apiIds.push(...this.childrenData)
this.apiIds.push(...this.userPermissions)
this.apiIds = this.unique(this.apiIds)
this.$refs.userTree.$refs['preTree'].setCheckedKeys(this.apiIds)
})
}
},
unique(arr) {
return Array.from(new Set(arr))
},
confirmPermission() {
const arr = this.$refs.userTree.getCheckedNodes()
const sarr = arr.filter(item => {
return typeof item.id === 'number'
})
const obj = {
userId: this.permissionObj.id,
permissionIds: []
}
sarr.forEach(item => {
obj.permissionIds.push(item.id)
})
putUserPremission(obj).then(res => {
this.$message.success('保存成功')
this.dialogVisible = false
})
}
}
}
</script>
<style scoped>
</style>