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
<template>
<div>
<el-dialog
title="分配角色资源"
:destroy-on-close="true"
:visible.sync="Visible"
width="50%"
@close="handleClose"
:close-on-click-modal="false"
top="10vh"
>
<div class="mb-5" v-for="(v, key) in resourceList" :key="key">
<div class="title-box mb-5">
<span class="mr-5 text-[16px] font-bold">{{ key }}</span>
<el-checkbox
:indeterminate="v.isIndeterminate"
v-model="v.checkAll"
@change="handleCheckAllChange($event, v)"
>全选</el-checkbox
>
</div>
<el-checkbox-group
v-model="form.resourceIdList"
@change="onChange($event, v)"
>
<el-row>
<el-col :span="12" v-for="item in v.list" :key="item.id">
<el-checkbox :label="item.id">{{ item.name }}</el-checkbox>
</el-col>
</el-row>
</el-checkbox-group>
</div>
<span slot="footer" class="dialog-footer">
<el-button size="small" @click="handleRest">重 置</el-button>
<el-button size="small" type="primary" @click="handleOk"
>确 定</el-button
>
</span>
</el-dialog>
</div>
</template>
<script>
import {
getResourceList,
getRoleResourceList,
addRoleResource,
} from "@/api/system";
export default {
components: {},
props: {
addVisible: {
type: Boolean,
required: true,
default: false,
},
},
data() {
return {
resourceList: {},
userResourceList: [], // 用户资源列表
form: {
resourceIdList: [],
roleId: "",
},
};
},
computed: {
Visible: {
get() {
return this.addVisible;
},
set(val) {
this.$emit("update:addVisible", val);
},
},
},
methods: {
// 获取资源列表
async getResourceList() {
let res = await getResourceList({
page: 1,
size: -1,
});
if (res.data.code == 1) {
let { data } = res.data.data;
this.resourceList = this.groupByAuth(data);
}
},
// 获取角色资源权限列表
async getRoleResourceList(roleId) {
let res = await getRoleResourceList({
size: -1,
page: 1,
roleId,
});
if (res.data.code == 1) {
let { data } = res.data.data;
let arr = data.filter((v) => v.resourceId);
this.form.resourceIdList = arr.map((v) => v.resourceId);
}
},
// 权限分组
groupByAuth(list) {
let group = {};
list.forEach((item) => {
let name = item.name.split("-")[0];
if (!group[name]) {
group[name] = {
isIndeterminate: false,
checkAll: false,
list: [],
};
}
group[name].list.push(item);
});
return group;
},
// 确定
async handleOk() {
let res = await addRoleResource(this.form);
let { code } = res.data;
if (code === 1) {
this.$message.success("添加成功");
this.$emit("addSuccess");
this.handleClose();
}
},
// 新增
onAdd(roleId) {
Object.assign(this.form, this.$options.data().form);
this.form.roleId = roleId;
this.getResourceList();
this.getRoleResourceList(roleId);
},
// 重置
handleRest() {
this.form.resourceIdList = [];
Object.keys(this.resourceList).forEach((key) => {
this.resourceList[key].checkAll = false;
this.resourceList[key].indeterminate = false;
});
},
// 关闭
handleClose() {
this.handleRest();
this.Visible = false;
},
handleCheckAllChange(checked, row) {
let rowIds = row.list.map((v) => v.id);
row.isIndeterminate = false;
if (checked) {
this.form.resourceIdList = [
...new Set([...this.form.resourceIdList, ...rowIds]),
];
} else {
this.form.resourceIdList = this.form.resourceIdList.filter((v) => {
return !rowIds.includes(v);
});
}
},
onChange(checkedList, row) {
let rowIds = row.list.map((v) => v.id);
let list = checkedList.filter((v) => {
return rowIds.includes(v);
});
row.isIndeterminate = !!list.length && list.length < rowIds.length;
row.checkAll = list.length === rowIds.length;
},
},
};
</script>
<style lang="less" scoped>
:deep(.el-dialog__body) {
max-height: 620px;
overflow: auto;
}
.title-box {
border-bottom: 1px solid #e9e9e9;
}
.el-col {
margin-bottom: 10px;
}
</style>