import {Component, OnInit} from '@angular/core'; import {FormBuilder} from '@angular/forms'; import {Commons} from '../../commons'; import {RegisterService} from './register.service'; import {TranslateService} from '@ngx-translate/core'; import {Observable, of} from 'rxjs'; @Component({ selector: 'app-register', templateUrl: './register.component.html', styleUrls: ['./register.component.scss'] }) // 注册模块 export class RegisterComponent extends Commons implements OnInit { // 注册表单 registerForm = this.fb.group({ // 管理员名 managerName: [], // 密码 password: [], // 确认密码 confirmPassword: [], // 手机号 mobile: [], // 邮箱 email: [], // 邮箱类型 emailType: this.fb.control('-1') }); /** * 邮箱类型 */ emailType$ = this.registerService.getEmailType(); // 表单验证 validForm = { confirmPassword: { flag: false }, mobile: { flag: false } }; constructor( private fb: FormBuilder, private registerService: RegisterService, private translateService: TranslateService ) { super(); } /** * 检查单个表单值 * @param name formControlName */ checkValue(name): boolean { if (!this.validForm.hasOwnProperty(name)) { this.validForm[name] = {}; } this.validForm[name].flag = this.registerForm.value[name]; return !this.validForm[name].flag; } /** * 检查表单所有值 */ checkForm(): boolean { for (const key in this.validForm) { if (!this.validForm[key].flag) { return true; } } return this.registerForm.value.emailType === '-1'; } /** * 检查确认密码 */ checkPwd(): Observable { if (!this.registerForm.value.confirmPassword) { this.validForm.confirmPassword.flag = false; return this.translateService.get('tip.password_null'); } else if (this.registerForm.value.password !== this.registerForm.value.confirmPassword) { this.validForm.confirmPassword.flag = false; return this.translateService.get('tip.password_diff'); } else { this.validForm.confirmPassword.flag = true; return of(''); } } /** * 检查手机 */ checkMobile(): Observable { if (!this.registerForm.value.mobile) { this.validForm.mobile.flag = false; return this.translateService.get('tip.mobile_null'); } else if (!/^1[3456789]\d{9}$/.test(this.registerForm.value.mobile)) { this.validForm.mobile.flag = false; return this.translateService.get('tip.mobile_error'); } else { this.validForm.mobile.flag = true; return of(''); } } ngOnInit(): void { } register() { this.registerService.register(this.registerForm.value); } }