register.component.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import { Component, ViewChild,OnInit } from '@angular/core';
  2. import { ReactiveFormsModule } from '@angular/forms';
  3. import { HttpClient } from "@angular/common/http";
  4. import { Router } from '@angular/router';
  5. import { catchError } from "rxjs/operators";
  6. import {
  7. FormControl,
  8. FormGroup,
  9. NonNullableFormBuilder,
  10. Validators,
  11. ValidatorFn,
  12. AbstractControl,
  13. } from '@angular/forms';
  14. import { CommonCompModule } from '../common.modules';
  15. import { textbookServer } from '../../../services/textbook';
  16. import { AuthServr } from '../../../services/auth.service';
  17. import { CaptchaComponent } from '../../../app/captcha/captcha.component';
  18. import Parse from "parse";
  19. import { NzMessageService } from 'ng-zorro-antd/message';
  20. import { ParseAuthing } from './parse-authing';
  21. @Component({
  22. selector: 'app-register',
  23. standalone: true,
  24. imports: [ReactiveFormsModule, CommonCompModule,CaptchaComponent],
  25. templateUrl: './register.component.html',
  26. styleUrl: './register.component.scss'
  27. })
  28. export class RegisterComponent implements OnInit{
  29. @ViewChild("codelogin") codelogin: any;
  30. @ViewChild("codeloginSign") codeloginSign: any;
  31. ngOnInit(){
  32. let parseAuthing = new ParseAuthing({
  33. // 监听事件:登陆成功后,返回用户信息
  34. login:(user,authClient)=>{
  35. console.log(user,authClient)
  36. // this.authServr.profileVerify()
  37. this.router.navigate(['/user/account_info'])
  38. }
  39. });
  40. parseAuthing.initLoginModal();
  41. }
  42. code:string = '' //本地生成验证码
  43. active:number = 0
  44. validateForm: FormGroup<{
  45. userName: FormControl<string>;
  46. password: FormControl<string>;
  47. code: FormControl<string>;
  48. checked: FormControl<boolean>;
  49. }> = this.fb.group({
  50. userName: ['', [Validators.required]],
  51. password: ['', [Validators.required]],
  52. code: ['', [Validators.required]],
  53. checked: [false]
  54. });
  55. //校验手机号
  56. confirmationValidator: ValidatorFn = (control: AbstractControl): { [s: string]: boolean } => {
  57. let a = /^1[3456789]\d{9}$/;
  58. if (!control.value || !String(control.value).match(a)) {
  59. return { required: true };
  60. }
  61. return {};
  62. };
  63. validateFormPhone: FormGroup<{
  64. phoneNumber: FormControl<string>;
  65. code: FormControl<string>;
  66. checkCode:FormControl<string>;
  67. checked: FormControl<boolean>;
  68. }> = this.fb.group({
  69. phoneNumber: ['', [Validators.required, this.confirmationValidator]],
  70. code: ['', [Validators.required]],
  71. checkCode: ['', [Validators.required]],
  72. checked: [false]
  73. });
  74. constructor(
  75. public tbookSer: textbookServer,
  76. private fb: NonNullableFormBuilder,
  77. public router: Router,
  78. private authServr: AuthServr,
  79. private message: NzMessageService,
  80. private http: HttpClient,
  81. ) {
  82. Parse?.User?.logOut()
  83. }
  84. onChangeCode(e:any){
  85. let { code } = e
  86. this.code = code
  87. }
  88. submitForm(type:string): void {
  89. console.log(this.code);
  90. if(type == 'account'){//登录
  91. if (this.validateForm.valid) {
  92. let {userName, password, code, checked } = this.validateForm.value
  93. console.log(userName, password, code);
  94. if(this.code.toLowerCase() != code?.toLowerCase()){
  95. this.message.warning('验证码错误')
  96. return
  97. }else if(!checked){
  98. this.message.warning('请勾选隐私协议与服务条款')
  99. return
  100. }
  101. // this.authServr.login(userName, password, this.tbookSer.company).then(()=>{
  102. // this.codelogin.updateDrawCode();
  103. // }) .catch(err=>{
  104. // console.warn(err);
  105. // this.message.error(err?.message || '登录失败')
  106. // })
  107. } else {
  108. this.message.warning('填写信息不正确')
  109. Object.values(this.validateForm.controls).forEach((control) => {
  110. if (control.invalid) {
  111. control.markAsDirty();
  112. control.updateValueAndValidity({ onlySelf: true });
  113. }
  114. });
  115. }
  116. }else{//手机号登录/注册
  117. console.log(this.validateFormPhone.value);
  118. if (this.validateFormPhone.valid) {
  119. let {phoneNumber, code } = this.validateFormPhone.value
  120. console.log(phoneNumber, code);
  121. if(this.code.toLowerCase() != code?.toLowerCase()){
  122. this.message.warning('验证码错误')
  123. return
  124. }
  125. this.authServr.register(phoneNumber, code, this.tbookSer.company).then(()=>{
  126. this.codeloginSign.updateDrawCode();
  127. }).catch((err) => {
  128. console.warn(err);
  129. this.message.error(err?.message || '登录失败')
  130. });
  131. } else {
  132. this.message.warning('填写信息不正确')
  133. Object.values(this.validateFormPhone.controls).forEach((control) => {
  134. if (control.invalid) {
  135. control.markAsDirty();
  136. control.updateValueAndValidity({ onlySelf: true });
  137. }
  138. });
  139. }
  140. }
  141. }
  142. onChange(e: any) {
  143. console.log(e);
  144. this.active = e.index
  145. this.validateForm.reset()
  146. this.validateFormPhone.reset()
  147. this.codelogin.updateDrawCode();
  148. this.codeloginSign.updateDrawCode();
  149. }
  150. goUrl(path: string) {
  151. this.router.navigate([
  152. path,
  153. {
  154. // type: this.currentProfile.type,
  155. },
  156. ]);
  157. }
  158. buttonText = "获取验证码";
  159. //添加倒计时开始和结束的判断
  160. isCountingdown = false;
  161. /* 获取验证码 */
  162. codeDown: boolean = false;
  163. startCountdown() {
  164. let a = /^1[3456789]\d{9}$/;
  165. let { phoneNumber, code } = this.validateFormPhone.value
  166. console.log(phoneNumber);
  167. if (!String(phoneNumber).match(a)) {
  168. this.message.error("请填写正确手机号");
  169. return;
  170. }
  171. if (code?.toLowerCase() != this.code.toLowerCase()) {
  172. this.message.error("验证码不正确");
  173. return;
  174. }
  175. if (this.codeDown || this.isCountingdown) return;
  176. this.codeDown = true;
  177. let host =
  178. (Parse as any).serverURL?.split("parse")?.[0] ||
  179. "https://server.fmode.cn/";
  180. this.http
  181. .post(host + "api/apig/message", {
  182. company: this.tbookSer.company,
  183. mobile: phoneNumber,
  184. })
  185. .pipe(
  186. catchError(async (e) => {
  187. // 显示报错
  188. console.log(e);
  189. this.message.create("error", e.error.mess || "验证码获取失败");
  190. this.codeDown = false;
  191. this.isCountingdown = false;
  192. return;
  193. })
  194. )
  195. .subscribe((res: any) => {
  196. console.log(res);
  197. if(res){
  198. this.message.success("发送成功");
  199. this.isCountingdown = true;
  200. this.time();
  201. }
  202. this.codeloginSign.updateDrawCode();
  203. this.codeDown = false;
  204. });
  205. }
  206. /* 倒计时 */
  207. time() {
  208. this.isCountingdown = true;
  209. this.buttonText = `${this.authServr.countdown}秒`;
  210. const timer = setInterval(() => {
  211. this.authServr.countdown--;
  212. this.buttonText = `${this.authServr.countdown}秒`;
  213. if (this.authServr.countdown === 0) {
  214. clearInterval(timer);
  215. this.buttonText = "重新发送";
  216. this.isCountingdown = false;
  217. }
  218. }, 1000);
  219. }
  220. }