审查视图

src/views/nav1/user.vue 19.5 KB
朱兆平 authored
1 2 3 4 5 6
<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
7 8 9
					<el-input v-model="filters.realName" placeholder="真实姓名">
						<template slot="prepend">姓名</template>
					</el-input>
朱兆平 authored
10 11
				</el-form-item>
				<el-form-item>
12 13 14
					<el-input v-model="filters.userName" placeholder="登录账号">
						<template slot="prepend">账号</template>
					</el-input>
15 16
				</el-form-item>
				<el-form-item>
17
					<el-button type="primary" v-on:click="getUsers" icon="el-icon-search">查询</el-button>
18 19
				</el-form-item>
				<el-form-item>
20
					<el-button type="success" @click="handleAdd" icon="el-icon-document">新增</el-button>
朱兆平 authored
21 22 23 24 25
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
26 27 28 29
		<el-table :data="users" highlight-current-row border v-loading="listLoading"
				  @selection-change="selsChange"
				  tooltip-effect="dark"
				  style="border-radius: 10px 10px 0px 0px;line-height: 25px;"
30
				  :header-cell-style="{background:'#6F8294',color:'#FFFFFF'}" >
31 32
			<el-table-column type="selection" width="55">
			</el-table-column>
朱兆平 authored
33 34
			<!--<el-table-column type="index" width="60">-->
			<!--</el-table-column>-->
35 36 37 38 39 40 41 42
			<el-table-column prop="userId" label="ID" width="100" sortable>
			</el-table-column>
			<el-table-column prop="username" label="账号" width="120" sortable>
			</el-table-column>
			<el-table-column prop="realname" label="姓名" width="120" sortable>
			</el-table-column>
			<el-table-column prop="sex" label="性别" width="100" :formatter="formatSex" sortable>
			</el-table-column>
朱兆平 authored
43
			<el-table-column prop="mobilephone" label="电话" width="125">
44 45 46 47 48
			</el-table-column>
			<el-table-column prop="creattime" label="创建时间" width="170" sortable>
			</el-table-column>
			<el-table-column prop="updatetime" label="更新时间" width="170" sortable>
			</el-table-column>
49 50 51 52
<!--			<el-table-column prop="address" label="地址" min-width="180">-->
<!--			</el-table-column>-->
<!--			<el-table-column prop="email" label="Email" min-width="180">-->
<!--			</el-table-column>-->
53 54
			<el-table-column prop="state" label="启用" width="100" :formatter="formatState" sortable>
			</el-table-column>
55
			<el-table-column label="操作" width="250" fixed="right">
56
				<template slot-scope="scope">
57 58 59
					<el-button  @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="info" @click="roleEdit(scope.$index, scope.row)">角色配置</el-button>
					<el-button type="danger"  @click="handleDel(scope.$index, scope.row)">删除</el-button>
60 61 62 63 64 65 66
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
shenhailong authored
67
			<el-pagination layout="total, prev, pager, next" @current-change="handleCurrentChange" :page-size="10" :total="total" style="float:right;">
68 69
			</el-pagination>
		</el-col>
朱兆平 authored
70
71 72 73 74 75 76 77 78 79 80 81 82 83
		<!--新增界面-->
		<el-dialog :title="dialogMap[dialogStatus]" :visible.sync="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" ref="addForm" class="user-form">
<!--				<el-form-item label="所属公司" prop="companyId">-->
<!--					<el-select v-model="addForm.companyId" filterable placeholder="请选择">-->
<!--						<el-option-->
<!--								v-for="item in options"-->
<!--								:key="item.roleId"-->
<!--								:label="item.roleName"-->
<!--								:value="item.roleId">-->
<!--						</el-option>-->
<!--					</el-select>-->
<!--				</el-form-item>-->
84
				<el-form-item label="ID">
85
					<span>{{addForm.userId}}</span>
86
				</el-form-item>
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
				<el-row>
					<el-col :span="12">
						<el-form-item label=" " prop="username" :rules="addFormRules.username">
							<el-input v-model="addForm.username" aria-placeholder="用户名长度在5-11位,支持英文和数字">
								<template slot="prepend">账&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;号</template>
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label=" " prop="realname">
							<el-input v-model="addForm.realname">
								<template slot="prepend">姓&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;名</template>
							</el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="12">
						<el-form-item label=" " prop="password" :rules="dialogStatus==='update'?[{ required: false}]:addFormRules.password">
							<el-input v-model="addForm.password" type="password" show-password :disabled="dialogStatus==='update'">
								<template slot="prepend">密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码</template>
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label=" " prop="checkPass" :rules="dialogStatus==='update'?[{ required: false}]:addFormRules.checkPass">
							<el-input v-model="addForm.checkPass" type="password" show-password :disabled="dialogStatus==='update'">
								<template slot="prepend">确认密码</template>
							</el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="12">
						<el-form-item label=" " prop="email">
							<el-input v-model="addForm.email">
								<template slot="prepend">Email&nbsp;&nbsp;&nbsp;&nbsp;</template>
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label=" " prop="address">
							<el-input v-model="addForm.address">
								<template slot="prepend">地&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;址</template>
							</el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="12">
						<el-form-item label=" " prop="mobilephone">
							<el-input v-model="addForm.mobilephone">
								<template slot="prepend">电&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;话</template>
							</el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<el-divider></el-divider>
				<el-row>
					<el-col :span="12">
						<el-form-item label="性别" prop="sex">
							<el-radio-group v-model="addForm.sex">
								<el-radio class="radio" :label="1">男</el-radio>
								<el-radio class="radio" :label="0">女</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="启用" prop="state">
							<el-radio-group v-model="addForm.state">
								<el-radio class="radio" :label="true" >是</el-radio>
								<el-radio class="radio" :label="false" >否</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="24">
						<el-form-item label="年龄" prop="age">
							<el-input-number v-model="addForm.age" :min="0" :max="200"></el-input-number>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="24">
						<el-form-item lable="头像" prop="userface">
							<el-upload
									class="avatar-uploader"
									action="http://127.0.0.1:7003/upload"
									:show-file-list="true"
									accept="image/jpeg,image/jpg,image/png"
									:on-success="handleAvatarSuccess"
									:before-upload="beforeAvatarUpload"
朱兆平 authored
180
							>
181 182 183 184 185 186
								<img v-if="faceImageUrl" :src="faceImageUrl" class="avatar">
								<i v-else class="el-icon-plus avatar-uploader-icon"></i>
							</el-upload>
						</el-form-item>
					</el-col>
				</el-row>
187 188 189
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
190
				<el-button type="primary" @click.native="dialogStatus==='create'?addSubmit():editSubmit()" :loading="addLoading">提交</el-button>
191 192
			</div>
		</el-dialog>
朱兆平 authored
193 194

		<!--权限设置-->
195
		<el-dialog title="权限设置" :visible.sync="roleFormVisible" :close-on-click-modal="false">
朱兆平 authored
196 197 198 199 200 201 202
			<el-form :model="roleEditForm" label-width="80px"  ref="roleEditForm" >
				<el-form-item label="ID">
					<span>{{roleEditForm.userId}}</span>
				</el-form-item>
				<el-form-item label="账号">
					<span>{{roleEditForm.username}}</span>
				</el-form-item>
朱兆平 authored
203
				<el-tree :data="roles" :props="treeDefaultProps" default-expand-all show-checkbox highlight-current check-strictly node-key="roleId"  ref="tree" @check-change="treeHandleCheckChange">
204
				</el-tree>
朱兆平 authored
205
			</el-form>
206
朱兆平 authored
207 208 209 210 211
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="roleFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="roleEditSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
朱兆平 authored
212 213
	</section>
</template>
214
朱兆平 authored
215
<script>
216
	import util from '../../common/js/util'
朱兆平 authored
217
	import NProgress from 'nprogress'
218
	import { getUserList, removeUser, batchRemoveUser, editUser, addUser, setUserRole } from '../../api/user';
朱兆平 authored
219
    import { getList} from '../../api/role_api';
220
	import { getList as getRoleList} from '../../api/role_api';
221
    import moment from 'moment'
朱兆平 authored
222 223
	export default {
		data() {
224 225 226 227 228 229 230 231 232 233
			var validatePass = (rule, value, callback) => {
				if (value === '') {
					callback(new Error('请输入密码'));
				} else {
					if (this.checkPass !== '') {
						this.$refs.addForm.validateField('checkPass');
					}
					callback();
				}
			};
朱兆平 authored
234 235
			return {
				filters: {
shenhailong authored
236 237
                    userName: '',
					realName: ''
朱兆平 authored
238
				},
239 240 241 242
				treeDefaultProps: {
                    children: 'children',
                    label: 'roleName',
                },
243 244 245 246 247
				dialogMap: {
					update: '编辑',
					create: '新增',
				},
				dialogStatus: 'create',
248 249 250
				users: [],
				total: 0,
                pageNum: 1,
朱兆平 authored
251
                pageSize: 10,
朱兆平 authored
252
                faceImageUrl: '',
253 254 255 256 257 258 259
				listLoading: false,
				sels: [],//列表选中列
				//编辑界面是否显示
				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				addFormRules: {
                    username: [
朱兆平 authored
260 261 262 263
						{ required: true, message: '请输入姓名,用户名长度在5-11位,支持英文和数字', trigger: 'blur' },
					],
					password: [
						{ required: true, message: '请输入密码,用户名长度在6-18位,支持英文和数字和非空字符', trigger: 'blur'}
264
					],
265 266
					checkPass: [
						{ required: true, message: '请输入密码,用户名长度在6-18位,支持英文和数字和非空字符', trigger: 'blur'}
267 268
					]
				},
朱兆平 authored
269 270
				//用户角色配置
                roleFormVisible: false,
271
				//新增界面数据
272
273
				addForm: {
274 275
					checkPass:'',
					userId: 0,
276 277 278 279 280 281 282
                    username: '',
                    password: '',
                    sex: 1,
                    address: '',
                    realname: '',
                    email:	'',
                    mobilephone: '',
283
					age: 1,
284
					state: true
朱兆平 authored
285 286 287 288 289 290 291
				},
                roleEditForm: {
                    userId: 1,
					username: "",
					roles: []
				},
				roles: [],
292
                roleIds: [],
293
				options: [],
朱兆平 authored
294 295 296 297 298 299 300
			}
		},
		methods: {
			//性别显示转换
			formatSex: function (row, column) {
				return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
			},
301 302 303 304 305 306 307
            formatState: function (row, column) {
                return row.state == true ? '是' : row.state == false ? '否' : '未知';
            },
			handleCurrentChange(val) {
				this.pageNum = val;
				this.getUsers();
			},
朱兆平 authored
308
			//获取用户列表
309
			getUsers:function() {
朱兆平 authored
310
				let para = {
311 312
                    pageNum: this.pageNum,
                    pageSize: this.pageSize,
shenhailong authored
313 314
                    userName: this.filters.userName,
                    realName: this.filters.realName
朱兆平 authored
315
				};
316
				this.listLoading = true;
朱兆平 authored
317
                getUserList(para).then((res) => {
朱兆平 authored
318 319 320
                    let response = res.data.data;
					this.total = response.total;
					this.users = response.list;
321
					this.listLoading = false;
朱兆平 authored
322
					//NProgress.done();
323 324
				}).catch((error) => {
                    this.listLoading = false;
朱兆平 authored
325
                    this.$message.error(error.toString());
326 327 328
				});

			},
329 330 331 332

			// 获取公司列表
			getCompanyNmae(){
				let para = {
shenhailong authored
333
					type: "公司"
334 335 336 337 338 339 340 341 342
				};
				getRoleList(para).then((res) => {
					this.options = res.data.list;
					this.listLoading = false;
					//NProgress.done();
				}).catch(() => {

				});
			},
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 368 369 370
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { userId: row.userId };
					removeUser(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getUsers();
					}).catch((error) => {
                        this.listLoading = false;
                        alert(error);
					});
				}).catch();
			},
            /**
			 * 显示编辑界面
             * @param index
             * @param row 为这行的数据对象
             */
			handleEdit: function (index, row) {
371 372 373
				this.addFormVisible = true;
				this.dialogStatus= 'update',
				this.addForm = Object.assign({}, row);
374
			},
朱兆平 authored
375
            roleEdit: function (index, row) {
376 377
                this.roleFormVisible = true;
                this.getRoles();
朱兆平 authored
378 379 380
                var _this = this;
                _this.roleIds = [];
                this.roleEditForm = Object.assign({}, row);
381 382 383 384 385 386 387
                let roles = this.roleEditForm.roles;

                if (util.checkNull(roles)){
                    roles.forEach(function (role,v_index,v_arr) {
                        if (util.checkNull(role)) {
                            _this.roleIds[v_index] = role.roleId;
                        }
朱兆平 authored
388 389
                    });
				}
390 391 392 393
                this.$nextTick(() => {
                    //反向适配
                    this.$refs.tree.setCheckedKeys(this.roleIds);
                });
朱兆平 authored
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
            },
            getRoles() {
                let para = {
                    pageNum: 1,
                    pageSize: 100
                };
                NProgress.start();
                getList(para).then((res) => {
                    this.roles = res.data.list;
                    NProgress.done();
                }).catch((error) => {
                    if(null!= error.response && error.response!==undefined){
                        let status= error.response.status;
                        let msg = error.response.statusText;
                        alert(status+msg);
                    }else {
                        alert(error);
                    }
                });
414
朱兆平 authored
415
            },
416 417 418
			//显示新增界面,每次点开初始化数据
			handleAdd: function () {
				this.addFormVisible = true;
419
				this.dialogStatus= 'create',
420 421 422
				this.addForm = {
                    username: '',
                    password: '',
423
					checkPass: '',
424 425 426 427 428
                    sex: 1,
                    address: '',
                    realname: '',
                    email:	'',
                    mobilephone: '',
429
                    age: 1,
430
					state: true
431 432 433 434
				};
			},
			//编辑
			editSubmit: function () {
435
				this.$refs.addForm.validate((valid) => {
436 437 438 439
					if (valid) {
						this.$confirm('确认提交吗?', '提示', {}).then(() => {
							this.editLoading = true;
							//NProgress.start();
440
							let para = Object.assign({}, this.addForm);
441 442 443 444
							//不需要提交的 去掉,后端不好接收
							para.authorities = null;
							para.permissions = null;
							para.roles = null;
445
							this.$delete(para,'checkPass')
446 447 448 449 450 451 452 453
//							para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
                            /*
        					查询之后格式this.filters.column.create_start_date中日期发生变化;
        					Wed Aug 09 2017 00:00:00 GMT+0800 (中国标准时间) 变成了 "2017-08-08T16:00:00.000Z";
        					所以使用 moment 日期格式化插件将时间转换成 [ Wed Aug 09 2017 00:00:00 GMT+0800 (中国标准时间) ] 格
        					式;
    						*/
							/*moment 安装 npm install moment --save*/
朱兆平 authored
454
//                            para.creattime = moment(para.creattime).format('YYYY-MM-DD HH:mm:ss');
455 456 457 458 459 460 461 462
                            this.editLoading = false;
							editUser(para).then((res) => {

								//NProgress.done();
								this.$message({
									message: '提交成功',
									type: 'success'
								});
463 464
								this.$refs['addForm'].resetFields();
								this.addFormVisible = false;
465 466 467 468 469 470 471 472 473 474 475 476 477
								this.getUsers();
							}).catch(error => alert(error));
						});
					}
				});
			},
			//新增
			addSubmit: function () {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗?', '提示', {}).then(() => {
							this.addLoading = true;
							let para = Object.assign({}, this.addForm);
478
							this.$delete(para,'checkPass')
shenhailong authored
479
//							para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
480 481 482 483 484 485 486 487 488 489
							addUser(para).then((res) => {
								this.addLoading = false;
								//NProgress.done();
								this.$message({
									message: '提交成功',
									type: 'success'
								});
								this.$refs['addForm'].resetFields();
								this.addFormVisible = false;
								this.getUsers();
朱兆平 authored
490 491
							}).catch(error => {
                                this.addLoading = false;
朱兆平 authored
492 493 494 495
								this.$message({
									message: error.message,
									type: 'error'
								});
朱兆平 authored
496
							});
497 498 499 500
						});
					}
				});
			},
501
502 503 504 505 506
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
507
				var ids = this.sels.map(item => item.userId).toString();
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
				this.$confirm('确认删除选中记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { ids: ids };
					batchRemoveUser(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getUsers();
					});
				}).catch(() => {
朱兆平 authored
525
				});
朱兆平 authored
526 527 528 529 530
			},
            roleEditSubmit: function () {
                this.$confirm('确认提交吗?', '提示', {}).then(() => {
//                    this.editLoading = true;
                    let userId = this.roleEditForm.userId;
朱兆平 authored
531
                    let roleIds = this.$refs.tree.getCheckedKeys();
朱兆平 authored
532 533 534
                    let para = {userId,roleIds};
                    setUserRole(para).then((res) => {
                        if (res.status ===200) {
535 536 537 538
                            this.$message({
                                message: '岗位设置成功',
                                type: 'success'
                            });
朱兆平 authored
539 540 541 542 543 544 545 546
						}
						this.getUsers();
                        this.roleFormVisible = false;
                    }).catch(err => {
                        alert(err);
					});
                    console.log(para);
				});
朱兆平 authored
547 548 549 550 551
            },
            handleAvatarSuccess: function(response){
                this.faceImageUrl = 'http://127.0.0.1:7003/'+response.data;
			},
            beforeAvatarUpload: function () {
朱兆平 authored
552
553 554 555 556 557 558 559 560 561
            },
            treeHandleCheckChange: function (data, checked, indeterminate) {
                if(checked) {
                    this.roleIds.push(data.roleId);
				}else {
                    this.roleIds.splice(this.roleIds.contains(data.roleId),1);
				}
//                console.log(data, checked, indeterminate);
                console.log(this.roleIds);
朱兆平 authored
562
            }
朱兆平 authored
563 564
		},
		mounted() {
565
			this.getUsers();
566
			// this.getCompanyNmae();
朱兆平 authored
567 568 569 570 571 572
		},
		computed: {
            hideShow() {//当图片多于一张的时候,就隐藏上传框
                return this.file === '' ? false : true
            }
        },
573
	}
朱兆平 authored
574 575 576

</script>
577 578 579 580 581 582 583 584 585 586 587 588
<style lang="scss">
	.user-form{
		.el-form-item__label{
			font-size: 12px;
		}
		.el-form-item__content{
			width: 260px;
			.el-textarea{
				width:100%;
			}
		}
	}
朱兆平 authored
589
590
</style>