|
|
|
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<JSONResponse<any>>(HttpInterface.register, body, this.httpOptions)
|
|
|
|
.pipe(
|
|
|
|
catchError(this.handleError<any>('注册', this.httpError))
|
|
|
|
).subscribe(r => {
|
|
|
|
if (r.result === Result.OK) {
|
|
|
|
this.messageService.info('注册成功');
|
|
|
|
this.router.navigateByUrl('/login');
|
|
|
|
} else {
|
|
|
|
this.messageService.danger('注册失败');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取邮箱类型
|
|
|
|
*/
|
|
|
|
getEmailType(): Observable<any> {
|
|
|
|
return this.http.get<JSONResponse<any>>(HttpInterface.getEmailType)
|
|
|
|
.pipe(
|
|
|
|
catchError(this.handleError<any>('获取邮箱类型'))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 发送验证码
|
|
|
|
*/
|
|
|
|
sendEmail(sender): Observable<any> {
|
|
|
|
return this.http.post<JSONResponse<any>>(HttpInterface.sendCode, {email: 'sender'})
|
|
|
|
.pipe(
|
|
|
|
catchError(this.handleError<any>('发送邮件验证码'))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|