tab1.page.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import { Component, OnInit, OnDestroy } from '@angular/core';
  2. import { CommonModule } from '@angular/common';
  3. import {
  4. IonHeader, IonToolbar, IonTitle, IonContent, IonSearchbar, IonIcon,
  5. IonTabButton, IonTabs, IonTabBar, IonLabel, IonNav, IonImg, IonSegmentView,
  6. IonSegment, IonSegmentButton, IonSegmentContent, IonThumbnail, IonButton, IonRippleEffect,
  7. IonBackButton, ModalController
  8. } from '@ionic/angular/standalone';
  9. import { ExploreContainerComponent } from '../explore-container/explore-container.component';
  10. import { Router } from '@angular/router';
  11. import { CloudUser } from 'src/lib/ncloud';
  12. import { openUserLoginModal } from 'src/lib/user/model-user-login/model-user-login.component';
  13. import { AlertController } from '@ionic/angular';
  14. import { CategoryModalComponent } from 'src/app/category-modal/category-modal.component';
  15. interface Slide {
  16. image: string;
  17. alt: string;
  18. }
  19. @Component({
  20. selector: 'app-tab1',
  21. templateUrl: 'tab1.page.html',
  22. styleUrls: ['tab1.page.scss'],
  23. standalone: true,
  24. imports: [
  25. CommonModule,
  26. IonHeader, IonToolbar, IonTitle, IonContent, ExploreContainerComponent,
  27. IonSearchbar, IonIcon, IonTabButton, IonTabs, IonTabBar, IonLabel, IonNav, IonImg,
  28. IonSegmentView, IonSegment, IonSegmentButton, IonSegmentContent, IonThumbnail, IonButton,
  29. IonRippleEffect, IonBackButton]
  30. })
  31. export class Tab1Page implements OnInit, OnDestroy {
  32. slides: Slide[] = [
  33. { image: 'assets/img/lunbo4.png', alt: '轮播图1' },
  34. { image: 'assets/img/lunbo1.png', alt: '轮播图2' },
  35. { image: 'assets/img/lunbo2.png', alt: '轮播图3' },
  36. { image: 'assets/img/lunbo3.png', alt: '轮播图4' },
  37. { image: 'assets/img/study.png', alt: '轮播图5' }
  38. ];
  39. currentIndex = 0;
  40. translateX = 0;
  41. private autoPlayInterval: any;
  42. private touchStartX = 0;
  43. private touchDeltaX = 0;
  44. private slideWidth = 0;
  45. // 添加分类数据
  46. categories = [
  47. {
  48. title: '艺术创作',
  49. items: ['绘画', '音乐', '舞蹈', '摄影', '手工艺', '书法', '设计']
  50. },
  51. {
  52. title: '运动健身',
  53. items: ['瑜伽', '健身', '游泳', '跑步', '球类运动', '户外运动', '武术']
  54. },
  55. {
  56. title: '文化学习',
  57. items: ['文学', '历史', '哲学', '外语', '写作', '戏剧', '诗歌']
  58. },
  59. {
  60. title: '科技创新',
  61. items: ['编程', '人工智能', '机器人', '电子制作', '3D打印', '虚拟现实']
  62. },
  63. {
  64. title: '生活技能',
  65. items: ['烹饪', '园艺', '美妆', '摄影', '旅行', '茶艺', '插花']
  66. },
  67. {
  68. title: '职业发展',
  69. items: ['商业', '金融', '管理', '营销', '创业', '职场技能']
  70. }
  71. ];
  72. // 添加控制分类弹窗显示的变量
  73. isCategoryModalOpen = false;
  74. constructor(
  75. private router: Router,
  76. private modalCtrl: ModalController,
  77. private alertCtrl: AlertController // 注入 AlertController
  78. ) { }
  79. ngOnInit() {
  80. this.startAutoPlay();
  81. // 获取轮播图容器宽度
  82. setTimeout(() => {
  83. const container = document.querySelector('.banner-container');
  84. if (container) {
  85. this.slideWidth = container.clientWidth;
  86. }
  87. });
  88. }
  89. ngOnDestroy() {
  90. this.stopAutoPlay();
  91. }
  92. private startAutoPlay() {
  93. this.stopAutoPlay();
  94. this.autoPlayInterval = setInterval(() => {
  95. this.nextSlide();
  96. }, 3000); // 每3秒切换一次
  97. }
  98. private stopAutoPlay() {
  99. if (this.autoPlayInterval) {
  100. clearInterval(this.autoPlayInterval);
  101. }
  102. }
  103. nextSlide() {
  104. this.currentIndex = (this.currentIndex + 1) % this.slides.length;
  105. this.updateTranslateX();
  106. }
  107. prevSlide() {
  108. this.currentIndex = (this.currentIndex - 1 + this.slides.length) % this.slides.length;
  109. this.updateTranslateX();
  110. }
  111. goToSlide(index: number) {
  112. this.currentIndex = index;
  113. this.updateTranslateX();
  114. }
  115. private updateTranslateX() {
  116. this.translateX = -this.currentIndex * this.slideWidth;
  117. }
  118. // 触摸事件处理
  119. onTouchStart(event: TouchEvent) {
  120. this.stopAutoPlay();
  121. this.touchStartX = event.touches[0].clientX;
  122. this.touchDeltaX = 0;
  123. }
  124. onTouchMove(event: TouchEvent) {
  125. this.touchDeltaX = event.touches[0].clientX - this.touchStartX;
  126. const newTranslateX = -this.currentIndex * this.slideWidth + this.touchDeltaX;
  127. this.translateX = newTranslateX;
  128. }
  129. onTouchEnd() {
  130. const threshold = this.slideWidth / 3;
  131. if (Math.abs(this.touchDeltaX) > threshold) {
  132. if (this.touchDeltaX > 0) {
  133. this.prevSlide();
  134. } else {
  135. this.nextSlide();
  136. }
  137. } else {
  138. this.updateTranslateX();
  139. }
  140. this.startAutoPlay();
  141. }
  142. async checkLoginAndNavigate(path: string) {
  143. const currentUser = new CloudUser();
  144. if (!currentUser.id) {
  145. console.log("用户ID不存在");
  146. // 使用 AlertController 来创建和显示弹窗
  147. const alert = await this.alertCtrl.create({
  148. header: '需要登录',
  149. message: '请先登录后再使用此功能',
  150. buttons: [
  151. {
  152. text: '取消',
  153. role: 'cancel'
  154. },
  155. {
  156. text: '去登录',
  157. handler: async () => {
  158. const user = await openUserLoginModal(this.modalCtrl);
  159. if (user) {
  160. this.router.navigate([path]);
  161. }
  162. }
  163. }
  164. ]
  165. });
  166. console.log("显示弹窗");
  167. await alert.present(); // 弹窗显示
  168. } else {
  169. this.router.navigate([path]);
  170. }
  171. }
  172. goToInterestTest() {
  173. this.checkLoginAndNavigate('/tabs/interest-test');
  174. }
  175. goToViewAll() {
  176. this.router.navigate(['/tabs/view-all'])
  177. }
  178. goToInterestPicture() {
  179. this.router.navigate(['/tabs/interest-picture'])
  180. }
  181. goToInterestSearch() {
  182. this.checkLoginAndNavigate('/tabs/interest-search');
  183. }
  184. goTomailbox() {
  185. this.router.navigate(['/tabs/mailbox'])
  186. }
  187. goToAttendance() {
  188. this.router.navigate(['/tabs/attendance'])
  189. }
  190. goTodrawing() {
  191. this.router.navigate(['/tabs/drawing'])
  192. }
  193. // 添加打开分类弹窗的方法
  194. async openCategoryModal() {
  195. const modal = await this.modalCtrl.create({
  196. component: CategoryModalComponent,
  197. componentProps: {
  198. categories: this.categories
  199. },
  200. breakpoints: [0, 0.9],
  201. initialBreakpoint: 0.9,
  202. cssClass: 'category-modal'
  203. });
  204. await modal.present();
  205. }
  206. }