import { Component, OnInit, TemplateRef, ViewContainerRef } from '@angular/core'; import { FormBuilder, FormGroup, Validators } from '@angular/forms'; 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'; @Component({ selector: 'app-user', templateUrl: './user.component.html', styleUrls: ['./user.component.scss'] }) export class UserComponent implements OnInit { validateForm!: FormGroup; constructor(private fb: FormBuilder, private modal: NzModalService, private viewContainerRef: ViewContainerRef, private message: NzMessageService, private http: HttpClient) { } ngOnInit(): void { this.validateForm = this.fb.group({ search: [null] }); this.getAllUsers() } //获取所有用户 usersLIst: any = [] usersNum: string getAllUsers() { let params = { SkipCount: String(this.SkipCount), MaxResultCount: String(this.MaxResultCount) } this.http.get('/api/services/app/User/GetAll', { params: params }).subscribe((data: any) => { this.usersLIst = data.result.items this.usersNum = data.result.totalCount console.log('所有用户', this.usersLIst) }) } SkipCount: number = 0 //0 16 32 48 MaxResultCount: number = 16 pageChange($event) { this.SkipCount = ($event - 1) * this.MaxResultCount this.getAllUsers() } //搜索框提交 submitForm(): void { console.log(12345) for (const i in this.validateForm.controls) { this.validateForm.controls[i].markAsDirty(); this.validateForm.controls[i].updateValueAndValidity(); } } //新增用户 addUser(): void { const modal = this.modal.create({ nzTitle: '新增用户', nzContent: AdduserComponent, nzViewContainerRef: this.viewContainerRef, nzWidth: 288, nzComponentParams: { title: '', subtitle: '' }, nzOnOk: async () => { console.log('hhhhhhh', instance.validateForm) if (instance.validateForm.valid) { await new Promise(resolve => { let body = { userName: instance.validateForm.value.account, name: instance.validateForm.value.name, organizationId: Number(instance.validateForm.value.organization), roleNames: instance.validateForm.value.role, phoneNumber: instance.validateForm.value.phonenum, isActive: true } this.http.post('/api/services/app/User/Create', body).subscribe(data => { resolve(data) this.message.create('success', '创建成功!'); this.SkipCount = 0 this.getAllUsers() return true }, err => { resolve(err) this.message.create('warning', '创建失败'); return false }) }) } else { this.message.create('warning', '请填写完整!'); return false } } }); const instance = modal.getContentComponent(); modal.afterOpen.subscribe(() => console.log('[afterOpen] emitted!')); // Return a result when closed modal.afterClose.subscribe(result => console.log('[afterClose] The result is:', result)); } //编辑用户 editUser(data): void { const modal = this.modal.create({ nzTitle: '编辑用户', nzContent: EdituserComponent, nzViewContainerRef: this.viewContainerRef, nzWidth: 288, nzComponentParams: { data: data, }, nzOnOk: async () => { console.log('hhhhhhh', instance.validateForm) if (instance.validateForm.valid) { await new Promise(resolve => { let body = { userName: instance.validateForm.value.account, name: instance.validateForm.value.name, organizationId: instance.validateForm.value.organization, roleNames: instance.validateForm.value.role, phoneNumber: instance.validateForm.value.phonenum, isActive: true } this.http.post('/api/services/app/User/Create', body).subscribe(data => { resolve(data) this.message.create('success', '创建成功!'); this.getAllUsers() return true }, err => { resolve(err) this.message.create('warning', '创建失败'); return false }) }) } else { this.message.create('warning', '请填写完整!'); return false } } }); const instance = modal.getContentComponent(); } //重置密码 resetPassword(item) { console.log(item) this.modal.confirm({ nzTitle: `确定要重置${item.userName}这个账户的密码吗?`, nzOkText: '确定', nzOkType: 'primary', nzOnOk: () => { this.http.post('/api/services/app/User/ResetPassword', { params: { userId: item.id } }).subscribe(data => { this.message.create('success', '重置成功!'); this.getAllUsers() }) }, nzCancelText: '取消', nzOnCancel: () => { } }); } //删除 delete(item) { this.modal.confirm({ nzTitle: `确定要删除${item.userName}这个账户吗?`, nzOkText: '确定', nzOkType: 'danger', nzOnOk: () => { this.http.delete('/api/services/app/User/Delete', { params: { Id: item.id } }).subscribe(data => { this.message.create('success', '删除成功!'); this.getAllUsers() }) }, nzCancelText: '取消', nzOnCancel: () => { } }); } }