Files
airport-chengdu-web/src/app/pages/system-management/role-management/role-management-modify/role-management-modify.component.ts
T

336 lines
11 KiB
TypeScript

import { Component, OnInit } from '@angular/core';
import { Location } from '@angular/common';
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
import { HttpClientService } from '../../../../services/http-client.service';
import { Router, ActivatedRoute } from '@angular/router';
import { NzMessageService } from 'ng-zorro-antd';
import 'ztree';
declare var $: any;
@Component({
selector: 'app-role-management-modify',
templateUrl: './role-management-modify.component.html',
styleUrls: ['./role-management-modify.component.scss']
})
export class RoleManagementModifyComponent implements OnInit {
/* 新树形组件参数 begin*/
parentNode: any;
parentName: any;
treeSelected: any;
isTreeTop: any;
/* 新树形组件参数 bed */
validateForm: FormGroup;
visibleUsers = false; //控制抽屉显示
statusParamsDetail: any = { //获取用户详情接口的状态,0请求失败 1请求前 2请求中 3请求成功,有数据 4请求成功,无数据
status: 1,
}
statusParamsUsers: any = { //接口的状态,0请求失败 1请求前 2请求中 3请求成功,有数据 4请求成功,无数据
status: 1,
}
modifyType: any = 1; //当前页面操作类型 1 新增 2 编辑
public roleId: any = '';
public userSelecteds: any = []; //机构负责人已选用户-新增、修改时
public userData: any = []; //用户列表数据
public search: any = {
page_index: '1',
page_size: '10',
real_name: '',
}
collectionSize: any;
public permissionChecked: any = []; //角色详情的权限列表
isLoadingSave: any = false;
constructor(
private fb: FormBuilder,
private http: HttpClientService,
private route: ActivatedRoute,
private router: Router,
private location: Location,
private message: NzMessageService,
) { }
ngOnInit() {
this.validateForm = this.fb.group({
role_name: ['', [Validators.required]],
role_type: ['1', [Validators.required]], //0=系统内置(不可删除) ,1=用户定义
description: [''],
role_code: [''],
status: [''],
user: [],
permission: [],
});
this.queryTable();
//取路由参数
this.route.queryParams.subscribe(routeParams => {
this.modifyType = routeParams.operateType;
//routeParams 1新增 2修改
if (routeParams.operateType == 2) {
this.roleId = routeParams.id;
this.queryUserById();
this.queryDetail();
} else {
this.initTree();
this.statusParamsDetail.status = 3;
}
})
}
submitForm() {
let permission: any = [];
var zTree = $.fn.zTree.getZTreeObj("treeDemo");
var checkList = zTree.getCheckedNodes(true);
if (checkList.length > 0) {
for (var i = 0; i < checkList.length; i++) {
if (checkList[i].checked && checkList[i].level != 0 && checkList[i].level != 1) {
permission.push({
pid: checkList[i].id,
})
}
}
}
let users: any = [];
if (this.userSelecteds && this.userSelecteds.length > 0) {
this.userSelecteds.forEach(item => {
users.push({
uid: item.uid,
real_name: item.real_name,
mobile: item.mobile,
email: item.email,
sex: item.sex,
})
})
}
this.validateForm.patchValue({
permission: permission,
user: users,
})
this.isLoadingSave = true;
if (this.modifyType == 1) {
this.http.post('/sysapi/setting/role/add/', this.validateForm.value).subscribe((json) => {
if (json.is_success) {
this.message.success(`新增成功!`);
this.router.navigate(['app/system/roles'])
} else {
this.message.error(`新增失败!`);
}
this.isLoadingSave = false;
})
} else if (this.modifyType == 2) {
this.http.put('/sysapi/setting/role/update/' + this.roleId, this.validateForm.value).subscribe((json) => {
if (json.is_success) {
this.message.success(`修改成功!`);
this.router.navigate(['app/system/roles'])
} else {
this.message.error(`修改失败!`);
}
this.isLoadingSave = false;
})
}
}
cancel() {
this.location.back()
}
//修改时查询该角色下的所有用户
queryUserById(): void {
this.http.get('/sysapi/setting/role/users/' + this.roleId).subscribe((json) => {
if (json.is_success) {
json.body.forEach(item => {
item.checked = true;
item.disabled = false;
this.userSelecteds = [...this.userSelecteds, item];
})
} else {
}
})
}
//根据id查询角色详情
queryDetail(): void {
this.http.get('/sysapi/setting/role/detail/find', { rid: this.roleId }).subscribe((json) => {
if (json.is_success) {
if (json.body) {
this.statusParamsDetail = 3;
//数据回填
let item = json.body;
this.validateForm.patchValue({
role_name: item.role_name,
role_type: item.role_type, //0=系统内置(不可删除) ,1=用户定义
description: item.description,
role_code: item.role_code,
status: item.status,
})
this.permissionChecked = item.permission;
this.initTree();
} else {
this.statusParamsDetail = 4;
}
} else {
this.statusParamsDetail = 0;
}
})
}
//保存机构负责人
userSave() {
this.userData.forEach(item => {
if (item.checked && !item.disabled) {
// 增加数据
this.userSelecteds = [...this.userSelecteds, item];
}
})
this.visibleUsers = false;
}
//处理机构负责人选择和数据回填
userSelect() {
if (this.userData) {
this.userData.forEach(item => {
this.userSelecteds.forEach(option => {
if (item.uid == option.uid) {
item.checked = true;
item.disabled = true;
}
})
})
}
}
//删除机构负责人
delectConfirm(item) {
this.userSelecteds = this.userSelecteds.filter(function (option) {
return item.uid !== option.uid
})
}
//打开机构负责人抽屉
openUsers() {
this.visibleUsers = true;
this.queryTable();
}
//关闭机构选择页面
closeUser(): void {
this.visibleUsers = false;
}
//翻页
pageChange(event) {
this.search.page_index = event;
this.queryTable();
}
//获取用户列表
queryTable(): void {
this.statusParamsUsers.status = 1;
this.http.get('/sysapi/setting/user/list', this.search).subscribe((json) => {
if (json.is_success) {
let data = json.body.list;
this.collectionSize = json.body.total;
if (json.body.list && json.body.list.length > 0) {
this.statusParamsUsers.status = 3;
data.forEach(item => {
if (this.userSelecteds.length > 0) {
this.userSelecteds.forEach(option => {
if (item.uid == option.uid) {
item.checked = true;
item.disabled = true;
}
})
} else {
item.checked = false;
item.disabled = false;
}
})
} else {
this.statusParamsUsers.status = 4;
}
this.userData = data;
} else {
this.statusParamsUsers.status = 0;
}
})
}
//新树模型参数设置
public zNodes: any[];
setting = {
data: {
simpleData: {
enable: true
}
},
view: {
showLine: true,
dblClickExpand: false,
showIcon: true
},
check: {
autoCheckTrigger: true,
chkboxType: { "Y": "ps", "N": "ps" },
enable: true,
chkStyle: "checkbox",
},
callback: {
//节点点击事件
onClick: (event, treeId, treeNode, clickFlag) => {
var zTree = $.fn.zTree.getZTreeObj("treeDemo");
zTree.expandNode(treeNode);
this.treeSelected = treeNode;
this.parentNode = this.treeSelected.getParentNode();
if (!this.parentNode) {
this.isTreeTop = true;
} else {
this.isTreeTop = false;
}
},
//节点展开事件
onExpand: (event, treeId, treeNode) => {
}
},
};
//新树模型
initTree() {
this.http.get('/sysapi/setting/permission/menu/list').subscribe(json => {
if (json.is_success) {
if (json.body && json.body.length > 0) {
if (this.modifyType == 2 && this.permissionChecked.length > 0) {
json.body.forEach(item => {
this.foreachNode(item);
})
}
}
}
let treeList = [];
treeList.push({ //一层目录
name: "全部权限",
isParent: true,
open: true,
id: '',
children: json.body
});
this.zNodes = treeList;
$.fn.zTree.init($("#treeDemo"), this.setting, this.zNodes);
var zTree = $.fn.zTree.getZTreeObj("treeDemo");
let node = zTree.getNodes();
})
}
//递归方法处理权限树回填
foreachNode(node) {
this.setNode(node);
if (node.children && node.children.length > 0) {
node.children.forEach(item => {
this.foreachNode(item)
})
}
}
//递归方法,处理单个节点
setNode(node) {
this.permissionChecked.forEach(item => {
if (item.pid == node.id) {
node.checked = true;
}
})
}
}