审查视图

src/views/nav1/role.vue 15.9 KB
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>
shenhailong authored
7
					<el-input v-model="filters.roleName" placeholder="岗位/角色名称"></el-input>
8 9 10 11 12 13 14 15 16 17 18
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getRoles()">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
19 20 21 22 23
		<el-table :data="roles" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;"
					default-expand-all
					row-key="roleId"
					border
					:tree-props="{children: 'children', hasChildren: 'hasChildren'}">
24 25 26 27
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
28 29
			<!--<el-table-column prop="roleId" label="ID" width="100" sortable>-->
			<!--</el-table-column>-->
shenhailong authored
30
			<el-table-column prop="roleName" label="岗位/角色名称" min-width="200" sortable>
31
			</el-table-column>
32 33 34
			<!--<el-table-column prop="departmentName" label="部门名称" min-width="200" sortable>-->
			<!--</el-table-column>-->
			<el-table-column prop="description" label="描述" min-width="200" sortable>
35
			</el-table-column>
36
			<el-table-column label="操作" min-width="260">
37
				<template slot-scope="scope">
38
					<el-button size="small" @click="setPerm(scope.$index, scope.row)">设置权限</el-button>
39 40 41 42 43 44 45 46
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</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
47
			<el-pagination layout="total, prev, pager, next" @current-change="handleCurrentChange" :page-size="5" :total="total" style="float:right;">
48 49 50 51
			</el-pagination>
		</el-col>

		<!--编辑界面-->
52
		<el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
53 54 55 56
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="ID">
					<span>{{editForm.roleId}}</span>
				</el-form-item>
57
				<el-form-item label="岗位名称">
shenhailong authored
58
					<el-input v-model="editForm.roleName" auto-complete="off" placeholder="请输入岗位/角色名称:例如:ROLE_name"></el-input>
59
				</el-form-item>
60
				<el-form-item label="岗位描述">
shenhailong authored
61
					<el-input v-model="editForm.description" auto-complete="off" placeholder="请输入岗位/角色描述:例如:用户管理员"></el-input>
62
				</el-form-item>
63 64 65 66 67 68 69 70 71
				<el-form-item label="部门名称">
					<el-select v-model="editForm.departmentId" placeholder="请选择">
						<el-option
								v-for="item in departmentNameList"
								:key="item.departmentId"
								:label="item.departmentName"
								:value="item.departmentId">
						</el-option>
					</el-select>
72 73 74 75 76 77 78 79 80
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--新增界面-->
81
		<el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
82
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
shenhailong authored
83 84
				<el-form-item label="岗位/角色名称" prop="roleName">
					<el-input v-model="addForm.roleName" auto-complete="off" placeholder="请输入岗位/角色名称:例如:ROLE_name"></el-input>
85
				</el-form-item>
shenhailong authored
86 87
				<el-form-item label="岗位/角色描述" prop="description">
					<el-input v-model="addForm.description" auto-complete="off" placeholder="请输入岗位/角色描述:例如:用户管理员"></el-input>
88
				</el-form-item>
89 90 91 92 93 94 95 96 97
				<el-form-item label="部门名称" prop="roleSign">
					<el-select v-model="addForm.departmentId" placeholder="请选择">
						<el-option
								v-for="item in departmentNameList"
								:key="item.departmentId"
								:label="item.departmentName"
								:value="item.departmentId">
						</el-option>
					</el-select>
98 99 100 101 102 103 104
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
105 106

		<!--权限设置界面-->
107
		<el-dialog title="岗位的权限设置" :visible.sync="PermFormVisible" :close-on-click-modal="false">
108 109 110 111 112 113 114 115 116 117
			<el-form :model="permForm" label-width="80px"  ref="permForm">
				<el-checkbox-group v-model="permIds" size="small">
					<el-checkbox v-for="perm in permissons" :label="perm.permissionId" :key="perm.permissionId">{{perm.name}}</el-checkbox>
				</el-checkbox-group>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="PermFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="setPermSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
118 119 120 121 122
	</section>
</template>

<script>
	import util from '../../common/js/util'
123 124
	import NProgress from 'nprogress'
	import { getList, remove, batchRemove, edit, add, updateRolePerm } from '../../api/role_api';
125
	import { getList as getDepartmentList} from '../../api/department';
126
    import { getList as permList } from '../../api/perm_api';
127 128 129 130 131 132 133
    import moment from 'moment'
	export default {
		data() {
			return {
				filters: {
                    roleName: ''
				},
134
                departmentNameList:[],
135
				roles: [],
136 137
				permissons: [],
				permIds: [],
138 139 140 141 142 143 144 145 146 147
				total: 0,
                pageNum: 1,
                pageSize: 5,
				listLoading: false,
				sels: [],//列表选中列
				//编辑界面是否显示
				editFormVisible: false,
				editLoading: false,
				editFormRules: {
                    roleName: [
shenhailong authored
148
						{ required: true, message: '请输入岗位/角色名称', trigger: 'blur' }
149 150 151 152 153 154 155
					]
				},
				//编辑界面数据
				editForm: {
                    roleId: 1,
                    description: '',
                    roleName: '',
156 157
                    roleSign: 1,
					departmentId:''
158
				},
shenhailong authored
159
160 161 162 163
                //新增界面是否显示
				addFormVisible: false,
				//设置权限界面是否显示
                PermFormVisible: false,
164 165 166
				addLoading: false,
				addFormRules: {
                    roleName: [
shenhailong authored
167
						{ required: true, message: '请输入岗位/角色名称', trigger: 'blur' }
168 169
					],
                    description: [
shenhailong authored
170
                        { required: true, message: '请输入岗位/角色描述', trigger: 'blur' }
171 172 173 174 175 176
					]
				},
				//新增界面数据
				addForm: {
                    description: '',
                    roleName: '',
177 178
                    roleSign: 1,
                    departmentId:''
179 180 181 182 183 184 185
				},
                permForm: {
                    roleId: 1,
                    description: '',
                    roleName: '',
                    roleSign: 1,
                    permissions: []
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
				}

			}
		},
		methods: {
			//性别显示转换
			formatSex: function (row, column) {
				return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
			},
            formatState: function (row, column) {
                return row.state == true ? '是' : row.state == false ? '否' : '未知';
            },
			handleCurrentChange(val) {
				this.pageNum = val;
				this.getRoles();
			},
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
			getRoles() {
				let para = {
                    pageNum: this.pageNum,
                    pageSize: this.pageSize,
                    roleName: this.filters.roleName
				};
				this.listLoading = true;
				//NProgress.start();
                getList(para).then((res) => {
					this.total = res.data.total;
					this.roles = res.data.list;
					this.listLoading = false;
					//NProgress.done();
				}).catch((error) => {

                    this.listLoading = false;
                    if(null!= error.response && error.response!==undefined){
                        let status= error.response.status;
                        let msg = error.response.statusText;
                        alert(status+msg);
					}else {
                        alert(error);
					}

				});

			},
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


            //获取部门列表
            getdepartmentNames() {

                this.listLoading = true;
                //NProgress.start();
                getDepartmentList().then((res) => {
                    this.total = res.data.total;
                    this.departmentNameList = res.data.list;
                    this.listLoading = false;
                    //NProgress.done();
                }).catch((error) => {

                    this.listLoading = false;
                    if(null!= error.response && error.response!==undefined){
                        let status= error.response.status;
                        let msg = error.response.statusText;
                        alert(status+msg);
                    }else {
                        alert(error);
                    }

                });

            },
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
			//获取权限列表
            getPermList() {
                let para = {
                    pageNum: 1,
                    pageSize: 200
                };
                NProgress.start();
                permList(para).then((res) => {
                    this.permissons = 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);
                    }
                });

            },
278 279 280 281 282 283 284
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
285
					let para = { roleId: row.roleId };
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
                    remove(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getRoles();
					}).catch((error) => {
                        this.listLoading = false;
                        alert(error);
					});
				}).catch();
			},
            /**
			 * 显示编辑界面
             * @param index
             * @param row 为这行的数据对象
             */
			handleEdit: function (index, row) {
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
308
				this.getdepartmentNames();
309
			},
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
            setPerm: function (index, row) {
				var _this = this;
				this.permIds = [];
                this.PermFormVisible = true;
                this.permForm = Object.assign({}, row);
				let rolePerms = this.permForm.permissions;
                if (util.checkNull(rolePerms)){
                    rolePerms.forEach(function (perm,v_index,v_arr) {
                        if(util.checkNull(perm)){
                            _this.permIds[v_index] = perm.permissionId;
						}
                    });
                }
                this.getPermList();
            },
325 326 327 328 329 330 331 332 333 334 335 336 337
			//显示新增界面,每次点开初始化数据
			handleAdd: function () {
				this.addFormVisible = true;
				this.addForm = {
                    username: '',
                    password: '',
                    sex: 1,
                    address: '',
                    realname: '',
                    email:	'',
                    mobilephone: '',
                    age: 1
				};
338
				this.getdepartmentNames();
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 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗?', '提示', {}).then(() => {
							this.editLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.editForm);
							//不需要提交的 去掉,后端不好接收
							para.authorities = null;
							para.permissions = null;
							para.roles = null;
//							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*/
                            para.creattime = moment(para.creattime).format('YYYY-MM-DD HH:mm:ss');
                            this.editLoading = false;
                            edit(para).then((res) => {

								//NProgress.done();
								this.$message({
									message: '提交成功',
									type: 'success'
								});
								this.$refs['editForm'].resetFields();
								this.editFormVisible = false;
								this.getRoles();
							}).catch(error => alert(error));
						});
					}
				});
			},
			//新增
			addSubmit: function () {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗?', '提示', {}).then(() => {
							this.addLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.addForm);
                            add(para).then((res) => {
								this.addLoading = false;
								//NProgress.done();
								this.$message({
									message: '提交成功',
									type: 'success'
								});
								this.$refs['addForm'].resetFields();
								this.addFormVisible = false;
								this.getRoles();
							}).catch(error => alert(error));
						});
					}
				});
			},
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
			//设置角色提交
            setPermSubmit: function () {
                this.$refs.permForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗?', '提示', {}).then(() => {
                            this.addLoading = true;
                            //NProgress.start();
                            let role = Object.assign({}, this.permForm);
                            let roleId = role.roleId;
                            let permissionIds = this.permIds
							let para = {roleId,permissionIds};
                            updateRolePerm(para).then((res) => {
                                this.addLoading = false;
                                if (res.status ===200) {
                                    this.$message({
                                        message: '提交成功',
                                        type: 'success'
                                    });
                                    this.$refs['permForm'].resetFields();
                                }
                                //NProgress.done();
                                this.PermFormVisible = false;
                                this.getRoles();
                            }).catch(error => alert(error));
                        });
                    }
                });
            },
428 429 430 431 432
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
433
				var ids = this.sels.map(item => item.roleId).toString();
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
				this.$confirm('确认删除选中记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { ids: ids };
                    batchRemove(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getRoles();
					});
				}).catch(() => {

				});
			}
		},
		mounted() {
			this.getRoles();
		}
	}

</script>

<style scoped>

</style>