nav-pc-top-menu.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import { Component, OnInit } from '@angular/core';
  2. import { CommonModule } from '@angular/common';
  3. import { Router, RouterModule } from '@angular/router';
  4. import { FormsModule } from '@angular/forms';
  5. import { CloudObject, CloudQuery, CloudUser } from '../../../lib/ncloud';
  6. @Component({
  7. selector: 'app-nav-pc-top-menu',
  8. standalone: true,
  9. imports: [CommonModule, RouterModule, FormsModule],
  10. templateUrl: './nav-pc-top-menu.html',
  11. styleUrls: ['./nav-pc-top-menu.scss']
  12. })
  13. export class NavPcTopMenu implements OnInit {
  14. user: CloudUser | null = null;
  15. userAvatar: string | null = null;
  16. userInitials: string = '';
  17. loginError: string = '';
  18. registerError: string = '';
  19. isLoading: boolean = true;
  20. showMenu: boolean = false;
  21. loginIdentifier: string = '';
  22. emailError: boolean = false;
  23. // 登录注册模态框相关
  24. showAuthModal: boolean = false;
  25. authType: 'login' | 'register' = 'login';
  26. // 登录表单
  27. loginUsername: string = '';
  28. loginPassword: string = '';
  29. rememberMe: boolean = true;
  30. isLoggingIn: boolean = false;
  31. // 注册表单
  32. registerUsername: string = '';
  33. registerEmail: string = '';
  34. registerPhone: string = '';
  35. registerPassword: string = '';
  36. registerPasswordConfirm: string = '';
  37. acceptTerms: boolean = false;
  38. isRegistering: boolean = false;
  39. constructor(private router: Router) {}
  40. async ngOnInit() {
  41. // 尝试获取当前用户
  42. try {
  43. this.user = new CloudUser();
  44. const currentUser = await this.user.current();
  45. if (currentUser && this.user.id) {
  46. this.user = currentUser;
  47. this.generateUserInitials();
  48. } else {
  49. this.user = null;
  50. }
  51. } catch (error) {
  52. console.error('获取用户信息失败:', error);
  53. this.user = null;
  54. } finally {
  55. this.isLoading = false;
  56. }
  57. }
  58. // 生成用户头像首字母
  59. generateUserInitials() {
  60. if (!this.user) return;
  61. const username = this.user.get('username') || '';
  62. if (username.length > 0) {
  63. this.userInitials = username.charAt(0).toUpperCase();
  64. }
  65. // 如果有头像URL,则设置
  66. const avatar = this.user.get('avatar');
  67. if (avatar && typeof avatar === 'string') {
  68. this.userAvatar = avatar;
  69. }
  70. }
  71. toggleMenu() {
  72. this.showMenu = !this.showMenu;
  73. }
  74. openAuthModal(type: 'login' | 'register') {
  75. this.authType = type;
  76. this.showAuthModal = true;
  77. this.showMenu = false;
  78. }
  79. closeAuthModal() {
  80. this.showAuthModal = false;
  81. this.isLoggingIn = false;
  82. this.isRegistering = false;
  83. }
  84. setAuthType(type: 'login' | 'register') {
  85. this.authType = type;
  86. }
  87. async login() {
  88. if (!this.loginIdentifier || !this.loginPassword) {
  89. this.loginError = '请输入用户名/手机号/邮箱和密码';
  90. return;
  91. }
  92. this.isLoggingIn = true;
  93. this.loginError = '';
  94. try {
  95. const user = new CloudUser();
  96. const loggedIn = await user.login(
  97. this.loginIdentifier,
  98. this.loginPassword
  99. );
  100. if (loggedIn) {
  101. this.user = loggedIn;
  102. this.generateUserInitials();
  103. this.closeAuthModal();
  104. } else {
  105. this.loginError = '用户名或密码错误';
  106. }
  107. } catch (error) {
  108. console.error('登录失败:', error);
  109. this.loginError = '登录失败,请稍后重试';
  110. } finally {
  111. this.isLoggingIn = false;
  112. }
  113. }
  114. async register() {
  115. // 重置错误状态
  116. this.registerError = '';
  117. this.emailError = false;
  118. // 表单验证
  119. if (!this.registerUsername) {
  120. alert('请输入用户名');
  121. return;
  122. }
  123. if (!this.registerEmail) {
  124. alert('请输入邮箱');
  125. return;
  126. }else {
  127. const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
  128. if (!emailRegex.test(this.registerEmail)) {
  129. this.emailError = true;
  130. this.registerError = '邮箱格式不正确';
  131. return;
  132. }
  133. }
  134. if (!this.registerPassword) {
  135. alert('请输入密码');
  136. return;
  137. }
  138. if (this.registerPassword !== this.registerPasswordConfirm) {
  139. alert('两次输入的密码不一致');
  140. return;
  141. }
  142. if (!this.acceptTerms) {
  143. alert('请阅读并同意用户协议和隐私政策');
  144. return;
  145. }
  146. this.isRegistering = true;
  147. try {
  148. const additionalData = {
  149. email: this.registerEmail,
  150. phone: this.registerPhone
  151. };
  152. const user = new CloudUser();
  153. const newUser = await user.signUp(
  154. this.registerUsername,
  155. this.registerPassword,
  156. additionalData
  157. );
  158. if (newUser) {
  159. this.user = newUser;
  160. this.generateUserInitials();
  161. this.closeAuthModal();
  162. // 注册成功后自动登录
  163. this.loginIdentifier = this.registerUsername;
  164. this.loginPassword = this.registerPassword;
  165. await this.login();
  166. } else {
  167. this.registerError = '注册失败,请稍后重试';
  168. }
  169. } catch (error: any) {
  170. console.error('注册失败:', error);
  171. this.registerError = error.message || '注册失败,请稍后重试';
  172. } finally {
  173. this.isRegistering = false;
  174. }
  175. }
  176. async logout() {
  177. if (!this.user) return;
  178. try {
  179. await this.user.logout();
  180. this.user = null;
  181. this.userAvatar = null;
  182. this.userInitials = '';
  183. this.showMenu = false;
  184. this.router.navigate(['/home']);
  185. } catch (error) {
  186. console.error('退出登录失败:', error);
  187. alert('退出登录失败: ' + (error instanceof Error ? error.message : '未知错误'));
  188. }
  189. }
  190. }