tab4.page.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. import { Component, NgZone } from '@angular/core';
  2. import {
  3. IonHeader, IonToolbar, IonTitle, IonContent,
  4. IonCard, IonCardHeader, IonCardTitle, IonCardContent,
  5. IonChip, IonIcon, IonLabel, IonList, IonItem,
  6. IonRow, IonCol, IonModal, IonButton, IonInput,
  7. IonGrid, IonButtons, IonBackButton, IonAvatar
  8. } from '@ionic/angular/standalone';
  9. import { addIcons } from 'ionicons';
  10. import {
  11. timeOutline, calendarOutline, trophyOutline,
  12. documentTextOutline, heartOutline, schoolOutline,
  13. medalOutline, nutritionOutline, walletOutline,
  14. giftOutline, peopleOutline, helpCircleOutline,
  15. chevronForwardOutline, logInOutline, personAddOutline,
  16. logOutOutline, closeOutline, checkmarkOutline, warningOutline } from 'ionicons/icons';
  17. import { CommonModule } from '@angular/common';
  18. import { FormsModule } from '@angular/forms';
  19. import { CloudUser, CloudObject } from '../../lib/ncloud';
  20. import { Capacitor } from '@capacitor/core';
  21. @Component({
  22. selector: 'app-tab4',
  23. templateUrl: 'tab4.page.html',
  24. styleUrls: ['tab4.page.scss'],
  25. standalone: true,
  26. imports: [
  27. CommonModule,
  28. FormsModule,
  29. IonHeader, IonToolbar, IonTitle, IonContent,
  30. IonCard, IonCardHeader, IonCardTitle, IonCardContent,
  31. IonChip, IonIcon, IonLabel, IonList, IonItem,
  32. IonRow, IonCol, IonModal, IonButton, IonInput,
  33. IonGrid, IonButtons, IonBackButton, IonAvatar
  34. ],
  35. })
  36. export class Tab4Page {
  37. weeklyBars = [30, 60, 90, 40, 70, 50, 80];
  38. totalBars = [20, 40, 60, 80, 100, 50, 70];
  39. // 用户状态
  40. isLoggedIn = false;
  41. currentUser: any = {
  42. name: '运动达人',
  43. email: '',
  44. stats: {
  45. following: 128,
  46. followers: 356,
  47. cheers: 1024,
  48. hours: 90,
  49. days: 120,
  50. medals: 5
  51. }
  52. };
  53. // 登录/注册表单
  54. loginForm = {
  55. email: '',
  56. password: ''
  57. };
  58. registerForm = {
  59. name: '',
  60. email: '',
  61. password: '',
  62. confirmPassword: ''
  63. };
  64. // 模态框状态
  65. showLoginModal = false;
  66. showRegisterModal = false;
  67. authError = '';
  68. // 用户服务
  69. userService = new CloudUser();
  70. constructor(private ngZone: NgZone) {
  71. addIcons({
  72. logOutOutline,logInOutline,timeOutline,calendarOutline,trophyOutline,
  73. personAddOutline,documentTextOutline,chevronForwardOutline,heartOutline,
  74. schoolOutline,medalOutline,nutritionOutline,walletOutline,giftOutline,
  75. peopleOutline,helpCircleOutline,closeOutline,warningOutline,checkmarkOutline
  76. });
  77. // 检查登录状态
  78. this.checkLoginStatus();
  79. }
  80. // 检查登录状态
  81. async checkLoginStatus() {
  82. // 检查是否在移动设备上运行
  83. const isMobile = Capacitor.isNativePlatform();
  84. // 尝试获取当前用户
  85. const currentUser = await this.userService.current();
  86. if (this.userService.id) {
  87. this.isLoggedIn = true;
  88. this.updateUserProfile();
  89. } else if (isMobile && currentUser) {
  90. // 在移动设备上需要额外处理当前用户状态
  91. this.ngZone.run(() => {
  92. this.isLoggedIn = true;
  93. this.updateUserProfile();
  94. });
  95. } else {
  96. this.isLoggedIn = false;
  97. }
  98. }
  99. // 更新用户资料
  100. updateUserProfile() {
  101. this.currentUser = {
  102. name: this.userService.get('name') || '健身爱好者',
  103. email: this.userService.get('email') || '未设置邮箱',
  104. stats: {
  105. following: this.userService.get('following') || 0,
  106. followers: this.userService.get('followers') || 0,
  107. cheers: this.userService.get('cheers') || 0,
  108. hours: this.userService.get('totalHours') || 0,
  109. days: this.userService.get('activeDays') || 0,
  110. medals: this.userService.get('medals') || 0
  111. }
  112. };
  113. }
  114. // 打开登录模态框
  115. openLogin() {
  116. this.showLoginModal = true;
  117. this.authError = '';
  118. this.loginForm = { email: '', password: '' };
  119. }
  120. // 打开注册模态框
  121. openRegister() {
  122. this.showRegisterModal = true;
  123. this.authError = '';
  124. this.registerForm = {
  125. name: '',
  126. email: '',
  127. password: '',
  128. confirmPassword: ''
  129. };
  130. }
  131. // 关闭模态框
  132. closeModals() {
  133. this.showLoginModal = false;
  134. this.showRegisterModal = false;
  135. }
  136. // 处理登录
  137. async handleLogin() {
  138. if (!this.loginForm.email || !this.loginForm.password) {
  139. this.authError = '请输入邮箱和密码';
  140. return;
  141. }
  142. try {
  143. const user = await this.userService.login(
  144. this.loginForm.email,
  145. this.loginForm.password
  146. );
  147. if (user) {
  148. this.isLoggedIn = true;
  149. this.updateUserProfile();
  150. this.closeModals();
  151. } else {
  152. this.authError = '登录失败,请检查邮箱和密码';
  153. }
  154. } catch (error: any) {
  155. this.authError = '登录过程中发生错误';
  156. if (error.message) {
  157. this.authError = error.message;
  158. }
  159. console.error('登录错误:', error);
  160. }
  161. }
  162. // 处理注册
  163. async handleRegister() {
  164. if (this.registerForm.password !== this.registerForm.confirmPassword) {
  165. this.authError = '两次输入的密码不一致';
  166. return;
  167. }
  168. if (!this.registerForm.name || !this.registerForm.email || !this.registerForm.password) {
  169. this.authError = '请填写所有必填字段';
  170. return;
  171. }
  172. if (this.registerForm.password.length < 6) {
  173. this.authError = '密码至少需要6位字符';
  174. return;
  175. }
  176. try {
  177. const user = await this.userService.signUp(
  178. this.registerForm.email,
  179. this.registerForm.password,
  180. {
  181. name: this.registerForm.name,
  182. email: this.registerForm.email,
  183. following: 0,
  184. followers: 0,
  185. cheers: 0,
  186. totalHours: 0,
  187. activeDays: 0,
  188. medals: 0
  189. }
  190. );
  191. if (user) {
  192. this.isLoggedIn = true;
  193. this.updateUserProfile();
  194. this.closeModals();
  195. } else {
  196. this.authError = '注册失败,请稍后再试';
  197. }
  198. } catch (error: any) {
  199. this.authError = '注册过程中发生错误';
  200. if (error.message) {
  201. this.authError = error.message;
  202. }
  203. console.error('注册错误:', error);
  204. }
  205. }
  206. // 退出登录
  207. async logout() {
  208. try {
  209. await this.userService.logout();
  210. this.isLoggedIn = false;
  211. this.currentUser = {
  212. name: '运动达人',
  213. email: '',
  214. stats: {
  215. following: 128,
  216. followers: 356,
  217. cheers: 1024,
  218. hours: 90,
  219. days: 120,
  220. medals: 5
  221. }
  222. };
  223. } catch (error) {
  224. console.error('登出错误:', error);
  225. }
  226. }
  227. }