tab3.page.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { Component, OnInit } from '@angular/core';
  2. import { NavController } from '@ionic/angular';
  3. import { UserService } from '../services/user.service';
  4. import { Subscription } from 'rxjs';
  5. @Component({
  6. selector: 'app-tab3',
  7. templateUrl: 'tab3.page.html',
  8. styleUrls: ['tab3.page.scss']
  9. })
  10. export class Tab3Page implements OnInit {
  11. username: string = '';
  12. userAvatar: string | null = 'src/assets/images/user.png'; // 允许 null
  13. userType: string = '普通用户';
  14. previewImage: string | null = null;
  15. private userInfoSubscription?: Subscription;
  16. // 状态变量
  17. hasUnreadHealthRecords = false; // 是否有未读健康记录
  18. hasPendingAppointments = false; // 是否有待处理预约
  19. hasNewPromotions = false; // 是否有新的优惠活动
  20. hasUnprocessedOrders = false; // 是否有待处理订单
  21. constructor(private navCtrl: NavController, private userService: UserService) {}
  22. ngOnInit() {
  23. this.loadUserInfo();
  24. this.checkStatuses(); // 初始化时检查状态
  25. }
  26. ionViewWillEnter() {
  27. this.loadUserInfo();
  28. this.checkStatuses(); // 每次进入页面时检查状态
  29. }
  30. ngOnDestroy() {
  31. if (this.userInfoSubscription) {
  32. this.userInfoSubscription.unsubscribe();
  33. }
  34. }
  35. private loadUserInfo() {
  36. this.userAvatar = '/assets/images/user.png'; // 立即设置默认头像
  37. // 取消之前的订阅(如果有)
  38. if (this.userInfoSubscription) {
  39. this.userInfoSubscription.unsubscribe();
  40. }
  41. // 订阅最新的用户信息
  42. this.userInfoSubscription = this.userService.getUserInfo().subscribe(userInfo => {
  43. if (userInfo && userInfo.userAvatar) {
  44. this.userAvatar = userInfo.userAvatar;
  45. this.previewImage = userInfo.userAvatar;
  46. }
  47. // 如果 userInfo 为 null 或 undefined,保持默认头像不变
  48. });
  49. }
  50. private checkStatuses() {
  51. // 尝试从 localStorage 加载状态
  52. const savedStates = JSON.parse(localStorage.getItem('userStatuses') || '{}');
  53. this.hasUnreadHealthRecords = savedStates.hasUnreadHealthRecords ?? false;
  54. this.hasPendingAppointments = savedStates.hasPendingAppointments ?? false;
  55. this.hasNewPromotions = savedStates.hasNewPromotions ?? false;
  56. this.hasUnprocessedOrders = savedStates.hasUnprocessedOrders ?? false;
  57. }
  58. private saveStatuses() {
  59. const userStatuses = {
  60. hasUnreadHealthRecords: this.hasUnreadHealthRecords,
  61. hasPendingAppointments: this.hasPendingAppointments,
  62. hasNewPromotions: this.hasNewPromotions,
  63. hasUnprocessedOrders: this.hasUnprocessedOrders
  64. };
  65. localStorage.setItem('userStatuses', JSON.stringify(userStatuses));
  66. }
  67. editProfile() {
  68. console.log('编辑用户资料');
  69. this.navCtrl.navigateForward('/edit-profile');
  70. }
  71. viewHealthRecord() {
  72. console.log('查看/编辑健康档案');
  73. this.navCtrl.navigateForward('/health-record');
  74. this.hasUnreadHealthRecords = false; // 标记为已读
  75. this.saveStatuses(); // 保存状态
  76. }
  77. viewMedicalServices() {
  78. console.log('查看医疗服务');
  79. this.navCtrl.navigateForward('/medical-services');
  80. this.hasPendingAppointments = false;
  81. this.saveStatuses();
  82. }
  83. viewPointsAndCoupons() {
  84. console.log('查看积分与优惠');
  85. this.navCtrl.navigateForward('/points-and-coupons');
  86. this.hasNewPromotions = false;
  87. this.saveStatuses();
  88. }
  89. viewOrders() {
  90. console.log('查看订单管理');
  91. this.navCtrl.navigateForward('/orders');
  92. this.hasUnprocessedOrders = false;
  93. this.saveStatuses();
  94. }
  95. goToFaq() {
  96. console.log('跳转到常见问题页面');
  97. this.navCtrl.navigateForward('/faq');
  98. }
  99. goToFeedback() {
  100. console.log('跳转到意见反馈页面');
  101. this.navCtrl.navigateForward('/feedback');
  102. }
  103. inviteFriends() {
  104. console.log('邀请好友');
  105. this.navCtrl.navigateForward('/invite-friends');
  106. }
  107. manageFamily() {
  108. console.log('家人管理');
  109. this.navCtrl.navigateForward('/manage-family');
  110. }
  111. }