tab4.page.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import { Component, OnInit } from '@angular/core';
  2. import { ActionSheetController, AlertController } from '@ionic/angular';
  3. import Parse from 'parse';
  4. @Component({
  5. selector: 'app-tab4',
  6. templateUrl: './tab4.page.html',
  7. styleUrls: ['./tab4.page.scss'],
  8. })
  9. export class Tab4Page implements OnInit {
  10. rating: number = 0;
  11. user: Parse.User | undefined;
  12. userData: any = {
  13. username: '',
  14. email: '',
  15. phone: '',
  16. gender: '',
  17. tags: '',
  18. birthday: '' // 添加生日属性
  19. };
  20. stars: string[] = Array(5).fill('star-outline');
  21. hoverIndex: number = -1;
  22. ratedIndex: number = -1;
  23. constructor(private actionSheetCtrl: ActionSheetController, private alertCtrl: AlertController) {}
  24. ngOnInit() {
  25. this.fetchUserData();
  26. }
  27. async fetchUserData() {
  28. try {
  29. const currentUser = Parse.User.current();
  30. if (currentUser) {
  31. this.user = currentUser;
  32. this.userData = {
  33. username: currentUser.get('username'),
  34. email: currentUser.get('email'),
  35. phone: currentUser.get('phone'),
  36. gender: currentUser.get('gender'),
  37. tags: currentUser.get('tags'),
  38. birthday: currentUser.get('birthday') // 获取生日属性
  39. };
  40. }
  41. } catch (error) {
  42. console.error('Error fetching user data:', error);
  43. }
  44. }
  45. async saveUserData() {
  46. try {
  47. if (this.user) {
  48. this.user.set('username', this.userData.username);
  49. this.user.set('email', this.userData.email);
  50. this.user.set('phone', this.userData.phone);
  51. this.user.set('gender', this.userData.gender);
  52. this.user.set('tags', this.userData.tags);
  53. this.user.set('birthday', this.userData.birthday); // 保存生日属性
  54. await this.user.save();
  55. console.log('User data saved successfully');
  56. }
  57. } catch (error) {
  58. console.error('Error saving user data:', error);
  59. }
  60. }
  61. async presentActionSheet() {
  62. const actionSheet = await this.actionSheetCtrl.create({
  63. header: 'Actions',
  64. buttons: [
  65. {
  66. text: 'QQ',
  67. icon: 'logo-qq',
  68. handler: () => {
  69. this.shareToApp('qq');
  70. }
  71. },
  72. {
  73. text: '微信',
  74. icon: 'logo-wechat',
  75. handler: () => {
  76. this.shareToApp('wechat');
  77. }
  78. },
  79. {
  80. text: '微博',
  81. icon: 'logo-weibo',
  82. handler: () => {
  83. this.shareToApp('weibo');
  84. }
  85. },
  86. {
  87. text: 'Cancel',
  88. role: 'cancel',
  89. handler: () => {
  90. console.log('Cancel clicked');
  91. }
  92. }
  93. ],
  94. });
  95. await actionSheet.present();
  96. }
  97. shareToApp(app: string) {
  98. let url = '';
  99. switch (app) {
  100. case 'qq':
  101. url = 'mqq://';
  102. break;
  103. case 'wechat':
  104. url = 'weixin://';
  105. break;
  106. case 'weibo':
  107. url = 'sinaweibo://';
  108. break;
  109. }
  110. window.location.href = url;
  111. }
  112. rate(index: number) {
  113. this.ratedIndex = index;
  114. this.hoverIndex = -1;
  115. this.stars = this.stars.map((_, i) => (i <= index ? 'star' : 'star-outline'));
  116. console.log('Rated: ' + (index + 1) + ' stars');
  117. this.presentRatingAlert(index + 1);
  118. }
  119. hover(index: number) {
  120. this.hoverIndex = index;
  121. this.stars = this.stars.map((_, i) => (i <= index ? 'star' : 'star-outline'));
  122. }
  123. resetHover() {
  124. this.hoverIndex = -1;
  125. this.stars = this.stars.map((_, i) => (i <= this.ratedIndex ? 'star' : 'star-outline'));
  126. }
  127. async presentRatingAlert(rating: number) {
  128. const alert = await this.alertCtrl.create({
  129. header: 'Rating',
  130. message: `You have rated ${rating} stars.`,
  131. buttons: ['OK']
  132. });
  133. await alert.present();
  134. }
  135. }