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
<template>
<div class="role">
<div class="head clear-both">
<el-button @click="add" plain type="success" style="float: right">新增角色</el-button>
</div>
<el-table
:data="roleList"
style="width: 100%">
<el-table-column
prop="id"
label="角色ID">
</el-table-column>
<el-table-column
prop="name"
label="角色名称">
</el-table-column>
<el-table-column
prop="menu_ids"
label="菜单IDs">
</el-table-column>
<el-table-column
prop="created_at"
label="创建时间">
</el-table-column>
<el-table-column
label="操作">
<template slot-scope="scope">
<el-button size="mini" plain type="primary" @click="edit(scope.row)">
编辑
</el-button>
<el-button size="mini" type="danger" plain @click="del(scope.row)">
删除
</el-button>
</template>
</el-table-column>
</el-table>
<page :nowPage="nowPage" :total="total"/>
<el-dialog
:title="dialog.title"
center
append-to-body
:visible.sync="dialog.show"
width="30%">
<el-form ref="form" :rules="dialog.rules" :model="dialog.form" label-width="100px">
<el-form-item label="菜单名称" prop="name">
<el-input v-model="dialog.form.name"></el-input>
</el-form-item>
<el-form-item label="菜单选项" prop="menu_ids">
<el-select v-model="dialog.form.menu_ids" multiple placeholder="请选择">
<el-option
v-for="item in dialog.select"
:key="item.id"
:label="item.name"
:value="item.id">
</el-option>
</el-select>
</el-form-item>
</el-form>
<span slot="footer" class="dialog-footer">
<el-button @click="dialog.show = false">取 消</el-button>
<el-button type="primary" @click="sub">确 定</el-button>
</span>
</el-dialog>
</div>
</template>
<script>
import {getMenuListApi,getRoleListApi,getRoleDetailApi,delRoleApi,updateRoleApi,saveRoleApi} from "../../service/api";
import page from '../framework/page'
export default {
name: "menu",
components:{
page
},
data(){
return {
nowPage:1,
total:0,
roleList:[],
dialog:{
title:'新增角色',
show:false,
select:[],
rules: {
name: [
{required: true, message: '请填写角色名称', trigger: 'change'},
],
menu_ids: [
{required: true, message: '请选择菜单', trigger: 'change'},
]
},
form:{
name:'',
menu_ids:[],
id: ''
}
},
}
},
created(){
this.getList()
},
methods:{
getMenuList(){
getMenuListApi().then(res=>{
if (res) {
this.dialog.select = res
}
})
},
getList(){
getRoleListApi().then(res=>{
if (res) {
this.roleList = res.list;
this.total = res.total;
}
})
},
edit(data){
this.dialog.form.id = data.id;
this.dialog.title = '编辑角色';
this.getMenuList();
getRoleDetailApi(data.id).then((res)=>{
this.dialog.form.name = res.name
let _ids = res.menu_ids.split(',').map((item)=>{
return parseInt(item);
})
this.dialog.form.menu_ids = _ids
this.dialog.show = true;
})
},
add(){
this.dialog.show = true;
this.dialog.form.id = '';
this.dialog.title = '新增菜单';
this.dialog.form.name = ''
this.dialog.form.menu_ids = [];
this.getMenuList();
},
del(data){
this.$confirm('此操作将删除该角色?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
delRoleApi(data.id).then(res=>{
this.$message({
type: 'success',
message: '删除成功!'
});
this.getList()
});
});
},
sub(){
this.$refs['form'].validate((valid) => {
if(valid){
let dia = this.dialog;
if(dia.form.id){
let json = {
name:dia.form.name,
menu_ids:dia.form.menu_ids.join(',')
};
updateRoleApi(dia.form.id,json).then(()=>{
this.$message({
type: 'success',
message: '修改成功!'
});
dia.show = false;
this.getList()
})
}else{
let json = {
name:dia.form.name,
menu_ids:dia.form.menu_ids.join(',')
};
saveRoleApi(json).then(()=>{
this.$message({
type: 'success',
message: '添加成功!'
});
dia.show = false;
this.getList()
})
}
}
})
}
}
}
</script>
<style scoped lang="less">
.role{
.head{
padding: 5px;
}
width: 100%;
padding: 10px;
.page-div{
text-align: center;
padding-top: 20px
}
}
.clear-both{
&:after{
content: '';
display: block;
clear: both;
}
}
</style>