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
<template>
<div class="admin">
<div class="head clear-both">
<el-button @click="add" plain type="success" style="float: right">新增角色</el-button>
</div>
<el-table
:data="adminList"
style="width: 100%">
<el-table-column
prop="id"
label="账号ID">
</el-table-column>
<el-table-column
prop="user_name"
label="用户名">
</el-table-column>
<el-table-column
prop="role_id"
label="角色">
</el-table-column>
<el-table-column
label="状态">
<template slot-scope="scope">
{{scope.row.status | adminStatus}}
</template>
</el-table-column>
<el-table-column
label="操作">
<template slot-scope="scope">
<el-popover
placement="top"
width="280">
<div style="text-align: center">
<el-button size="mini" plain type="primary" @click="edit(scope.row)">
编辑
</el-button>
<el-button size="mini" type="warning" plain @click="editPW(scope.row)">
修改密码
</el-button>
<el-button size="mini" type="danger" plain @click="del(scope.row)">
删除
</el-button>
</div>
<el-button slot="reference" size="mini" type="text" >操作</el-button>
</el-popover>
</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="80px">
<el-form-item label="账号" prop="username" v-if="dialog.dialogType === 0">
<el-input v-model="dialog.form.username"></el-input>
</el-form-item>
<el-form-item label="密码" prop="password" v-if="dialog.dialogType === 2 || dialog.dialogType === 0">
<el-input type="password" v-model="dialog.form.password"></el-input>
</el-form-item>
<el-form-item label="确认密码" prop="surePassword" v-if="dialog.dialogType === 2 || dialog.dialogType === 0 ">
<el-input type="password" v-model="dialog.form.surePassword"></el-input>
</el-form-item>
<el-form-item label="角色" prop="role_id" v-if="dialog.dialogType !== 2">
<el-select v-model="dialog.form.role_id" placeholder="请选择">
<el-option
v-for="item in roleList"
:key="item.id"
:label="item.name"
:value="item.id">
</el-option>
</el-select>
</el-form-item>
<el-form-item label="简述" prop="desc" v-if="dialog.dialogType !== 2">
<el-input v-model="dialog.form.desc"></el-input>
</el-form-item>
<el-form-item label="状态" prop="status" v-if="dialog.dialogType === 1">
<el-select v-model="dialog.form.status" placeholder="请选择">
<el-option
v-for="item in dialog.select"
:key="item.code"
:label="item.value"
:value="item.code">
</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="onSub">确 定</el-button>
</span>
</el-dialog>
</div>
</template>
<script>
import {getAdminListApi,editAdminListApi,addAdminListApi,delAdminListApi,editPasswordApi,getRoleListApi} from "../../service/api";
import {ADMINSTATUS} from "../../util/wordbook";
import page from '../framework/page'
export default {
name: "admin",
components:{
page
},
data(){
// 表单验证
let usernameCheck=(rule, value, callback)=>{
if (value === '' && this.dialog.dialogType === 0) {
callback(new Error('请输入账号'));
} else {
callback();
}
};
let passCheck= (rule, value, callback) => {
if (value === '' && (this.dialog.dialogType === 0 || this.dialog.dialogType === 2 )) {
callback(new Error('请输入密码'));
} else {
if(this.dialog.form.surePassword !== '' && (this.dialog.dialogType === 0 || this.dialog.dialogType === 2 )){
this.$refs['form'].validateField('surePassword');
}
callback();
}
};
let surePassCheck= (rule, value, callback) => {
if(this.dialog.dialogType === 0 || this.dialog.dialogType === 2 ){
if (value === '') {
callback(new Error('请再次输入密码'));
} else if (value !== this.dialog.form.password) {
callback(new Error('两次输入密码不一致!'));
}else{
callback();
}
} else {
callback();
}
};
let roleIdCheck= (rule, value, callback) => {
if(this.dialog.dialogType === 0 || this.dialog.dialogType === 1 ){
if (value === '') {
callback(new Error('请输入角色'));
}
}else{
callback()
}
};
let descCheck= (rule, value, callback) => {
if(this.dialog.dialogType === 0 || this.dialog.dialogType === 1 ){
if (value === '') {
callback(new Error('请输入简述'));
}
}else{
callback()
}
};
return {
nowPage:1,
total:0,
adminList:[],
dialog:{
dialogType:0,
title:'新增账号',
show:false,
select:[{
code:0,
value:ADMINSTATUS['0']
},{
code:1,
value:ADMINSTATUS['1']
}],
rules:{
username:[{ validator: usernameCheck, trigger: 'blur' }],
password:[{ validator: passCheck, trigger: 'blur' }],
surePassword:[{ validator: surePassCheck, trigger: 'blur' }],
role_id:[{ validator: roleIdCheck, trigger: 'blur' }],
desc:[{ validator: descCheck, trigger: 'blur' }],
},
form:{
id:'',
username:'',
password:'',
surePassword:'',
role_id:'',
status:0,
desc:''
}
},
roleList: []
}
},
filters:{
adminStatus:function (value) {
return ADMINSTATUS[value]
},
},
created(){
this.getList();
this.getRoleList();
},
methods:{
getRoleList(){
getRoleListApi({page: 1,limit: 100}).then(res=>{
if (res) {
this.roleList = res.list;
}
})
},
getList(){
getAdminListApi().then(res=>{
this.adminList = res.list
this.total=Number(res.total)
this.$store.commit('mainCanShow')
})
},
editPW(data){
this.dialog.form.id = data.id;
this.dialog.dialogType = 2;
this.dialog.title = '修改密码';
this.dialog.form.password = '';
this.dialog.form.surePassword = '';
this.dialog.show = true;
},
edit(data){
this.dialog.show = true;
this.dialog.dialogType=1;
this.dialog.form.id = data.id;
this.dialog.title = '编辑';
this.dialog.form.username = data.user_name;
this.dialog.form.password = '';
this.dialog.form.status = data.status;
this.dialog.form.role_id = data.role_id;
this.dialog.form.desc = data.desc
},
add(){
this.dialog.show = true;
this.dialog.dialogType=0;
this.dialog.form.id = '';
this.dialog.title = '新增';
this.dialog.form.username = '';
this.dialog.form.status = 0;
this.dialog.form.password = '';
this.dialog.form.role_id = '';
this.dialog.form.desc = ''
},
del(data){
this.$confirm('此操作将删除该账号?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
delAdminListApi(data.id).then(res=>{
this.$message({
type: 'success',
message: '删除成功!'
});
});
this.getList()
});
},
onSub(){
// this.$refs['form'].validate((valid) => {
// if(valid){
let dia = this.dialog;
if(dia.dialogType === 1){
let json = {
role_id:dia.form.role_id,
desc:dia.form.desc,
status:dia.form.status
};
editAdminListApi(dia.form.id,json).then(()=>{
this.$message({
type: 'success',
message: '修改成功!'
});
dia.show = false;
this.getList()
})
}else if(dia.dialogType === 0){
let json = {
role_id:dia.form.role_id,
desc:dia.form.desc,
passwd:dia.form.password,
username:dia.form.username
};
addAdminListApi(json).then(()=>{
this.$message({
type: 'success',
message: '添加成功!'
});
dia.show = false;
this.getList()
})
}else if(dia.dialogType === 2){
console.log(1)
let json = {
passwd_new:dia.form.password
}
editPasswordApi(dia.form.id,json).then(()=>{
this.$message({
type: 'success',
message: '修改成功!'
});
dia.show = false;
this.getList()
})
}
// }
// })
}
}
}
</script>
<style scoped lang="less">
.admin{
.head{
padding: 5px;
}
width: 100%;
padding: 10px;
.page-div{
text-align: center;
padding-top: 20px
}
}
.clear-both{
&:after{
content: '';
display: block;
clear: both;
}
}
</style>