bus_role.vue
10.8 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
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
<template>
<div class="sys-container">
<div class="sys-pb-5">
<el-button icon="el-icon-refresh-right" size="small" @click="btnRefresh">刷新</el-button>
<el-button type="primary" size="small" icon="el-icon-plus" @click="editOrAddOpen(null, 'add')">
新增
</el-button>
</div>
<!-- table -->
<div class="sys-table-border">
<el-table :data="tableData" ref="multipleTable" highlight-current-row style="width: 100%" height="570">
<el-table-column label="序号" width="60" type="index" />
<el-table-column prop="id" label="id" v-if="false"></el-table-column>
<el-table-column prop="roleCode" label="角色编码" v-if="false">
</el-table-column>
<el-table-column prop="roleName" label="角色名称"> </el-table-column>
<el-table-column prop="remark" label="备注"> </el-table-column>
<el-table-column prop="created" label="创建时间" width="180">
</el-table-column>
<el-table-column prop="createdBy" label="创建人"> </el-table-column>
<el-table-column label="操作" width="120">
<template slot-scope="scope">
<el-button type="text" @click="editOrAddOpen(scope.row, 'edit')">编辑</el-button>
<el-button style="color: red" type="text" @click="deleteAciton(scope.row)">删除</el-button>
</template>
</el-table-column>
</el-table>
</div>
<!-- dialog 新增和编辑 -->
<el-dialog :title="editOrAddOpt.title" v-if="editOrAddOpt.visible" :close-on-click-modal="false"
:visible.sync="editOrAddOpt.visible" width="60%">
<el-form :model="editOrAddOpt.form" label-width="80px">
<el-row :gutter="20">
<el-col :span="24">
<el-form-item label="名称">
<el-input v-model="editOrAddOpt.form.roleCode" ref="roleCode" maxlength="20" show-word-limit
autocomplete="off"></el-input>
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col :span="24">
<el-form-item label="备注">
<el-input v-model="editOrAddOpt.form.remark" type="textarea" :rows="2" autocomplete="off">
</el-input>
</el-form-item>
</el-col>
</el-row>
<el-row :gutter="20">
<el-col>
<el-form-item label="权限">
<el-tree :data="treeOpt.data" show-checkbox node-key="id" :props="treeOpt.defaultProps"
:default-expand-all="false" :check-strictly="true" :expand-on-click-node="false"
:highlight-current="true" style="margin-top: 5px; height: 260px; overflow-y: auto"
@node-click="roleNodeClick" ref="treePer" @check="handleNodeCheck">
<!-- 自定义节点模板 -->
<template #default="{ node, data }">
<span class="custom-tree-node">
<!-- 根据权限类型显示不同图标 -->
<i v-if="data.permissionType === 10" class="icon-module"></i>
<i v-if="data.permissionType === 1" class="icon-folder"></i>
<i v-if="data.permissionType === 5" class="icon-menu"></i>
<i v-if="data.permissionType === 15" class="icon-btn"></i>
<span>{{ node.label }}</span>
</span>
</template>
</el-tree>
</el-form-item>
</el-col>
</el-row>
</el-form>
<span slot="footer" class="dialog-footer">
<el-button type="primary" @click="userAddOrEditSave">确 定</el-button>
<el-button @click="editOrAddOpt.visible = false">取 消</el-button>
</span>
</el-dialog>
</div>
</template>
<script>
import * as userApi from "@/api/systemPermission/userPermission";
export default {
data() {
const editOrAddData = {
roleCode: "",
roleName: "",
remark: null,
Disable: false,
//角色拥有的权限数据
PermissionsIds: [],
Permissions: null,
Created: null,
CreatedBy: null,
Updated: null,
UpdatedBy: null,
Id: 0,
};
return {
tableData: [],
editOrAddOpt: {
visible: false,
flag: "add",
title: "新增",
form: { ...editOrAddData },
formDefaultData: JSON.parse(JSON.stringify(editOrAddData)),
},
//临时存储的数据
treeOpt: {
data: [], // 处理后的数据(展示用)
originData: [], // 原始数据
defaultProps: {
children: "children",
label: "label",
},
},
};
},
methods: {
/**
* 查询或者搜索
*/
btnRefresh() {
this.getAllPermissionList();
this.getData();
},
getData() {
userApi.getUserRole().then((res) => {
userApi.ajaxSuccessBefore(res, (res) => {
this.tableData = res.data;
});
});
},
/**
* 获取权限数据
*/
getAllPermissionList() {
userApi.getAllPermission().then((res) => {
userApi.ajaxSuccessBefore(res, (res) => {
this.treeOpt.data = userApi.transformToTreeData(res.data);
this.treeOpt.originData = res.data;
//console.log(res.data, "all");
});
});
},
/**
* 新增或者编辑弹出框
*/
editOrAddOpen(row, flag) {
debugger;
if (this.treeOpt.data.length == 0) {
this.$message.error(
"权限数据加载中...请稍后在操作,反复出现请联系管理员!"
);
return;
}
this.editOrAddOpt.flag = flag;
this.editOrAddOpt.visible = true;
if (flag == "add") {
this.editOrAddOpt.form = JSON.parse(
JSON.stringify(this.editOrAddOpt.formDefaultData)
);
this.editOrAddOpt.title = "新增";
} else {
this.editOrAddOpt.title = `编辑,用户名【${row.roleName}】`;
this.editOrAddOpt.form = row;
userApi.getPermissionByRoleId(row.id).then((res) => {
if (res.code !== "Success") return;
const keys = res.data.map((x) => x.id);
this.$nextTick(() => {
this.$refs.treePer.setCheckedKeys(keys);
});
console.log(keys, "keys");
});
}
this.$nextTick(() => {
debugger;
const firstNode = this.$refs.treePer.root.childNodes[0];
if (firstNode) firstNode.expand();
});
},
/**
* 新增或者编辑 保存操作
*/
userAddOrEditSave() {
debugger;
if (
this.editOrAddOpt.form.roleCode == "" ||
this.editOrAddOpt.form.roleCode == null
) {
this.$message.error("权限名称不能为!");
this.$refs.roleCode.focus();
return;
}
this.editOrAddOpt.form.roleName = this.editOrAddOpt.form.roleCode;
var selectData = this.getSelectRoleVal();
if (selectData.length == 0) return;
this.editOrAddOpt.form.PermissionsIds = selectData;
if (this.editOrAddOpt.flag == "edit") {
userApi.editRoleAndPermissions(this.editOrAddOpt.form).then((res) => {
userApi.ajaxSuccessBefore(
res,
(res) => {
this.editOrAddOpt.visible = false;
this.getData();
},
this
);
});
return;
}
userApi.addRoleAndPermissions(this.editOrAddOpt.form).then((res) => {
userApi.ajaxSuccessBefore(
res,
(res) => {
this.editOrAddOpt.visible = false;
this.getData();
},
this
);
});
},
//删除
deleteAciton(row) {
var title = `此操作将永久删除该数据,【${row.roleName}】,是否继续?`;
this.$confirm(title, "提示", { type: "warning" })
.then(() => {
userApi.deleteRoleByIds([row.id]).then((res) => {
userApi.ajaxSuccessBefore(
res,
(res) => {
this.editOrAddOpt.visible = false;
this.getData();
},
this
);
});
})
.catch();
},
setCurrent(row) {
this.$refs.multipleTable.setCurrentRow(row);
},
//获取选中的权限
getSelectRoleVal() {
var result = this.$refs.treePer.getCheckedKeys();
if (result.length == 0) {
this.$message.error("请选择权限数据!");
return [];
}
return result;
},
roleNodeClick(data, node, event) {
console.log("selectRowData", data);
},
// 处理节点勾选事件
handleNodeCheck(data, { checkedNodes }) {
// 新增:获取当前节点自身的选中状态
const isChecked = checkedNodes.some((node) => node.id === data.id);
if (isChecked) {
// 勾选时同步子节点
if (data.children) {
this.toggleChildren(data.children, true);
}
} else {
// 取消勾选时同步子节点(关键修复)
if (data.children) {
const childKeys = this.getAllChildKeys(data);
const currentChecked = this.$refs.treePer.getCheckedKeys();
const newChecked = currentChecked.filter(
(k) => !childKeys.includes(k)
);
this.$refs.treePer.setCheckedKeys(newChecked);
}
}
}, // 新增方法:获取所有子节点键值
getAllChildKeys(node) {
return (
node.children?.reduce((acc, child) => {
acc.push(child.id);
if (child.children) acc.push(...this.getAllChildKeys(child));
return acc;
}, []) || []
);
},
// 修改后的子节点同步方法
toggleChildren(children, checked) {
children.forEach((child) => {
this.$refs.treePer.setChecked(child.id, checked);
if (child.children) this.toggleChildren(child.children, checked);
});
},
},
mounted() {
this.getAllPermissionList();
this.getData();
},
};
</script>
<style scoped lang="scss">
.sys-container {
width: 100%;
.sys-pb-5 {
padding-bottom: 5px !important;
}
.sys-form-item {
display: flex;
border-radius: 4px;
min-height: 46px;
margin-bottom: 10px;
align-items: center;
}
.sys-table-border {
border: 1px solid #e5e9f2;
}
.sys-pagination-container {
text-align: center;
}
}
.current-row {
background-color: #ecf5ff;
}
/* 图标样式 */
.custom-tree-node i[class^="icon-"] {
display: inline-block;
width: 16px;
height: 16px;
margin-right: 5px;
vertical-align: middle;
}
.icon-module {
background: url(~@/assets/images/module.png) no-repeat center/contain;
}
.icon-folder {
background: url(~@/assets/images/folder.png) no-repeat center/contain;
}
.icon-menu {
background: url(~@/assets/images/menu.png) no-repeat center/contain;
}
.icon-btn {
background: url(~@/assets/images/btn.png) no-repeat center/contain;
}
</style>