login.component.ts 7.1 KB

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