premissionTree.vue
2.66 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
<template>
<div>
<el-tree
ref="preTree"
:data="treeData"
:props="dprops"
:default-checked-keys="apiIds"
:show-checkbox="showCheckBox"
node-key="id"
@check="checkItem"
@getCheckedNodes="getCheckedNodes"
/>
</div>
</template>
<script>
import { getPremission } from '@/api/system/permissionTemplate'
export default {
name: 'PremissionTree',
props: {
apiIds: {
type: Array,
default() {
return []
}
},
isUserSet: {
type: Boolean,
required: false,
default() {
return false
}
},
isShowCheckBox: {
type: Boolean,
required: false,
default() {
return false
}
}
},
data() {
return {
treeLoading: false,
treeData: [],
defaultIds: [],
dprops: {
label: 'label',
children: 'children'
}
}
},
computed: {
showCheckBox() {
if (this.isShowCheckBox) {
return true
} else {
return false
}
}
},
created() {
this.getPremission()
},
methods: {
getPremission() {
getPremission().then(res => {
const arr = res
const lastArr = []
let basePname = arr[0].pname
let baseObj = {
label: basePname,
id: basePname,
children: []
}
for (let i = 0; i < arr.length; i++) {
const item = arr[i]
if (item.pname === basePname) {
item.label = item.alias
baseObj.children.push(item)
} else {
lastArr.push(baseObj)
basePname = item.pname
item.label = item.alias
baseObj = {
label: basePname,
id: basePname,
children: []
}
baseObj.children.push(item)
}
}
this.treeData = lastArr
this.defaultIds = this.apiIds
})
},
checkItem(currentObj, treeStatus) {
if (!this.isUserSet) return
// 去除一级节点
const allSelectChecked = treeStatus.checkedKeys.filter(item => {
return typeof item === 'number'
})
// 获取手动点击的(非模版的)
const handSelectChecked = this.getArrDifference(allSelectChecked, this.apiIds)
this.$refs['preTree'].setCheckedKeys(allSelectChecked)
this.$emit('handChecked', handSelectChecked)
},
getCheckedNodes() {
return this.$refs.preTree.getCheckedNodes()
},
getArrDifference(arr1, arr2) {
return arr1.concat(arr2).filter(function(v, i, arr) {
return arr.indexOf(v) === arr.lastIndexOf(v)
})
}
}
}
</script>
<style scoped>
</style>