register.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import { Component } from '@angular/core';
  2. import { CommonModule } from '@angular/common';
  3. import { FormsModule } from '@angular/forms';
  4. import { Router, RouterModule } from '@angular/router';
  5. import{FmodeParse} from 'fmode-ng' ;
  6. interface RegisterForm {
  7. identity: string;
  8. account: string;
  9. password: string;
  10. confirmPassword: string;
  11. }
  12. interface FormErrors {
  13. identity: boolean;
  14. account: boolean;
  15. password: boolean;
  16. confirmPassword: boolean;
  17. }
  18. @Component({
  19. selector: 'app-register',
  20. imports: [CommonModule, FormsModule, RouterModule],
  21. templateUrl: './register.html',
  22. styleUrl: './register.scss'
  23. })
  24. export class Register {
  25. // 表单数据
  26. registerForm: RegisterForm = {
  27. identity: 'user',
  28. account: '',
  29. password: '',
  30. confirmPassword: ''
  31. };
  32. // 错误状态
  33. formErrors: FormErrors = {
  34. identity: false,
  35. account: false,
  36. password: false,
  37. confirmPassword: false
  38. };
  39. // 密码显示状态
  40. showPassword = false;
  41. showConfirmPassword = false;
  42. // 身份选项
  43. identityOptions = [
  44. { value: 'user', label: 'C端用户', icon: '👤' },
  45. { value: 'business', label: 'B端企业', icon: '🏢' },
  46. { value: 'government', label: 'G端政府', icon: '🏛️' }
  47. ];
  48. constructor(private router: Router) {}
  49. // 选择身份
  50. selectIdentity(identity: string) {
  51. this.registerForm.identity = identity;
  52. this.formErrors.identity = false;
  53. }
  54. // 切换密码显示
  55. togglePasswordVisibility(field: 'password' | 'confirmPassword') {
  56. if (field === 'password') {
  57. this.showPassword = !this.showPassword;
  58. } else {
  59. this.showConfirmPassword = !this.showConfirmPassword;
  60. }
  61. }
  62. // 实时验证密码
  63. validatePassword() {
  64. const passwordRegex = /^[A-Za-z0-9!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]{6,20}$/;
  65. this.formErrors.password = !passwordRegex.test(this.registerForm.password);
  66. }
  67. // 实时验证确认密码
  68. validateConfirmPassword() {
  69. this.formErrors.confirmPassword = this.registerForm.password !== this.registerForm.confirmPassword;
  70. }
  71. // 验证账户信息
  72. validateAccount() {
  73. this.formErrors.account = !this.registerForm.account.trim();
  74. }
  75. // 表单提交
  76. onSubmit() {
  77. // 重置错误状态
  78. this.formErrors = {
  79. identity: false,
  80. account: false,
  81. password: false,
  82. confirmPassword: false
  83. };
  84. let isValid = true;
  85. // 验证身份选择
  86. if (!this.registerForm.identity) {
  87. this.formErrors.identity = true;
  88. isValid = false;
  89. }
  90. // 验证账户信息
  91. if (!this.registerForm.account.trim()) {
  92. this.formErrors.account = true;
  93. isValid = false;
  94. }
  95. // 验证密码
  96. const passwordRegex = /^[A-Za-z0-9!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]{6,20}$/;
  97. if (!passwordRegex.test(this.registerForm.password)) {
  98. this.formErrors.password = true;
  99. isValid = false;
  100. }
  101. // 验证确认密码
  102. if (this.registerForm.password !== this.registerForm.confirmPassword) {
  103. this.formErrors.confirmPassword = true;
  104. isValid = false;
  105. }
  106. if (isValid) {
  107. // Mock 注册成功
  108. this.mockRegister();
  109. }
  110. }
  111. // Mock 注册处理
  112. private mockRegister() {
  113. // 模拟API调用延迟
  114. setTimeout(() => {
  115. // Mock 注册成功响应
  116. const mockResponse = {
  117. success: true,
  118. message: '注册成功!',
  119. user: {
  120. id: Math.random().toString(36).substr(2, 9),
  121. identity: this.registerForm.identity,
  122. account: this.registerForm.account,
  123. createdAt: new Date().toISOString()
  124. }
  125. };
  126. FmodeParse.User.signUp(this.registerForm.account, this.registerForm.password).then(user => {
  127. console.log('FmodeParse 注册成功:', user);
  128. console.log('Mock 注册响应:', mockResponse);
  129. // 保存用户信息到localStorage,包含注册时的身份信息
  130. const userInfo = {
  131. ...mockResponse.user,
  132. fmodeUser: user
  133. };
  134. localStorage.setItem('registeredUser', JSON.stringify(userInfo));
  135. // 显示成功消息
  136. alert('注册成功!请使用刚注册的账号登录。');
  137. // 跳转到登录页面
  138. this.router.navigate(['/auth/login']);
  139. }).catch(err => {
  140. console.log('FmodeParse 注册失败:', err);
  141. // 即使FmodeParse失败,也使用Mock数据继续流程
  142. console.log('使用Mock数据继续注册流程');
  143. // 保存用户信息到localStorage,包含注册时的身份信息
  144. localStorage.setItem('registeredUser', JSON.stringify(mockResponse.user));
  145. // 显示成功消息
  146. alert('注册成功!请使用刚注册的账号登录。');
  147. // 跳转到登录页面
  148. this.router.navigate(['/auth/login']);
  149. });
  150. }, 1000);
  151. }
  152. // 根据用户身份类型导航到对应首页
  153. private navigateToHomePage(identity: string) {
  154. switch (identity) {
  155. case 'user':
  156. // C端用户跳转到consumer首页
  157. this.router.navigate(['/consumer']);
  158. break;
  159. case 'business':
  160. // B端企业跳转到business dashboard
  161. this.router.navigate(['/business/dashboard']);
  162. break;
  163. case 'government':
  164. // G端政府跳转到government首页
  165. this.router.navigate(['/government']);
  166. break;
  167. default:
  168. // 默认跳转到登录页面
  169. this.router.navigate(['/auth/login']);
  170. break;
  171. }
  172. }
  173. // 返回上一页
  174. goBack() {
  175. this.router.navigate(['/auth/login']);
  176. }
  177. // 跳转到登录页面
  178. goToLogin() {
  179. this.router.navigate(['/auth/login']);
  180. }
  181. }