home.page.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // home.page.ts
  2. import { Component, ViewChild, ElementRef, AfterViewInit } from '@angular/core';
  3. import { CommonModule } from '@angular/common';
  4. import { IonicModule, PopoverController } from '@ionic/angular';
  5. import { Router } from '@angular/router';
  6. interface Story {
  7. id: string;
  8. cover: string;
  9. title: string;
  10. createTime: Date;
  11. }
  12. @Component({
  13. selector: 'app-home',
  14. templateUrl: 'home.page.html',
  15. styleUrls: ['home.page.scss'],
  16. standalone: true,
  17. imports: [IonicModule, CommonModule]
  18. })
  19. export class HomePage implements AfterViewInit {
  20. stories: Story[] = [];
  21. slideWidth: number = 0; // 设置默认值为0
  22. autoPlayInterval: NodeJS.Timeout | undefined;
  23. currentIndex: number = 0;
  24. slides: any[] = []; // 初始化为空数组
  25. translateX: number = 0;
  26. startX: number = 0;
  27. currentTranslateX: number = 0;
  28. @ViewChild('bannerContainer', { read: ElementRef }) bannerContainer!: ElementRef;
  29. ngAfterViewInit() {
  30. this.startAutoPlay();
  31. this.checkSlideWidth();
  32. }
  33. ngAfterViewChecked() {
  34. this.checkSlideWidth();
  35. }
  36. private checkSlideWidth() {
  37. const container = this.bannerContainer.nativeElement;
  38. if (container && container.clientWidth > 0) {
  39. this.slideWidth = container.clientWidth;
  40. console.log('slideWidth:', this.slideWidth);
  41. this.slides = [
  42. { image: 'assets/images/banner1.jpg', alt: 'Slide 1' },
  43. { image: 'assets/images/banner2.jpg', alt: 'Slide 2' },
  44. { image: 'assets/images/banner3.jpg', alt: 'Slide 3' },
  45. { image: 'assets/images/banner4.jpg', alt: 'Slide 4' },
  46. { image: 'assets/images/banner5.jpg', alt: 'Slide 5' },
  47. ];
  48. this.updateTranslateX();
  49. } else {
  50. console.warn('bannerContainer not found or has zero width');
  51. }
  52. }
  53. ngOnDestroy() {
  54. this.stopAutoPlay();
  55. }
  56. private startAutoPlay() {
  57. this.stopAutoPlay();
  58. this.autoPlayInterval = setInterval(() => {
  59. this.nextSlide();
  60. }, 3000); // 每3秒切换一次
  61. }
  62. private stopAutoPlay() {
  63. if (this.autoPlayInterval) {
  64. clearInterval(this.autoPlayInterval);
  65. }
  66. }
  67. nextSlide() {
  68. this.currentIndex = (this.currentIndex + 1) % this.slides.length;
  69. this.updateTranslateX();
  70. }
  71. prevSlide() {
  72. this.currentIndex = (this.currentIndex - 1 + this.slides.length) % this.slides.length;
  73. this.updateTranslateX();
  74. }
  75. goToSlide(index: number) {
  76. this.currentIndex = index;
  77. this.updateTranslateX();
  78. }
  79. private updateTranslateX() {
  80. if (this.slideWidth !== undefined && this.slideWidth > 0) {
  81. this.translateX = -this.currentIndex * this.slideWidth;
  82. this.currentTranslateX = this.translateX;
  83. console.log('translateX:', this.translateX); // 添加调试信息
  84. } else {
  85. console.warn('slideWidth is not defined or zero');
  86. }
  87. }
  88. onTouchStart(event: TouchEvent) {
  89. this.startX = event.touches[0].clientX;
  90. this.stopAutoPlay();
  91. console.log('Touch Start:', this.startX); // 添加调试信息
  92. }
  93. onTouchMove(event: TouchEvent) {
  94. const currentX = event.touches[0].clientX;
  95. const diffX = currentX - this.startX;
  96. this.currentTranslateX = this.translateX + diffX;
  97. console.log('Touch Move:', diffX, this.currentTranslateX); // 添加调试信息
  98. }
  99. onTouchEnd(event: TouchEvent) {
  100. const currentX = event.changedTouches[0].clientX;
  101. const diffX = currentX - this.startX;
  102. if (diffX > 50) {
  103. this.prevSlide();
  104. } else if (diffX < -50) {
  105. this.nextSlide();
  106. } else {
  107. this.updateTranslateX();
  108. }
  109. this.startAutoPlay();
  110. console.log('Touch End:', diffX, this.currentIndex); // 添加调试信息
  111. }
  112. constructor(
  113. private router: Router,
  114. private popoverController: PopoverController
  115. ) { }
  116. navigateTo(path: string) {
  117. this.router.navigate([path]);
  118. }
  119. async presentCreateOptions(ev: any) {
  120. const popover = await this.popoverController.create({
  121. component: CreateOptionsComponent,
  122. event: ev,
  123. translucent: true,
  124. size: 'auto'
  125. });
  126. await popover.present();
  127. const { data } = await popover.onDidDismiss();
  128. if (data) {
  129. this.createStory(data.type);
  130. }
  131. }
  132. createStory(type: 'long' | 'short') {
  133. // 使用查询参数传递类型
  134. this.router.navigate(['/'], {
  135. queryParams: { generatorType: type, path: type === 'long' ? 'story-generator' : 'short-generator' }
  136. });
  137. const targetPath = type === 'long' ? '/story-generator' : '/short-generator';
  138. this.router.navigate([targetPath]);
  139. }
  140. formatDate(date: Date): string {
  141. return date.toLocaleDateString('zh-CN');
  142. }
  143. }
  144. @Component({
  145. template: `
  146. <ion-list>
  147. <ion-item button (click)="select('long')">
  148. <ion-icon name="book-outline" slot="start"></ion-icon>
  149. <ion-label>创建长篇</ion-label>
  150. </ion-item>
  151. <ion-item button (click)="select('short')">
  152. <ion-icon name="document-text-outline" slot="start"></ion-icon>
  153. <ion-label>创建短篇</ion-label>
  154. </ion-item>
  155. </ion-list>
  156. `,
  157. standalone: true,
  158. imports: [IonicModule]
  159. })
  160. export class CreateOptionsComponent {
  161. constructor(private popoverController: PopoverController) { }
  162. select(type: 'long' | 'short') {
  163. this.popoverController.dismiss({
  164. type: type
  165. });
  166. }
  167. }