edit.page.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { Component } from '@angular/core';
  2. import { ModalController } from '@ionic/angular';
  3. import { CloudObject, CloudUser } 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. diary: any = {
  12. Did: 0,
  13. date: this.getCurrentDate(),
  14. weekday: this.getCurrentWeekday(),
  15. time: this.getCurrentTime(),
  16. content: '',
  17. weather: '',
  18. mood: ''
  19. };
  20. constructor(private modalCtrl: ModalController) {}
  21. ngOnInit() {
  22. // 生成随机ID
  23. this.diary.Did = Math.floor(Math.random() * 100000);
  24. }
  25. // 获取当前日期
  26. private getCurrentDate(): string {
  27. return new Date().getDate().toString();
  28. }
  29. // 获取当前星期
  30. private getCurrentWeekday(): string {
  31. const weekdays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
  32. return weekdays[new Date().getDay()];
  33. }
  34. // 获取当前时间
  35. private getCurrentTime(): string {
  36. const now = new Date();
  37. return `${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}`;
  38. }
  39. async saveDiary() {
  40. // 创建新的日记对象
  41. const newDiary = new CloudObject("Diary");
  42. //newDiary.equalTo('user',Parse.User.current()?.id);
  43. //const currentUser = Parse.User.current();
  44. newDiary.set(this.diary);
  45. try {
  46. await newDiary.save();
  47. // 保存成功,返回并刷新列表
  48. this.modalCtrl.dismiss({ saved: true });
  49. } catch (error) {
  50. console.error('保存日记失败:', error);
  51. this.modalCtrl.dismiss({ saved: false });
  52. }
  53. }
  54. // 关闭模态框
  55. dismiss() {
  56. this.modalCtrl.dismiss({ saved: false });
  57. }
  58. }