tab3.page.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
  2. import { Router } from '@angular/router';
  3. import { ToastController } from '@ionic/angular';
  4. import * as Parse from "parse"
  5. import Swiper from 'swiper';
  6. @Component({
  7. selector: 'app-tab3',
  8. templateUrl: 'tab3.page.html',
  9. styleUrls: ['tab3.page.scss']
  10. })
  11. export class Tab3Page implements OnInit {
  12. @ViewChild("slide1Comp") slide1Comp:ElementRef |undefined
  13. ngOnInit() {
  14. this.getReply()
  15. // 延迟500毫秒加载
  16. setTimeout(() => {
  17. this.initSwiper();
  18. }, 500);
  19. }
  20. slide1:Swiper|undefined
  21. initSwiper(){
  22. console.log(this.slide1Comp)
  23. this.slide1 = new Swiper(this.slide1Comp?.nativeElement, {
  24. loop:true,
  25. autoplay:{
  26. delay:500,
  27. },
  28. mousewheel: {
  29. forceToAxis: true,
  30. },
  31. pagination:{
  32. el:".swiper-pagination",
  33. clickable:true
  34. }
  35. });
  36. }
  37. constructor(private toastController: ToastController,private router: Router) {}
  38. activeTab:string = 'explore';
  39. likedPosts: number[] = [];
  40. favoritePosts: number[] = [];
  41. followedUsers: number[] = [];
  42. changeTab(event:any){
  43. this.activeTab = event.detail.value
  44. }
  45. /**
  46. * 查询评论列表
  47. */
  48. replyList:any [] = []
  49. async getReply(){
  50. let query = new Parse.Query("Discuss")
  51. query.include("user")
  52. this.replyList = await query.find()
  53. console.log(this.replyList);
  54. }
  55. /**
  56. * 保存评论
  57. */
  58. currentUser:any
  59. comment = ''
  60. async saveComment(){
  61. this.currentUser = Parse.User.current();
  62. let com = Parse.Object.extend("Discuss")
  63. com = new com()
  64. com.set('user',{__type:'Pointer',className:'_User',objectId:this.currentUser?.id})
  65. com.set('reply',this.comment)
  66. try {
  67. await com.save();
  68. console.log('保存成功');
  69. this.getReply()
  70. this.comment = ''
  71. } catch (error) {
  72. console.error('保存失败',error);
  73. }
  74. }
  75. openSettingsPage() {
  76. // 打开设置页面
  77. this.router.navigate(['/settings']);
  78. }
  79. // navigateToPage(page: string) {
  80. // this.router.navigate([`/tabs/${page}`]);
  81. // }
  82. sendComment() {
  83. // 模拟发送评论的操作
  84. this.presentToast('评论已发送');
  85. }
  86. async presentToast(message: string) {
  87. const toast = await this.toastController.create({
  88. message: message,
  89. duration: 2000
  90. });
  91. toast.present();
  92. }
  93. likePost(postId: number) {
  94. if (this.isPostLiked(postId)) {
  95. this.likedPosts = this.likedPosts.filter(id => id !== postId);
  96. } else {
  97. this.likedPosts.push(postId);
  98. }
  99. }
  100. isPostLiked(postId: number): boolean {
  101. return this.likedPosts.includes(postId);
  102. }
  103. toggleFavorite(postId: number) {
  104. if (this.isPostFavorite(postId)) {
  105. this.favoritePosts = this.favoritePosts.filter(id => id !== postId);
  106. } else {
  107. this.favoritePosts.push(postId);
  108. }
  109. }
  110. isPostFavorite(postId: number): boolean {
  111. return this.favoritePosts.includes(postId);
  112. }
  113. toggleFollow(userId: number) {
  114. if (this.isUserFollowed(userId)) {
  115. this.followedUsers = this.followedUsers.filter(id => id !== userId);
  116. } else {
  117. this.followedUsers.push(userId);
  118. }
  119. }
  120. isUserFollowed(userId: number): boolean {
  121. return this.followedUsers.includes(userId);
  122. }
  123. goToAIChatPage() {
  124. this.router.navigate(['/aichat']); // 导航到AI对话页面
  125. }
  126. }