Newer
Older
import {Component, EventEmitter, OnInit, Output} from '@angular/core';
import {
AbstractControl,
FormBuilder,
FormControl,
FormGroup,
Validators
} from '@angular/forms';
import {SystemService} from '../../system.service';
import {NzMessageService, NzTreeNode} from 'ng-zorro-antd';
import {LayoutService} from '../../../layouts/layout.service';
import {CommonService} from '../../../shared/common/common.service';
@Component({
selector: 'smart-role',
templateUrl: './role.component.html',
})
export class RoleComponent implements OnInit {
@Output() add = new EventEmitter<any>();
constructor(private fb: FormBuilder, private message: NzMessageService, private overAllSer: OverAllService,
private systemSer: SystemService, private layoutSer: LayoutService, private commonSer: CommonService) {
name: [null, [Validators.required,Validators.maxLength(20)]],
comment: [null, [Validators.required,Validators.maxLength(300)]],
this.initGroupList = res.data;
this.viewGroupList = res.data;
this.viewGroupList.forEach(e => {
//获取模版分组
getTempGroup() {
this.overAllSer.getEditGroups(1).subscribe(
(res) => {
this.initTempGroupList = res.data;
this.viewTempGroupList = res.data;
this.viewTempGroupList.forEach(e => {
e.checked = false;
});
}
);
}
toNode(data) {
this.nodes = data.map(res => {
return new NzTreeNode(res);
});
}
showAddModal(): void {
this.title = '新增角色';
this.addInit();
}
//新增初始化
addInit() {
let list = this.commonSer.listToTree('id', 'parentId', this.initModuleList);
list.forEach(res => {
res.title = res.name;
res.key = res.id;
res.children.forEach((child) => {
child.title = child.name;
child.key = child.id;
child.isLeaf = true;
});
});
this.toNode(list);
//编辑初始化
editInit(data) {
if (data.resources.length > 0) {
const modulrArr = data.resources.map(res => {
return res.groupId;
});
//分组回显
this.viewGroupList = this.initGroupList.map(e => {
const data = {
groupid: e.groupid,
name: e.name,
checked: false,
};
if (modulrArr.indexOf(e.groupid) > -1) {
data.checked = true;
}
return data;
});
//模版分组回显
this.viewTempGroupList = this.initTempGroupList.map(e => {
const data = {
groupid: e.groupid,
name: e.name,
checked: false,
};
if (modulrArr.indexOf(e.groupid) > -1) {
data.checked = true;
}
return data;
});
}else{
this.viewGroupList = this.initGroupList;
this.viewTempGroupList = this.initTempGroupList;
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
}
this.dataModuleList = data.modules;
if (data.modules.length > 0) {
//菜单回显
this.systemSer.getMenuBySeparation().subscribe(
(res) => {
let list = res.data;
list.forEach(e => {
e.title = e.name;
e.key = e.id;
e.isChecked = false;
if(e.url){
e.isLeaf = true;
}
if (data.modules.indexOf(e.id) > -1) {
e.isChecked = true;
}
});
list = this.commonSer.listToTree('id', 'parentId', list);
this.toNode(list);
}
);
}
}
this.validateForm.controls[i].markAsDirty();
this.validateForm.controls[i].updateValueAndValidity();
}
});
this.viewTempGroupList.forEach(e => {
if (e.checked) {
const data = {
groupId: e.groupid,
groupName: e.name
};
resourceArr.push(data);
}
(res) => {
if (res.errCode == 10000) {
this.isVisible = false;
this.add.emit();
this.message.error(res.errMsg);
}
}
);
}
//编辑
role: {
id: this.roleId,
name: this.validateForm.value.name,
};
this.systemSer.editRole(data).subscribe(
(res) => {
if (res.errCode == 10000) {
this.isVisible = false;
this.add.emit();
this.ininForm();
this.message.success('修改成功');
selectCheckTree(e,node) {
if (node.origin.isChecked){
this.dataModuleList.push(node.key);
const index = this.dataModuleList.indexOf(node.key);
this.dataModuleList.splice(index, 1);