import { Component, OnInit, TemplateRef, ViewContainerRef } from '@angular/core'; import { FormBuilder, FormGroup, Validators } from '@angular/forms'; import { NzFormatEmitEvent, NzTreeComponent, NzTreeNodeOptions } from 'ng-zorro-antd/tree'; import { NzModalService } from 'ng-zorro-antd/modal'; import { AdduserComponent } from './adduser/adduser.component'; import { NzMessageService } from 'ng-zorro-antd/message'; import { HttpClient } from '@angular/common/http'; import { EdituserComponent } from './edituser/edituser.component'; import { TreeService } from 'src/app/service/tree.service'; @Component({ selector: 'app-user', templateUrl: './user.component.html', styleUrls: ['./user.component.scss'] }) export class UserComponent implements OnInit { constructor(private fb: FormBuilder, private modal: NzModalService, private viewContainerRef: ViewContainerRef, private message: NzMessageService, private http: HttpClient, private toTree: TreeService) { } searchForm = { Username: '', RoleName: '', name: '', or: '' } loading userList = [] ngOnInit(): void { this.getAllOrganization() this.getAllRoles() } //获取角色列表 roleList getAllRoles() { let params = { PageNumber: 1, PageSize: 100 } this.http.get('/api/Roles', { params: params }).subscribe((data: any) => { this.roleList = data.items }) } pageChange($event) { this.PageNumber = $event this.getAllUsers() } //获取用户列表 isLoading = false totalCount PageNumber = 1 PageSize = 16 async getAllUsers() { this.isLoading = true let params = { Username: this.searchForm.Username, RoleName: this.searchForm.RoleName, Name: this.searchForm.name, OrganizationId: this.searchForm.or, PageNumber: this.PageNumber, PageSize: this.PageSize, ContainsChildren: 'true' } await new Promise((resolve, reject) => { this.http.get('/api/Users', { params: params }).subscribe((data: any) => { this.isLoading = false this.userList = data.items this.totalCount = data.totalCount console.log('用户列表', data) resolve(data) }) }) } search() { this.PageNumber = 1 this.getAllUsers() } reset() { this.PageNumber = 1 this.searchForm = { Username: '', RoleName: '', name: '', or: JSON.parse(sessionStorage.getItem('userData')).organizationId } this.getAllUsers() } expandKeys defaultOrId: string //获取所有组织机构 nodes: any = [] getAllOrganization() { let organizationId = JSON.parse(sessionStorage.getItem('userData')).organizationId let params = { ContainsChildren: "true", PageNumber: 1, PageSize: 9999, code: '0000' } this.http.get('/api/Organizations', { params: params }).subscribe((data: any) => { console.log(data.items) data.items.forEach(element => { if (element.id == organizationId) { element.parentId = null } element.key = element.id element.title = element.name if (element.level == 'squadron') { element.isLeaf = true } }); this.nodes = [...this.toTree.toTree(data.items)] this.searchForm.or = JSON.parse(sessionStorage.getItem('userData')).organizationId this.getAllUsers() }) } //新增用户 addUser(): void { if (this.nodes.length == 0) { this.message.create('warning', '请组织机构加载完毕后重试'); return } const modal = this.modal.create({ nzTitle: '新增用户', nzContent: AdduserComponent, nzViewContainerRef: this.viewContainerRef, nzWidth: 485, nzMaskClosable: false, nzComponentParams: { nodes: this.nodes, roleList: this.roleList }, nzOnOk: async () => { if (instance.validateForm.valid) { await new Promise((resolve, reject) => { let body = { username: instance.validateForm.value.account, name: instance.validateForm.value.name, email: instance.validateForm.value.email, organizationId: instance.validateForm.value.organization, roleIds: instance.validateForm.value.role, posts: instance.validateForm.value.posts, } this.http.post('/api/Users', body).subscribe({ next: async (data) => { this.message.create('success', '创建成功'); resolve(data) await this.getAllUsers() return true }, error: (err) => { this.isLoading = false this.message.create('warning', '创建失败'); reject(err) return false } }) }) } else { this.message.create('warning', '请填写完整!'); return false } } }); const instance = modal.getContentComponent(); modal.afterOpen.subscribe(() => console.log('[afterOpen] emitted!')); modal.afterClose.subscribe(result => console.log('[afterClose] The result is:', result)); } //编辑用户 editUser(item): void { if (this.nodes.length == 0) { this.message.create('warning', '请组织机构加载完毕后重试'); return } const modal = this.modal.create({ nzTitle: '编辑用户', nzContent: EdituserComponent, nzViewContainerRef: this.viewContainerRef, nzWidth: 485, nzMaskClosable: false, nzComponentParams: { nodes: this.nodes, data: JSON.parse(JSON.stringify(item)), roleList: this.roleList }, nzOnOk: async () => { if (instance.validateForm.valid) { await new Promise(resolve => { let body = { username: instance.validateForm.value.account, name: instance.validateForm.value.name, email: instance.validateForm.value.email, organizationId: instance.validateForm.value.organization, roleIds: instance.validateForm.value.role, posts: instance.validateForm.value.posts, } this.http.patch(`/api/Users/${item.id}`, body).subscribe({ next: async (data) => { this.message.create('success', '创建成功'); resolve(data) await this.getAllUsers() return true }, error: (err) => { this.isLoading = false this.message.create('warning', '创建失败'); return true } }) }) } else { this.message.create('warning', '请填写完整!'); return false } } }); const instance = modal.getContentComponent(); } //重置密码 resetPassword(item) { this.modal.confirm({ nzTitle: `确定要重置${item.name}这个账户的密码吗?`, nzOkText: '确定', nzOkType: 'primary', nzOnOk: () => { this.http.patch(`/api/Users/${item.id}/Password`, null).subscribe(data => { this.message.create('success', '重置成功!'); }) }, nzCancelText: '取消', nzOnCancel: () => { } }); } //禁用 cancel(item, type) { this.isLoading = true let body = { enabled: type } this.http.patch(`/api/Users/${item.id}`, body).subscribe({ next: (data) => { this.isLoading = false item.enabled = type }, error: (err) => { this.isLoading = false } }) } delete(item) { this.modal.confirm({ nzTitle: `确定要删除${item.name}这个用户吗?`, nzOkText: '确定', nzOkType: 'default', nzOnOk: () => { this.http.delete(`/api/Users/${item.id}`).subscribe(data => { this.message.create('success', '删除成功!'); this.getAllUsers() }) }, nzCancelText: '取消' }); } export() { let params = { Username: this.searchForm.Username, RoleName: this.searchForm.RoleName, Name: this.searchForm.name, OrganizationId: this.searchForm.or, ContainsChildren: 'true', ExportToExcel: 'true' } this.http.get('/api/Users', { params: params, responseType: 'blob' as 'json' }).subscribe((data: any) => { const link = document.createElement('a'); const blob = new Blob([data], { type: 'application/vnd.ms-excel' }); link.setAttribute('href', window.URL.createObjectURL(blob)); link.setAttribute('download', '用户列表' + '.xls'); link.style.visibility = 'hidden'; document.body.appendChild(link); link.click(); document.body.removeChild(link); this.message.create('success', '导出成功!'); }) } }