edit.page.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { Component } from '@angular/core';
  2. import { NavController, NavParams } from '@ionic/angular';
  3. import { CloudObject } from 'src/lib/ncloud';
  4. @Component({
  5. selector: 'app-edit',
  6. templateUrl: 'edit.page.html',
  7. styleUrls: ['edit.page.scss'],
  8. standalone:false,
  9. })
  10. export class EditPage {
  11. action: string = '新建';
  12. diary: any = {
  13. date: new Date().getDate().toString(),
  14. weekday: this.getWeekday(new Date()),
  15. time: this.getCurrentTime(),
  16. content: '',
  17. weather: '晴',
  18. mood: '😊'
  19. };
  20. constructor(
  21. public navCtrl: NavController,
  22. private navParams: NavParams
  23. ) {
  24. const params = this.navParams.get('queryParams');
  25. this.action = params?.action || '新建';
  26. if (params?.diary) {
  27. this.diary = JSON.parse(params.diary);
  28. }
  29. }
  30. // 获取星期几
  31. private getWeekday(date: Date): string {
  32. const weekdays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
  33. return weekdays[date.getDay()];
  34. }
  35. // 获取当前时间
  36. private getCurrentTime(): string {
  37. const now = new Date();
  38. const hours = now.getHours().toString().padStart(2, '0');
  39. const minutes = now.getMinutes().toString().padStart(2, '0');
  40. return `${hours}:${minutes}`;
  41. }
  42. // 保存日记
  43. async saveDiary() {
  44. if (!this.diary.content) {
  45. console.error('日记内容不能为空');
  46. return;
  47. }
  48. const diaryObj = new CloudObject('Diary');
  49. // 如果是编辑现有日记
  50. if (this.diary.objectId) {
  51. diaryObj.id = this.diary.objectId;
  52. }
  53. // 设置日记数据
  54. diaryObj.set({
  55. ...this.diary,
  56. updatedAt: new Date().toISOString()
  57. });
  58. try {
  59. await diaryObj.save();
  60. this.navCtrl.back(); // 返回上一页
  61. } catch (error) {
  62. console.error('保存日记失败:', error);
  63. }
  64. }
  65. // 删除日记
  66. async deleteDiary() {
  67. if (!this.diary.objectId) {
  68. console.error('无法删除未保存的日记');
  69. return;
  70. }
  71. const diaryObj = new CloudObject('Diary');
  72. diaryObj.id = this.diary.objectId;
  73. try {
  74. await diaryObj.destroy();
  75. this.navCtrl.back(); // 返回上一页
  76. } catch (error) {
  77. console.error('删除日记失败:', error);
  78. }
  79. }
  80. }