import { UpdatePasswordDto, RhLoginedUserDto, RhSafeAny } from 'rh-base/model'; import { FormHelper, MsgHelper, RhStorageService } from 'rh-base/core'; import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core'; import { UntypedFormGroup, UntypedFormBuilder, Validators, UntypedFormControl } from '@angular/forms'; import { RhAacService } from '../../aac.service'; interface IUpdatePassword { /** 用户英文名称 */ UserName: string; } @Component({ selector: 'rh-update-password-snippet', templateUrl: './update-password-snippet.component.html', styleUrls: ['./update-password-snippet.component.less'] }) export class UpdatePasswordSnippetComponent implements OnInit { // ***************密码******************** updatePasswordDto!: UpdatePasswordDto | RhSafeAny; updatePasswordForm!: UntypedFormGroup; newPwd = null; isShowUpdatePasswordModal = false; updateLoading = false; /** 密码输入框是否显示明文 */ oldpasswordVisible = false; newpasswordVisible = false; newpasswordVisible2 = false; @Input() userAccountInfo: IUpdatePassword | null = null; @Output() closeSign = new EventEmitter(); user!: RhLoginedUserDto; // isAdmin = false; isCorrectPwd = false; constructor(public fb: UntypedFormBuilder, public operator: RhAacService, public localDb: RhStorageService) {} ngOnInit() { // this.user = this.localDb.getUserSession().User; // this.isAdmin = this.user.UserName === 'admin'; this.updatePasswordDto = this.initUpdatePasswordDto(); this.createUpdatePasswordForm(this.updatePasswordDto); // if (this.isAdmin) { // this.isCorrectPwd = true; // this.changePwdControlStatus(true); // } else { // this.changePwdControlStatus(false); // } this.changePwdControlStatus(false); if (this.userAccountInfo) { this.openUpdatePasswordModal(this.userAccountInfo); } } // ngOnChanges(changes: SimpleChanges) { // const { userAccountInfo } = changes; // } initUpdatePasswordDto(): UpdatePasswordDto { return UpdatePasswordDto.create(); } confirmValidator = (control: UntypedFormControl): { [s: string]: boolean } => { if (!control.value) { return { error: true, required: true }; } else if (control.value !== this.updatePasswordForm.get('NewPassword')?.value) { return { confirm: true, error: true }; } return {}; }; createUpdatePasswordForm(dto: UpdatePasswordDto) { this.updatePasswordForm = this.fb.group({ /** 用户密码 */ Password: [dto.Password, [Validators.required]], /** 用户新密码 */ NewPassword: [dto.NewPassword, [Validators.required]], /** 用户新密码 */ NewPassword2: [null, [this.confirmValidator]] }); } submitUpdatePasswordForm($event: Event) { if ($event) { $event.preventDefault(); } FormHelper.YGSubmitForm(this.updatePasswordDto, this.updatePasswordForm, (dto) => { this.updatePassword(dto as UpdatePasswordDto); }); } resetUpdatePasswordForm($event: Event | null) { if ($event) { $event.preventDefault(); } this.updatePasswordDto = this.initUpdatePasswordDto(); this.updatePasswordForm.reset(this.updatePasswordDto); } checkUserPassword() { // 验证密码 this.updatePasswordDto.Password = this.updatePasswordForm.get('Password')?.value; this.operator.CheckUserPassword(this.updatePasswordDto).subscribe((result) => { if (result.Success) { this.isCorrectPwd = true; this.changePwdControlStatus(true); } else { this.isCorrectPwd = false; this.changePwdControlStatus(false); // MsgHelper.ShowErrorMessage(`用户名与密码不匹配!${result.Message}`); } }); } updatePassword(dto: UpdatePasswordDto) { // 更新密码 this.updateLoading = true; this.operator.UpdatePassword(dto).subscribe( (result) => { if (result.Success) { MsgHelper.ShowSuccessMessage('修改成功!'); this.closeUpdatePasswordModal(); } else { MsgHelper.ShowWarningMessage(`修改失败!${result.Message}`); } this.updateLoading = false; }, () => { this.updateLoading = false; } ); } openUpdatePasswordModal(item: IUpdatePassword) { this.updatePasswordDto.UserId = item.UserName; // 模型属性匹配吗? if (this.updatePasswordForm) { this.updatePasswordForm.patchValue(this.updatePasswordDto); } this.isShowUpdatePasswordModal = true; } closeUpdatePasswordModal() { this.isShowUpdatePasswordModal = false; this.oldpasswordVisible = this.newpasswordVisible = this.newpasswordVisible2 = false; // this.isAdmin = false; this.isCorrectPwd = false; this.resetUpdatePasswordForm(null); this.closeSign.emit(true); this.changePwdControlStatus(false); } /** 是否启用新密码和重复新密码输入框 */ private changePwdControlStatus(status: boolean) { if (status) { this.updatePasswordForm.get('NewPassword')?.enable(); this.updatePasswordForm.get('NewPassword2')?.enable(); } else { this.updatePasswordForm.get('NewPassword')?.disable(); this.updatePasswordForm.get('NewPassword2')?.disable(); } } }