66 </div >
77 <div class =" details" >
88 <div class =" permissions-box" v-for =" (auth, moduleName) in allAuths" :key =" moduleName" >
9- <el-checkbox-group v-model =" auths " >
9+ <el-checkbox-group v-model =" auth_module_name " >
1010 <div class =" module-box" >
1111 <el-checkbox
1212 @change =" moduleCheck($event, auth, moduleName)"
1515 :indeterminate =" halfAuths.includes(moduleName)"
1616 ></el-checkbox >
1717 </div >
18-
19- <ul class =" permissions-ul" >
20- <li class =" permissions-li" v-for =" (item, key) in auth" :key =" key" >
21- <el-checkbox
22- :label =" key | filterTitle(32)"
23- @change =" singleCheck($event, key, moduleName)"
24- ></el-checkbox >
25- </li >
26- </ul >
2718 </el-checkbox-group >
19+ <ul class =" permissions-ul" >
20+ <li class =" permissions-li" v-for =" (item, key) in auth" :key =" key" >
21+ <el-checkbox
22+ :label =" item.name"
23+ :value =" auth_module_ids.indexOf(item.id) > -1"
24+ @change =" singleCheck(item.id, auth, moduleName)"
25+ ></el-checkbox >
26+ </li >
27+ </ul >
2828 </div >
2929 </div >
3030 </div >
3131 </div >
3232</template >
3333
3434<script >
35- import Utils from ' lin/utils/util'
3635import Admin from ' @/lin/models/admin'
3736
3837export default {
3938 props: [' id' , ' title' ],
4039 data () {
4140 return {
4241 allAuths: {}, // 所有分组权限
43- auths: [], // 拥有的分组权限
42+ // auths: [], // 拥有的分组权限
43+ auth_module_name: [], // 权限组 module name
44+ auth_module_ids: [], // 权限组 集合 id
4445 halfAuths: [], // 该分类下的权限没有全选中
4546 cacheFlag: true ,
4647 loading: false ,
@@ -53,82 +54,79 @@ export default {
5354 },
5455 // 获取分组权限
5556 async getGroupAuths () {
56- this .auths = [] // 父组件 重置
57+ // this.auths = [] // 父组件 重置
5758 this .allAuths = await Admin .getAllAuths ()
5859 // 通过判断有没有传入id,来判断当前页面是添加分组还是编辑分组
5960 if (this .id ) {
60- let res = await Admin .getOneGroup (this .id )
61- // 获取分组所拥有的权限
62- /* eslint-disable */
63- res = JSON .parse (JSON .stringify (res)) // 去除__ob__
64- for (let i = 0 ; i < res .auths .length ; i++ ) {
65- for (const key in res .auths [i]) {
66- for (let j = 0 ; j < res .auths [i][key].length ; j++ ) {
67- this .auths .push (res .auths [i][key][j].auth )
68- }
61+ const res = await Admin .getOneGroup (this .id )
62+ let temp = []
63+ const cache = {}
64+ res .permissions .forEach (v => {
65+ this .auth_module_ids .push (v .id )
66+ temp .push (v .module )
67+ if (! cache[v .module ]) {
68+ cache[v .module ] = 1
69+ } else {
70+ cache[v .module ]++
6971 }
70- }
71- this .$emit (' updateCacheAuths' , this .auths )
72- // 检查module状态是否需要选中
73- for (const key in this .allAuths ) {
74- this .initModuleCheck (key)
75- }
72+ })
73+ temp = Array .from (new Set (temp))
74+ temp .forEach (item => {
75+ if (this .allAuths [item].length === cache[item]) {
76+ this .auth_module_ids .push (item)
77+ } else {
78+ this .halfAuths .push (item)
79+ }
80+ })
81+ this .auth_module_name = Array .from (new Set (temp))
7682 }
7783 this .$emit (' updateAuths' , this .auths )
7884 this .$emit (' updateAllAuths' , this .allAuths )
7985 },
80- // 弹窗打开时,判断某一分类权限是否全部选中
81- initModuleCheck (moduleName ) {
82- const currentModuleChildrenArr = Object .keys (this .allAuths [moduleName])
83- const intersect = Utils .getIntersect (currentModuleChildrenArr, this .auths )
84- // 全选
85- if (intersect .length === currentModuleChildrenArr .length ) {
86- this .auths .push (moduleName)
87- }
88- // 半选
89- if (intersect .length > 0 && intersect .length < currentModuleChildrenArr .length ) {
90- this .halfAuths .push (moduleName)
91- }
92- },
93- moduleCheck (checked , auth , moduleName ) {
94- const authArr = Object .keys (auth)
86+
87+ // 批量选中
88+ moduleCheck (checked , ids , moduleName ) {
89+ const _ids = ids .map (item => item .id )
9590 if (checked) {
96- if (this .halfAuths .indexOf (moduleName) > - 1 ) {
97- this .halfAuths .splice (this .halfAuths .indexOf (moduleName), 1 )
98- }
99- this .auths .push (... authArr)
91+ this .auth_module_ids = Array .from (new Set (this .auth_module_ids .concat (_ids)))
92+ this .auth_module_name .push (moduleName)
93+ this .halfAuths = this .halfAuths .filter (v => v !== moduleName)
10094 } else {
101- if (this .halfAuths .indexOf (moduleName) > - 1 ) {
102- this .halfAuths .splice (this .halfAuths .indexOf (moduleName), 1 )
103- }
104- this .auths = this .auths .filter (x => authArr .indexOf (x) < 0 )
95+ this .auth_module_ids = this .auth_module_ids .filter (v => ! _ids .includes (v))
96+ this .auth_module_name = this .auth_module_name .filter (v => v !== moduleName)
10597 }
106- this .$emit (' updateAuths' , this .auths )
98+ // this.$emit('updateAuths', this.auths)
10799 },
108- singleCheck (checked , singleAuth , moduleName ) {
109- const currentModuleChildrenArr = Object .keys (this .allAuths [moduleName])
110- const intersect = Utils .getIntersect (currentModuleChildrenArr, this .auths )
111- if (intersect .length === currentModuleChildrenArr .length ) {
112- if (this .halfAuths .indexOf (moduleName) > - 1 ) {
113- this .halfAuths .splice (this .halfAuths .indexOf (moduleName), 1 )
114- }
115- this .auths .push (moduleName)
116- } else if (intersect .length > 0 && intersect .length < currentModuleChildrenArr .length ) {
117- if (this .auths .indexOf (moduleName) > - 1 ) {
118- this .auths .splice (this .auths .indexOf (moduleName), 1 )
119- }
120- this .halfAuths .push (moduleName)
121- } else if (intersect .length === 0 ) {
122- if (this .halfAuths .indexOf (moduleName) > - 1 ) {
123- this .halfAuths .splice (this .halfAuths .indexOf (moduleName), 1 )
100+
101+ // 单选
102+ singleCheck (id , auth , moduleName ) {
103+ const _ids = auth .map (item => item .id )
104+ let count = 0
105+ const index = this .auth_module_ids .indexOf (id)
106+ if (index === - 1 ) {
107+ this .auth_module_ids .push (id)
108+ } else {
109+ this .auth_module_ids .splice (index, 1 )
110+ }
111+ _ids .forEach (item => {
112+ if (this .auth_module_ids .indexOf (item) > - 1 ) {
113+ count++
124114 }
125- if (this .auths .indexOf (moduleName) > - 1 ) {
126- this .auths .splice (this .auths .indexOf (moduleName), 1 )
115+ // 全选状态
116+ if (_ids .length === count) {
117+ this .auth_module_name .push (moduleName)
118+ this .halfAuths = this .halfAuths .filter (v => v !== moduleName)
119+ } else if (count === 0 ) {
120+ // 未选中状态
121+ this .auth_module_name = this .auth_module_name .filter (v => v !== moduleName)
122+ this .halfAuths = this .halfAuths .filter (v => v !== moduleName)
123+ } else {
124+ // 半选状态
125+ this .auth_module_name = this .auth_module_name .filter (v => v !== moduleName)
126+ this .halfAuths .push (moduleName)
127127 }
128- }
129- this .halfAuths = Array .from (new Set (this .halfAuths ))
130- this .auths = Array .from (new Set (this .auths ))
131- this .$emit (' updateAuths' , this .auths )
128+ })
129+ this .$emit (' updateAuths' , this .auth_module_ids )
132130 },
133131 },
134132 async created () {
0 commit comments