import { RhStorageService, FormHelper, MsgHelper } from 'rh-base/core'; import { RhAacService } from './../../aac.service'; import { ResetPasswordDto } from './../../aac-model'; import { UntypedFormGroup, UntypedFormBuilder, UntypedFormControl, Validators } from '@angular/forms'; import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core'; import { UserAccountInfo, UpdatePasswordDto } from 'rh-base/model'; @Component({ selector: 'rh-reset-password-snippet', templateUrl: './reset-password-snippet.component.html', styleUrls: ['./reset-password-snippet.component.less'] }) export class ResetPasswordSnippetComponent implements OnInit { @Input() rhUser: UserAccountInfo | null = null; @Output() rhOk = new EventEmitter(); @Output() rhCancel = new EventEmitter(); // ***************密码******************** resetPasswordDto!: ResetPasswordDto; resetPasswordForm!: UntypedFormGroup; newPwd = null; resetLoading = false; /** 密码输入框是否显示明文 */ oldpasswordVisible = false; newpasswordVisible = true; // isAdmin = false; isCorrectPwd = false; readonly adminUserName = 'admin'; readonly defaultResetPwd = '123456'; constructor(public fb: UntypedFormBuilder, public operator: RhAacService, public storageSer: RhStorageService) {} ngOnInit() { this.resetPasswordDto = this.initResetPasswordDto(); this.createResetPasswordForm(this.resetPasswordDto); this.changePwdControlStatus(false); } initResetPasswordDto(): ResetPasswordDto { const temp = ResetPasswordDto.create(); temp.AdminUserId = 'admin'; temp.UserId = this.rhUser?.UserName as string; return temp; } confirmValidator = (control: UntypedFormControl): { [s: string]: boolean } => { if (!control.value) { return { error: true, required: true }; } else if (control.value !== this.resetPasswordForm.get('NewPassword')?.value) { return { confirm: true, error: true }; } return {}; }; createResetPasswordForm(dto: ResetPasswordDto) { this.resetPasswordForm = this.fb.group({ /** 用户密码 */ AdminUserId: [dto.AdminUserId, [Validators.required]], AdminPassword: [dto.AdminPassword, [Validators.required]], UserId: [dto.UserId, [Validators.required]], NewPassword: [dto.NewPassword, [Validators.required]] }); } submitResetPasswordForm($event: Event) { if ($event) { $event.preventDefault(); } FormHelper.YGSubmitForm(this.resetPasswordDto, this.resetPasswordForm, (dto) => { this.updatePassword(dto as ResetPasswordDto); }); } resetResetPasswordForm($event: Event) { if ($event) { $event.preventDefault(); } this.resetPasswordDto = this.initResetPasswordDto(); this.resetPasswordForm.reset(this.resetPasswordDto); } checkUserPassword() { // 验证密码 const adminPassword = this.resetPasswordForm.get('AdminPassword')?.value; const checkUserDto: UpdatePasswordDto = UpdatePasswordDto.create(); checkUserDto.UserId = this.adminUserName; checkUserDto.Password = adminPassword; this.operator.CheckUserPassword(checkUserDto).subscribe((result) => { if (result.Success) { this.isCorrectPwd = true; this.changePwdControlStatus(true); this.resetPasswordForm.get('NewPassword')?.patchValue('123456'); } else { this.isCorrectPwd = false; this.changePwdControlStatus(false); // MsgHelper.ShowErrorMessage(`用户名与密码不匹配!${result.Message}`); } }); } updatePassword(dto: ResetPasswordDto) { // 更新密码 this.resetLoading = true; this.operator.ResetPassword(dto).subscribe( (result) => { if (result.Success) { MsgHelper.ShowSuccessMessage('密码重置成功!'); this.rhOk.emit(); } else { MsgHelper.ShowWarningMessage(`密码重置失败!${result.Message}`); this.rhCancel.emit(); } this.resetLoading = false; }, () => { this.resetLoading = false; } ); } /** 是否启用新密码和重复新密码输入框 */ private changePwdControlStatus(status: boolean) { if (status) { this.resetPasswordForm.get('NewPassword')?.enable(); } else { this.resetPasswordForm.get('NewPassword')?.disable(); } } }