import {Injectable} from '@angular/core'; import {JSONRequest} from '../../interface/JSONRequest'; import {HttpClient} from '@angular/common/http'; import {catchError, tap} from 'rxjs/operators'; import {JSONResponse} from '../../interface/JSONResponse'; import {HttpInterface} from '../../interface/HttpInterface'; import {Observable} from 'rxjs'; import {Result} from '../../interface/Result'; import {Router} from '@angular/router'; import {MessageService} from '../../message/message.service'; @Injectable({ providedIn: 'root' }) export class RegisterService extends JSONRequest { constructor( private http: HttpClient, private router: Router, private messageService: MessageService ) { super(); this.httpError.result = Result.FAIL; this.httpError.message = '注册失败'; } /** * * @param body 注册表单 */ register(body) { this.http.post>(HttpInterface.register, body, this.httpOptions) .pipe( catchError(this.handleError('注册', this.httpError)) ).subscribe(r => { if (r.result === Result.OK) { this.messageService.info('注册成功'); this.router.navigateByUrl('/login'); } else { this.messageService.danger('注册失败'); } }); } /** * 获取邮箱类型 */ getEmailType(): Observable { return this.http.get>(HttpInterface.getEmailType) .pipe( catchError(this.handleError('获取邮箱类型')) ); } /** * 发送验证码 */ sendEmail(sender): Observable { return this.http.post>(HttpInterface.sendCode, {email: 'sender'}) .pipe( catchError(this.handleError('发送邮件验证码')) ); } }