tab1.page.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import { Component, OnInit,OnDestroy } from '@angular/core';
  2. import { AlertController, NavController } from '@ionic/angular';
  3. import { Router } from '@angular/router';
  4. import * as Parse from 'parse';
  5. import { interval, Subscription } from 'rxjs';
  6. @Component({
  7. selector: 'app-tab1',
  8. templateUrl: 'tab1.page.html',
  9. styleUrls: ['tab1.page.scss']
  10. })
  11. export class Tab1Page implements OnInit,OnDestroy {
  12. initial: string = '0KG';
  13. current: string = '0KG';
  14. target: string = '0KG';
  15. reward: string = '0元';
  16. username: string = ''; // 当前用户的用户名
  17. blueColor: boolean = false;
  18. private loadUserDataSubscription: Subscription | undefined;
  19. constructor(
  20. private alertController: AlertController,
  21. private router: Router,
  22. private navCtrl: NavController,
  23. ) {}
  24. ngOnInit() {
  25. this.loadUserData();
  26. //每隔一秒检测登录状况
  27. this.loadUserDataSubscription = interval(1000).subscribe(() => {
  28. this.loadUserData();
  29. console.log(this.username);
  30. });
  31. }
  32. ngOnDestroy() {
  33. if (this.loadUserDataSubscription) {
  34. this.loadUserDataSubscription.unsubscribe();
  35. }
  36. }
  37. async loadUserData() {
  38. const currentUser = Parse.User.current();
  39. if (currentUser) {
  40. this.username = currentUser.getUsername()!;
  41. this.loadData();
  42. } else {
  43. this.username = '未登录';
  44. this.loadData();
  45. }
  46. }
  47. async loadData() {
  48. try {
  49. const query = new Parse.Query('weight_status');
  50. query.equalTo('username', this.username); // 查找当前用户的数据
  51. const weightStatus = await query.first();
  52. if (weightStatus) {
  53. this.initial = weightStatus.get('initial') + 'KG' || '--KG';
  54. this.current = weightStatus.get('current') + 'KG' || '--KG';
  55. this.target = weightStatus.get('target') + 'KG' || '--KG';
  56. this.reward = weightStatus.get('reward') + '元' || '--元';
  57. }
  58. } catch (error) {
  59. console.error('Error loading data', error);
  60. }
  61. }
  62. async openEditModal() {
  63. if (this.username == '未登录') {
  64. // 如果用户未登录,则显示登录提示
  65. this.presentLoginAlert();
  66. return;
  67. }
  68. const alert = await this.alertController.create({
  69. header: '编辑体重信息',
  70. inputs: [
  71. {
  72. name: 'initial',
  73. type: 'text',
  74. placeholder: '初始',
  75. value: this.initial.replace('KG', '')
  76. },
  77. {
  78. name: 'current',
  79. type: 'text',
  80. placeholder: '当前',
  81. value: this.current.replace('KG', '')
  82. },
  83. {
  84. name: 'target',
  85. type: 'text',
  86. placeholder: '目标',
  87. value: this.target.replace('KG', '')
  88. },
  89. {
  90. name: 'reward',
  91. type: 'text',
  92. placeholder: '赏金',
  93. value: this.reward.replace('元', '')
  94. }
  95. ],
  96. buttons: [
  97. {
  98. text: '取消',
  99. role: 'cancel'
  100. },
  101. {
  102. text: '保存',
  103. handler: async (data) => {
  104. try {
  105. const query = new Parse.Query('weight_status');
  106. query.equalTo('username', this.username); // 查找当前用户的数据
  107. let weightStatus = await query.first();
  108. if (!weightStatus) {
  109. weightStatus = new Parse.Object('weight_status');
  110. }
  111. weightStatus.set('username', this.username);
  112. weightStatus.set('initial', data.initial);
  113. weightStatus.set('current', data.current);
  114. weightStatus.set('target', data.target);
  115. weightStatus.set('reward', data.reward);
  116. await weightStatus.save();
  117. this.loadData(); // 更新本地数据
  118. } catch (error) {
  119. console.error('Error updating data', error);
  120. }
  121. }
  122. }
  123. ]
  124. });
  125. await alert.present();
  126. }
  127. async presentLoginAlert() {
  128. const alert = await this.alertController.create({
  129. header: '未登录',
  130. message: '请先登录以继续操作',
  131. buttons: [
  132. {
  133. text: '取消',
  134. role: 'cancel'
  135. },
  136. {
  137. text: '去登录',
  138. handler: () => {
  139. this.navCtrl.navigateForward('/user/login');
  140. }
  141. }
  142. ]
  143. });
  144. await alert.present();
  145. }
  146. toggleButtonColor(habit: string) {
  147. this.blueColor = !this.blueColor;
  148. }
  149. navigateToTreePage() {
  150. this.router.navigate(['/tabs/tree']);
  151. }
  152. }