审查视图

src/views/nav1/user.vue 19.8 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

		<!--权限设置-->
朱兆平 authored
195
		<el-dialog title="权限设置" :visible.sync="roleFormVisible" :close-on-click-modal="false" class="roleDialog">
朱兆平 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
	export default {
223
    	name:'User',
朱兆平 authored
224
		data() {
225 226 227 228 229 230 231 232 233 234
			var validatePass = (rule, value, callback) => {
				if (value === '') {
					callback(new Error('请输入密码'));
				} else {
					if (this.checkPass !== '') {
						this.$refs.addForm.validateField('checkPass');
					}
					callback();
				}
			};
朱兆平 authored
235 236
			return {
				filters: {
shenhailong authored
237 238
                    userName: '',
					realName: ''
朱兆平 authored
239
				},
240 241 242 243
				treeDefaultProps: {
                    children: 'children',
                    label: 'roleName',
                },
244 245 246 247 248
				dialogMap: {
					update: '编辑',
					create: '新增',
				},
				dialogStatus: 'create',
249 250 251
				users: [],
				total: 0,
                pageNum: 1,
朱兆平 authored
252
                pageSize: 10,
朱兆平 authored
253
                faceImageUrl: '',
254 255 256 257 258 259 260
				listLoading: false,
				sels: [],//列表选中列
				//编辑界面是否显示
				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				addFormRules: {
                    username: [
朱兆平 authored
261 262 263 264
						{ required: true, message: '请输入姓名,用户名长度在5-11位,支持英文和数字', trigger: 'blur' },
					],
					password: [
						{ required: true, message: '请输入密码,用户名长度在6-18位,支持英文和数字和非空字符', trigger: 'blur'}
265
					],
266 267
					checkPass: [
						{ required: true, message: '请输入密码,用户名长度在6-18位,支持英文和数字和非空字符', trigger: 'blur'}
268 269
					]
				},
朱兆平 authored
270 271
				//用户角色配置
                roleFormVisible: false,
272
				//新增界面数据
273
274
				addForm: {
275 276
					checkPass:'',
					userId: 0,
277 278 279 280 281 282 283
                    username: '',
                    password: '',
                    sex: 1,
                    address: '',
                    realname: '',
                    email:	'',
                    mobilephone: '',
284
					age: 1,
285
					state: true
朱兆平 authored
286 287 288 289 290 291 292
				},
                roleEditForm: {
                    userId: 1,
					username: "",
					roles: []
				},
				roles: [],
293
                roleIds: [],
294
				options: [],
朱兆平 authored
295 296 297 298 299 300 301
			}
		},
		methods: {
			//性别显示转换
			formatSex: function (row, column) {
				return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
			},
302 303 304 305 306 307 308
            formatState: function (row, column) {
                return row.state == true ? '是' : row.state == false ? '否' : '未知';
            },
			handleCurrentChange(val) {
				this.pageNum = val;
				this.getUsers();
			},
朱兆平 authored
309
			//获取用户列表
310
			getUsers:function() {
朱兆平 authored
311
				let para = {
312 313
                    pageNum: this.pageNum,
                    pageSize: this.pageSize,
shenhailong authored
314 315
                    userName: this.filters.userName,
                    realName: this.filters.realName
朱兆平 authored
316
				};
317
				this.listLoading = true;
朱兆平 authored
318
                getUserList(para).then((res) => {
朱兆平 authored
319 320 321
                    let response = res.data.data;
					this.total = response.total;
					this.users = response.list;
322
					this.listLoading = false;
朱兆平 authored
323
					//NProgress.done();
324 325
				}).catch((error) => {
                    this.listLoading = false;
朱兆平 authored
326
                    this.$message.error(error.toString());
327 328 329
				});

			},
330 331 332 333

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

				});
			},
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 371
			//删除
			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) {
372 373 374
				this.addFormVisible = true;
				this.dialogStatus= 'update',
				this.addForm = Object.assign({}, row);
375
			},
朱兆平 authored
376
            roleEdit: function (index, row) {
377 378
                this.roleFormVisible = true;
                this.getRoles();
朱兆平 authored
379 380 381
                var _this = this;
                _this.roleIds = [];
                this.roleEditForm = Object.assign({}, row);
382 383 384 385 386 387 388
                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
389 390
                    });
				}
391 392 393 394
                this.$nextTick(() => {
                    //反向适配
                    this.$refs.tree.setCheckedKeys(this.roleIds);
                });
朱兆平 authored
395 396 397 398 399 400
            },
            getRoles() {
                let para = {
                    pageNum: 1,
                    pageSize: 100
                };
朱兆平 authored
401 402 403 404 405 406 407 408 409 410
				let ld = this.$loading({
					text:"加载中...",
					target:".roleDialog"
				})
				// 解决bug loading显示在弹窗下面!!!!
				setTimeout(() => {
					this.$nextTick(() => {
						document.querySelector(".el-loading-mask").style.zIndex = "999999";
					});
				});
朱兆平 authored
411 412 413 414 415 416 417 418 419 420
                getList(para).then((res) => {
                    this.roles = res.data.list;
                }).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);
                    }
朱兆平 authored
421 422 423
                }).finally(() =>{
					ld.close()
				});
朱兆平 authored
424
425
朱兆平 authored
426
            },
427 428 429
			//显示新增界面,每次点开初始化数据
			handleAdd: function () {
				this.addFormVisible = true;
430
				this.dialogStatus= 'create',
431 432 433
				this.addForm = {
                    username: '',
                    password: '',
434
					checkPass: '',
435 436 437 438 439
                    sex: 1,
                    address: '',
                    realname: '',
                    email:	'',
                    mobilephone: '',
440
                    age: 1,
441
					state: true
442 443 444 445
				};
			},
			//编辑
			editSubmit: function () {
446
				this.$refs.addForm.validate((valid) => {
447 448 449 450
					if (valid) {
						this.$confirm('确认提交吗?', '提示', {}).then(() => {
							this.editLoading = true;
							//NProgress.start();
451
							let para = Object.assign({}, this.addForm);
452 453 454 455
							//不需要提交的 去掉,后端不好接收
							para.authorities = null;
							para.permissions = null;
							para.roles = null;
456
							this.$delete(para,'checkPass')
457 458 459 460 461 462 463 464
//							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
465
//                            para.creattime = moment(para.creattime).format('YYYY-MM-DD HH:mm:ss');
466 467 468 469 470 471 472 473
                            this.editLoading = false;
							editUser(para).then((res) => {

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

</script>
588 589 590 591 592 593 594 595 596 597 598 599
<style lang="scss">
	.user-form{
		.el-form-item__label{
			font-size: 12px;
		}
		.el-form-item__content{
			width: 260px;
			.el-textarea{
				width:100%;
			}
		}
	}
朱兆平 authored
600
601
</style>