import { Component, OnInit, ViewChild, Inject } from '@angular/core'; import {HttpClient} from '@angular/common/http' import { MatDialogRef, MatDialog, MAT_DIALOG_DATA } from '@angular/material/dialog'; import { MatPaginator } from '@angular/material/paginator'; import { MatTableDataSource } from '@angular/material/table'; import { PageEvent } from '@angular/material/paginator'; import { AddEnterpriserUser } from './addenterpriseuser.component' import { MatSnackBar, MatSnackBarConfig } from '@angular/material/snack-bar'; import {FormControl} from '@angular/forms'; import { Router,ActivatedRoute } from '@angular/router' import { FlatTreeControl } from '@angular/cdk/tree'; import { MatTreeFlatDataSource, MatTreeFlattener } from '@angular/material/tree'; @Component({ selector: 'app-enterpriseuser', templateUrl: './enterpriseuser.component.html', styleUrls: ['./enterpriseuser.component.scss'] }) export class EnterpriseuserComponent implements OnInit { constructor(public http: HttpClient,public dialog: MatDialog,public snackBar: MatSnackBar,private router:Router,private route:ActivatedRoute) { } ngOnInit() { this.initData() this.getOrganizations() } allOrganizations:any; //所有组织机构 treeData:any = []; //tree型 data isShowTree:boolean = false; //树形结构是否展示 showTree () { this.isShowTree = true } hideTree () { this.isShowTree = false } //获取所有组织机构 getOrganizations () { this.http.get('/api/Organizations').subscribe((data:any)=>{ this.allOrganizations = data data.forEach(element => { element.children = [] data.forEach(item => { item.parentId === element.id? element.children.push(item) : null }); }); data.forEach(element => { !element.parentId? this.treeData.push(element) : null }); this.dataSources.data = this.treeData }) //http } organizationId:string = null; organizationName:string = null; private _transformer = (node, level: number) => { //初始化tree return { expandable: !!node.children && node.children.length > 0, name: node.name, level: level, id: node.id, parentId: node.parentId, children: node.children }; } treeControl = new FlatTreeControl(node => node.level, node => node.expandable); treeFlattener = new MatTreeFlattener(this._transformer, node => node.level, node => node.expandable, node => node.children); dataSources = new MatTreeFlatDataSource(this.treeControl, this.treeFlattener); hasChild = (_: number, node: any) => node.expandable; //选择tree节点 selectTree (e) { this.organizationId = e.id this.organizationName = e.name this.isShowTree = false } displayedColumns: string[] = [ 'name','identitycard', 'post', 'tel', 'time', 'operation',]; dataSource:any; //所有企业用户 name:any //姓名 identityCard:any //身份证 //分页 @ViewChild(MatPaginator, {static: true}) pageEvent: PageEvent; paginator: MatPaginator; length:any; //共多少条数据 pageSize:any; //每页条数 pageSizeOptions: number[] = [10] //设置每页条数 pageNumber:number = 1; //第几页 //分页切换 chagePage (e) { this.pageNumber = e.pageIndex+1 let data= { RealName: this.name || '', IdentityCard: this.identityCard || '', OrganizationId: this.organizationId || '', RoleType: '2', PageNumber: String(this.pageNumber), } this.http.get('/api/ExamUsers',{params:data}).subscribe((data:any)=>{ this.length = data.totalCount this.pageSize = data.pageSize this.dataSource = new MatTableDataSource(data.items) }) } //页面初始化 + 查询 + 重置 initData () { let data= { RealName: this.name || '', IdentityCard: this.identityCard || '', OrganizationId: this.organizationId || '', RoleType: '2', } this.http.get('/api/ExamUsers',{params:data}).subscribe((data:any)=>{ this.length = data.totalCount this.pageSize = data.pageSize this.pageEvent.pageIndex = 0 this.dataSource = new MatTableDataSource(data.items) }) } //更新当前页数据 getAllUsers () { let data= { RealName: this.name || '', IdentityCard: this.identityCard || '', OrganizationId: this.organizationId || '', RoleType: '2', PageNumber: String(this.pageNumber), } this.http.get('/api/ExamUsers',{params:data}).subscribe((data:any)=>{ this.length = data.totalCount this.pageSize = data.pageSize this.dataSource = new MatTableDataSource(data.items) }) } //清空搜索 empty () { this.name = '', this.identityCard = '', this.organizationId = '', this.organizationName = '', this.initData() } //创建用户 open(){ let data = this.treeData let dialogRef = this.dialog.open(AddEnterpriserUser,{data}); dialogRef.afterClosed().subscribe(data=>{ if (data) { const config = new MatSnackBarConfig(); config.verticalPosition = 'top'; config.duration = 3000 this.snackBar.open('创建成功!','确定',config); this.getAllUsers() } }); } //编辑企业用户 edit (e) { let data = {treeData: this.treeData, userData: e} let dialogRef = this.dialog.open(editenterpriseuser,{data}); dialogRef.afterClosed().subscribe(data=>{ if (data) { const config = new MatSnackBarConfig(); config.verticalPosition = 'top'; config.duration = 3000 this.snackBar.open('修改成功!','确定',config); this.getAllUsers() } }); } //重置密码 reset (e) { this.http.put(`/api/ExamUsers/${e.id}/ResetPassword`,{}).subscribe(data=>{ const config = new MatSnackBarConfig(); config.verticalPosition = 'top'; config.duration = 3000 this.snackBar.open('重置密码成功!','确定',config); },err=>{ const config = new MatSnackBarConfig(); config.verticalPosition = 'top'; config.duration = 3000 this.snackBar.open('重置密码失败!','确定',config); }) } //启用 enabled (e) { let body = { creationTime: new Date(), enabled: true, id: e.id, identityCard: e.identityCard, name : e.name, organizationId: e.organizationId, organizationName: e.organizationName, phone: e.phone, realName : e.realName, roleType : e.roleType, } this.http.put(`/api/ExamUsers/${e.id}`,body).subscribe(data => { const config = new MatSnackBarConfig(); config.verticalPosition = 'top'; config.duration = 3000; this.snackBar.open('启用成功!','确定',config); this.getAllUsers(); }) } //禁用 noEnabled (e) { let body = { creationTime: new Date(), enabled: false, id: e.id, identityCard: e.identityCard, name : e.name, organizationId: e.organizationId, organizationName: e.organizationName, phone: e.phone, realName : e.realName, roleType : e.roleType, } this.http.put(`/api/ExamUsers/${e.id}`,body).subscribe(data => { const config = new MatSnackBarConfig(); config.verticalPosition = 'top'; config.duration = 3000; this.snackBar.open('禁用成功!','确定',config); this.getAllUsers(); }) } //删除 delete (e) { let isTrue = confirm('您确定要删除吗') if (isTrue) { this.http.delete(`/api/ExamUsers/${e.id}`).subscribe(data=>{ const config = new MatSnackBarConfig(); config.verticalPosition = 'top'; config.duration = 3000 this.snackBar.open('删除成功!','确定',config); this.getAllUsers() }) } } } //编辑企业用户 @Component({ selector: 'app-editenterpriseuser', templateUrl: './editenterpriseuser.html', styleUrls: ['./enterpriseuser.component.scss'] }) export class editenterpriseuser { constructor(private http: HttpClient,public dialogRef: MatDialogRef,@Inject(MAT_DIALOG_DATA) public data,public snackBar: MatSnackBar,) {} ngOnInit(): void { this.dataSource.data = this.data.treeData this.realName = JSON.parse(JSON.stringify(this.data.userData.realName)) this.identityCard = JSON.parse(JSON.stringify(this.data.userData.identityCard)) this.phone = JSON.parse(JSON.stringify(this.data.userData.phone)) this.organizationId = JSON.parse(JSON.stringify(this.data.userData.organizationId)) this.organizationName = JSON.parse(JSON.stringify(this.data.userData.organizationName)) } errmsg:string = null; //捕获错误信息 realName:string = null; identityCard:string = null; phone:number = null; organizationId:string = null; organizationName:string = null; private _transformer = (node, level: number) => { //初始化tree return { expandable: !!node.children && node.children.length > 0, name: node.name, level: level, id: node.id, parentId: node.parentId, children: node.children }; } treeControl = new FlatTreeControl(node => node.level, node => node.expandable); treeFlattener = new MatTreeFlattener(this._transformer, node => node.level, node => node.expandable, node => node.children); dataSource = new MatTreeFlatDataSource(this.treeControl, this.treeFlattener); hasChild = (_: number, node: any) => node.expandable; //选择tree节点 selectTree (e) { this.organizationId = e.id this.organizationName = e.name } //提交创建表单 onSubmit (e) { console.log(666,e) if (this.organizationId && this.organizationName) { e.phone = String(e.phone) e.roleType = 2 // e.name = this.data.userData.name e.id = this.data.userData.id e.enabled = this.data.userData.enabled // e.creationTime = new Date() e.organizationId = this.organizationId e.organizationName = this.organizationName this.http.put(`/api/ExamUsers/${this.data.userData.id}`,e).subscribe(data => { this.dialogRef.close('success'); },err => { const config = new MatSnackBarConfig(); config.verticalPosition = 'top'; config.duration = 3000 this.snackBar.open(err,'确定',config); }) } else { const config = new MatSnackBarConfig(); config.verticalPosition = 'top'; config.duration = 3000 this.snackBar.open('请选择消防救援站','确定',config); } } } //查看企业用户 @Component({ selector: 'app-seeenterpriseuser', templateUrl: './seeenterpriseuser.html', styleUrls: ['./enterpriseuser.component.scss'] }) export class seeenterpriseuser { constructor(public http: HttpClient,public dialog: MatDialog, @Inject(MAT_DIALOG_DATA) public data) { } ngOnInit() {} }