tab4.page.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { Component, OnInit } from '@angular/core';
  2. import { Router } from '@angular/router';
  3. import {
  4. IonHeader,
  5. IonToolbar,
  6. IonTitle,
  7. IonContent,
  8. IonCard,
  9. IonCardContent,
  10. IonGrid,
  11. IonRow,
  12. IonCol,
  13. IonAvatar,
  14. IonButton,
  15. ModalController,
  16. IonItem,
  17. IonList,
  18. IonSegment,
  19. IonSegmentButton,
  20. IonLabel,
  21. IonSegmentView,
  22. IonSegmentContent,
  23. } from '@ionic/angular/standalone';
  24. import { CloudQuery, CloudUser } from 'src/lib/cyxncloud';
  25. import { ModalEditPagePage } from '../page/modal/modal-edit-page/modal-edit-page.page';
  26. export interface User {
  27. avatar: string;
  28. username: string;
  29. age: number | null;
  30. sex: string;
  31. email: string;
  32. phoneNumber: string;
  33. height: number;
  34. weight: number;
  35. fans: number;
  36. likes: number;
  37. }
  38. @Component({
  39. selector: 'app-tab4',
  40. templateUrl: 'tab4.page.html',
  41. styleUrls: ['tab4.page.scss'],
  42. standalone: true,
  43. imports: [
  44. IonHeader,
  45. IonToolbar,
  46. IonTitle,
  47. IonContent,
  48. IonCard,
  49. IonGrid,
  50. IonRow,
  51. IonCol,
  52. IonAvatar,
  53. IonCardContent,
  54. IonButton,
  55. IonItem,
  56. IonList,
  57. IonSegment,
  58. IonSegmentButton,
  59. IonLabel,
  60. IonSegmentView,
  61. IonSegmentContent,
  62. ],
  63. })
  64. export class Tab4Page implements OnInit {
  65. is_login: boolean = false;
  66. currentUser: CloudUser | null = null;
  67. userChangeInfo: any = '';
  68. user: User = {
  69. avatar: 'https://ionicframework.com/docs/img/demos/avatar.svg',
  70. username: '请登录',
  71. age: 0,
  72. sex: '-',
  73. email: '-',
  74. phoneNumber: '-',
  75. height: 0,
  76. weight: 0,
  77. fans: 0,
  78. likes: 0,
  79. };
  80. constructor(private router: Router, private modalCtrl: ModalController) {
  81. // this.getAllNews();
  82. let userCache = localStorage.getItem('userData');
  83. if (userCache) {
  84. let userData = JSON.parse(userCache);
  85. this.user.avatar = userData.avatar;
  86. this.user.username = userData.username;
  87. this.user.age = userData.age;
  88. this.user.sex = userData.sex;
  89. this.user.email = userData.email;
  90. this.user.phoneNumber = userData.phoneNumber;
  91. this.user.height = userData.height;
  92. this.user.weight = userData.weight;
  93. this.user.fans = userData.fans;
  94. this.user.likes = userData.likes;
  95. let is_loginCache = localStorage.getItem('is_login');
  96. this.is_login = is_loginCache === 'true' ? true : false;
  97. }
  98. }
  99. hasRefreshedKey = 'hasRefreshed'; // 用于在 localStorage 中存储状态
  100. ngOnInit() {}
  101. refreshPage() {
  102. window.location.reload(); // 强制刷新整个页面
  103. }
  104. navigateTo(path: string) {
  105. this.router.navigate([`/${path}`]);
  106. }
  107. async openModal() {
  108. const modal = await this.modalCtrl.create({
  109. component: ModalEditPagePage,
  110. });
  111. modal.present();
  112. const { data, role } = await modal.onWillDismiss();
  113. if (role === 'confirm') {
  114. console.log(data);
  115. this.userChangeInfo = data;
  116. this.updateUser();
  117. }
  118. }
  119. async logout() {
  120. this.is_login = false;
  121. let user = new CloudUser();
  122. let flag = await user.logout();
  123. localStorage.removeItem('userData');
  124. localStorage.removeItem('is_login');
  125. }
  126. async updateUser() {
  127. Object.keys(this.userChangeInfo).forEach((key) => {
  128. if (key == 'age') {
  129. this.userChangeInfo[key] = Number(this.userChangeInfo[key]);
  130. }
  131. if (key == 'height') {
  132. this.userChangeInfo[key] = Number(this.userChangeInfo[key]);
  133. }
  134. if (key == 'weight') {
  135. this.userChangeInfo[key] = Number(this.userChangeInfo[key]);
  136. }
  137. });
  138. let user = new CloudUser();
  139. let res = await user.update(this.userChangeInfo);
  140. console.log('成功更新用户信息');
  141. }
  142. newslists: any[] = [];
  143. // async getAllNews() {
  144. // let className = 'News';
  145. // let newsQuery = new CloudQuery(className);
  146. // let newslist = await newsQuery.getAll();
  147. // this.newslists = newslist.results;
  148. // this.newslists = newslist.results.map((news: any) => {
  149. // // 删除所有空格,并替换换行符\n\n为</p><p>,并在开始和结束处添加<p>和</p>
  150. // news.article =
  151. // '<p>' +
  152. // news.article.replace(/\n\n/g, '</p><p>').replace(/\s+/g, '') +
  153. // '</p>';
  154. // return news;
  155. // });
  156. // }
  157. }