throw-drift-bottle.component_20241224203132.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { Component, OnInit } from '@angular/core';
  2. import { ModalController, IonInput, IonItem, IonLabel, IonButton, IonCard, IonCardHeader, IonCardTitle, IonCardSubtitle, IonCardContent } from '@ionic/angular/standalone';
  3. import { CloudObject } from 'src/lib/ncloud';
  4. import { FormsModule } from '@angular/forms'; // 导入 FormsModule
  5. import { CommonModule } from '@angular/common';
  6. @Component({
  7. selector: 'app-throw-drift-bottle',
  8. templateUrl: './throw-drift-bottle.component.html',
  9. styleUrls: ['./throw-drift-bottle.component.scss'],
  10. standalone: true,
  11. imports: [
  12. IonCard,
  13. IonCardHeader,
  14. IonCardTitle,
  15. IonCardSubtitle,
  16. IonCardContent,
  17. IonButton,
  18. IonInput,
  19. IonItem,
  20. IonLabel,
  21. FormsModule,
  22. CommonModule
  23. ],
  24. })
  25. export class ThrowDriftBottleComponent implements OnInit {
  26. driftbottleData: { content: string } = { content: '' }; // 漂流瓶内容
  27. constructor(private modalCtrl: ModalController) {}
  28. ngOnInit() {}
  29. throw() {
  30. console.log('投掷漂流瓶:');
  31. console.log(`内容: ${this.driftbottleData.content}`);
  32. // 这里可以添加逻辑将漂流瓶内容发布到后端或其他处理
  33. const driftbottleData = {
  34. content: this.driftbottleData.content,
  35. };
  36. let driftbottle = new CloudObject("Driftbottle");
  37. driftbottle.set(driftbottleData);
  38. driftbottle.save().then(data => {
  39. console.log('漂流瓶已成功投掷', data);
  40. this.modalCtrl.dismiss(driftbottle, "confirm");
  41. }).catch(err => {
  42. console.error('投掷漂流瓶时出错');
  43. });
  44. }
  45. cancel() {
  46. this.modalCtrl.dismiss(null, "cancel");
  47. }
  48. }
  49. export async function openThrowDriftBottleModal(modalCtrl: ModalController): Promise<any> {
  50. const modal = await modalCtrl.create({
  51. component: ThrowDriftBottleComponent,
  52. breakpoints: [0.7, 1.0],
  53. initialBreakpoint: 0.7
  54. });
  55. modal.present();
  56. const { data, role } = await modal.onWillDismiss();
  57. if (role === 'confirm') {
  58. return data;
  59. }
  60. return null;
  61. }