drift-bottle.component_20241227183207.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { Component, OnInit, OnDestroy, Renderer2, ElementRef, ViewChild } from '@angular/core';
  2. import { Router } from '@angular/router';
  3. import { IonicModule } from '@ionic/angular';
  4. import { openThrowDriftBottleModal } from '../throw-drift-bottle/throw-drift-bottle.component';
  5. import { ModalController } from '@ionic/angular/standalone';
  6. import { HttpClient } from '@angular/common/http';
  7. import { CloudObject, CloudQuery } from 'src/lib/ncloud';
  8. import { UserService } from 'src/app/user.service'; // 假设这是你的用户服务
  9. @Component({
  10. selector: 'app-drift-bottle',
  11. templateUrl: './drift-bottle.component.html',
  12. styleUrls: ['./drift-bottle.component.scss'],
  13. standalone: true,
  14. imports: [
  15. IonicModule,
  16. // 其他导入项...
  17. ]
  18. })
  19. export class DriftBottleComponent implements OnInit, OnDestroy {
  20. alertHeader: string = '';
  21. alertMessage: string = '';
  22. @ViewChild('backgroundAudio', { static: false }) audioElement!: ElementRef<HTMLAudioElement>;
  23. constructor(
  24. private router: Router,
  25. private modalCtrl: ModalController,
  26. private http: HttpClient,
  27. private renderer: Renderer2,
  28. private userService: UserService // 注入用户服务
  29. ) {}
  30. ngOnInit() {
  31. if (this.audioElement && this.audioElement.nativeElement) {
  32. const audio = this.audioElement.nativeElement;
  33. audio.loop = true;
  34. audio.play().catch(error => {
  35. console.error('Failed to play audio:', error);
  36. });
  37. } else {
  38. console.warn('Audio element not found');
  39. }
  40. }
  41. ngOnDestroy() {
  42. if (this.audioElement && this.audioElement.nativeElement) {
  43. const audio = this.audioElement.nativeElement;
  44. audio.pause();
  45. audio.currentTime = 0; // 重置音频时间
  46. console.log('Audio element paused and reset');
  47. } else {
  48. console.warn('Audio element not found during destroy');
  49. }
  50. console.log('ngOnDestroy called');
  51. }
  52. throwDriftBottleModal() {
  53. openThrowDriftBottleModal(this.modalCtrl);
  54. }
  55. catchDriftBottle() {
  56. const query = new CloudQuery("DriftBottle");
  57. query.equalTo("status", "drifting");
  58. query.find().then((driftingBottles: any[]) => {
  59. if (driftingBottles.length === 0) {
  60. this.alertHeader = '失败';
  61. this.alertMessage = '没捞到…';
  62. return;
  63. }
  64. const randomIndex = Math.floor(Math.random() * driftingBottles.length);
  65. const bottleToCatch = driftingBottles[randomIndex];
  66. const username = this.userService.getUsername(); // 获取当前用户的用户名
  67. if (!username) {
  68. console.error('用户未登录或未找到用户名');
  69. this.alertHeader = '错误';
  70. this.alertMessage = '用户未登录,请先登录。';
  71. return;
  72. }
  73. bottleToCatch.set("status", "caught");
  74. bottleToCatch.set("catcher", username); // 设置打捞者
  75. bottleToCatch.set("catchTime", new Date()); // 设置打捞时间
  76. bottleToCatch.save().then(() => {
  77. this.alertHeader = '成功';
  78. this.alertMessage = '捞到啦!快去看看吧!';
  79. }).catch((err: any) => {
  80. console.error('更新漂流瓶状态时出错:', err);
  81. this.alertHeader = '错误';
  82. this.alertMessage = '发生错误,请重试。';
  83. });
  84. }).catch((err) => {
  85. console.error('获取漂流瓶时出错:', err);
  86. this.alertHeader = '错误';
  87. this.alertMessage = '发生错误,请重试。';
  88. });
  89. }
  90. dismissAlert() {
  91. this.alertHeader = '';
  92. this.alertMessage = '';
  93. }
  94. goMydriftbottle() {
  95. this.router.navigate(['tabs/my-drift-bottle']);
  96. }
  97. }