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>();
initGroupList = []; //分组权限
viewGroupList = []; //分组权限
initModuleList = []; //菜单权限
dataModuleList = []; //菜单数据
constructor(private fb: FormBuilder, private message: NzMessageService, private overAllSer: OverAllService,
private systemSer: SystemService, private layoutSer: LayoutService, private commonSer: CommonService) {
this.validateForm = this.fb.group({
name: [null, [Validators.required]],
status: [null, [Validators.required]],
);
}
//获取分组
getGroup() {
this.overAllSer.findGroup().subscribe(
(res) => {
this.initGroupList = res.data;
this.viewGroupList = res.data;
this.viewGroupList.forEach(e => {
}
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);
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
//编辑初始化
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.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();
}
role: {
name: this.validateForm.value.name,
status: this.validateForm.value.status,
comment: this.validateForm.value.comment,
(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,
status: this.validateForm.value.status,
comment: this.validateForm.value.comment,
};
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);