tab2.page.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { Component,OnInit } from '@angular/core';
  2. import { AlertController } from '@ionic/angular';
  3. @Component({
  4. selector: 'app-tab2',
  5. templateUrl: './tab2.page.html',
  6. styleUrls: ['./tab2.page.scss'],
  7. })
  8. export class Tab2Page implements OnInit {
  9. segment: string = 'memo';
  10. memos: string[] = [];
  11. newMemo: string = '';
  12. messages: { sender: string, text: string }[] = [];
  13. newMessage: string = '';
  14. constructor(private alertController: AlertController) {}
  15. ngOnInit() {
  16. this.loadMemos();
  17. }
  18. //加载备忘录
  19. loadMemos() {
  20. const memos = localStorage.getItem('memos');
  21. if (memos) {
  22. this.memos = JSON.parse(memos);
  23. }
  24. }
  25. //保存备忘录
  26. saveMemos() {
  27. localStorage.setItem('memos', JSON.stringify(this.memos));
  28. }
  29. //获取时间戳
  30. getCurrentTimestamp(): string {
  31. const now = new Date();
  32. const year = now.getFullYear();
  33. const month = (now.getMonth() + 1).toString().padStart(2, '0');
  34. const day = now.getDate().toString().padStart(2, '0');
  35. const hours = now.getHours().toString().padStart(2, '0');
  36. const minutes = now.getMinutes().toString().padStart(2, '0');
  37. return `${year}年${month}月${day}日${hours}:${minutes}`;
  38. }
  39. //异步函数,添加备忘录
  40. async addMemo() {
  41. if (this.newMemo.trim().length > 0) {
  42. this.memos.push(this.newMemo);
  43. this.newMemo = '';
  44. this.saveMemos();
  45. const alert = await this.alertController.create({
  46. header: '提示',
  47. message: '添加成功',
  48. buttons: ['确定']
  49. });
  50. await alert.present();
  51. }
  52. }
  53. //异步函数,确认删除
  54. async confirmDeleteMemo(memo: string) {
  55. const alert = await this.alertController.create({
  56. header: '确认删除',
  57. message: `你确定要删除 "${memo}" 吗?`,
  58. buttons: [
  59. {
  60. text: '取消',
  61. role: 'cancel'
  62. },
  63. {
  64. text: '删除',
  65. handler: () => {
  66. this.memos = this.memos.filter(m => m !== memo);
  67. this.saveMemos();
  68. }
  69. }
  70. ]
  71. });
  72. await alert.present();
  73. }
  74. //删除备忘录
  75. deleteMemo(memo: string) {
  76. this.memos = this.memos.filter(m => m !== memo);
  77. this.saveMemos();
  78. }
  79. //
  80. sendMessage() {
  81. if (this.newMessage.trim().length > 0) {
  82. this.messages.push({ sender: 'user', text: this.newMessage });
  83. this.newMessage = '';
  84. // 模拟AI回复
  85. setTimeout(() => {
  86. this.messages.push({ sender: 'ai', text: 'AI的回复' });
  87. }, 1000);
  88. }
  89. }
  90. }