drift-bottle.component_20241224222313.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // drift-bottle.component.ts
  2. import { Component, OnInit } from '@angular/core';
  3. import { Router } from '@angular/router';
  4. import { IonicModule } from '@ionic/angular';
  5. import { openThrowDriftBottleModal } from '../throw-drift-bottle/throw-drift-bottle.component';
  6. import { ModalController } from '@ionic/angular/standalone';
  7. import { HttpClient } from '@angular/common/http';
  8. import { CloudObject, CloudQuery } from 'src/lib/ncloud';
  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 {
  20. alertHeader: string = '';
  21. alertMessage: string = '';
  22. constructor(
  23. private router: Router,
  24. private modalCtrl: ModalController,
  25. private http: HttpClient
  26. ) {}
  27. ngOnInit() {}
  28. throwDriftBottleModal() {
  29. openThrowDriftBottleModal(this.modalCtrl);
  30. }
  31. catchDriftBottle() {
  32. const query = new CloudQuery("DriftBottle");
  33. query.equalTo("status", "drifting");
  34. query.find().then((driftingBottles: any[]) => {
  35. if (driftingBottles.length === 0) {
  36. this.alertHeader = '失败';
  37. this.alertMessage = '没捞着…';
  38. return;
  39. }
  40. const randomIndex = Math.floor(Math.random() * driftingBottles.length);
  41. const bottleToCatch = driftingBottles[randomIndex];
  42. bottleToCatch.set("status", "catched");
  43. bottleToCatch.save().then(() => {
  44. this.alertHeader = '成功';
  45. this.alertMessage = '捞到啦!';
  46. }).catch((err: any) => {
  47. console.error('更新漂流瓶状态时出错:', err);
  48. this.alertHeader = '错误';
  49. this.alertMessage = '发生错误,请重试。';
  50. });
  51. }).catch((err) => {
  52. console.error('获取漂流瓶时出错:', err);
  53. this.alertHeader = '错误';
  54. this.alertMessage = '发生错误,请重试。';
  55. });
  56. }
  57. dismissAlert() {
  58. this.alertHeader = '';
  59. this.alertMessage = '';
  60. }
  61. }