my-drift-bottle.component.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { Component, OnInit } from '@angular/core';
  2. import { Router } from '@angular/router';
  3. import { CommonModule } from '@angular/common'; // 导入 CommonModule
  4. import { IonHeader, IonButton, IonContent, IonIcon, IonItem, IonLabel, IonList,
  5. IonListHeader, IonCardHeader, IonCardTitle, IonCardContent, IonTitle, IonCard, IonToolbar, IonInput, IonSearchbar } from '@ionic/angular/standalone';
  6. import { CloudQuery, CloudObject, CloudUser } from 'src/lib/ncloud';
  7. import { UserService } from 'src/app/user.service'; // 假设这是你的用户服务
  8. @Component({
  9. selector: 'app-my-drift-bottle',
  10. templateUrl: './my-drift-bottle.component.html',
  11. styleUrls: ['./my-drift-bottle.component.scss'],
  12. standalone: true,
  13. imports: [
  14. CommonModule, // 添加 CommonModule
  15. IonHeader, IonToolbar, IonTitle, IonContent,
  16. IonList, IonListHeader, IonItem, IonCardTitle,
  17. IonLabel, IonIcon, IonButton, IonCardContent,
  18. IonInput, IonSearchbar, IonCard, IonCardHeader,
  19. ]
  20. })
  21. export class MyDriftBottleComponent implements OnInit {
  22. filteredDriftbottles2: any[] = [];
  23. Driftbottles: any[] = [];
  24. filteredDriftbottles: any[] = [];
  25. username: string = '';
  26. constructor(private router: Router, private userService: UserService) {}
  27. ngOnInit() {
  28. this.getDriftbottles(); // 获取所有漂流瓶并进行过滤
  29. }
  30. async getDriftbottles() {
  31. let query = new CloudQuery("Driftbottle");
  32. this.Driftbottles = await query.find();
  33. this.filterDriftbottles();
  34. this.filterDriftbottles2();
  35. }
  36. filterDriftbottles() {
  37. let user = new CloudUser();
  38. console.log(user);
  39. this.filteredDriftbottles = this.Driftbottles.filter(driftbottle => {
  40. console.log(driftbottle.get("username"), user.get("username"));
  41. return driftbottle.get("username") === user.get("username") || driftbottle.get("username") === 'all';
  42. });
  43. }
  44. filterDriftbottles2() {
  45. let user = new CloudUser();
  46. console.log(user);
  47. this.filteredDriftbottles2 = this.Driftbottles.filter(driftbottle => {
  48. console.log(driftbottle.get("catcher"), user.get("username"));
  49. return driftbottle.get("catcher") === user.get("username") || driftbottle.get("catcher") === 'all';
  50. });
  51. }
  52. goDriftbottle() {
  53. this.router.navigate(['tabs/drift-bottle']);
  54. }
  55. gothrowNewDriftbottle() {
  56. this.router.navigate(['tabs/drift-bottle']); // 假设这是投掷新漂流瓶的路由
  57. }
  58. async rethrowBottle(bottle: CloudObject) {
  59. try {
  60. bottle.set({
  61. status: "drifting",
  62. catcher: null,
  63. catchtime: null
  64. });
  65. await bottle.save();
  66. this.getDriftbottles(); // 刷新漂流瓶列表
  67. console.log('漂流瓶扔回成功');
  68. } catch (err) {
  69. console.error('扔回漂流瓶时出错:', err);
  70. }
  71. }
  72. }