123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- import { Component } from '@angular/core';
- import { NavController, NavParams } from '@ionic/angular';
- import { CloudObject } from 'src/lib/ncloud';
- @Component({
- selector: 'app-edit',
- templateUrl: 'edit.page.html',
- styleUrls: ['edit.page.scss'],
- standalone:false,
- })
- export class EditPage {
- action: string = '新建';
- diary: any = {
- date: new Date().getDate().toString(),
- weekday: this.getWeekday(new Date()),
- time: this.getCurrentTime(),
- content: '',
- weather: '晴',
- mood: '😊'
- };
- constructor(
- public navCtrl: NavController,
- private navParams: NavParams
- ) {
- const params = this.navParams.get('queryParams');
- this.action = params?.action || '新建';
-
- if (params?.diary) {
- this.diary = JSON.parse(params.diary);
- }
- }
- // 获取星期几
- private getWeekday(date: Date): string {
- const weekdays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
- return weekdays[date.getDay()];
- }
- // 获取当前时间
- private getCurrentTime(): string {
- const now = new Date();
- const hours = now.getHours().toString().padStart(2, '0');
- const minutes = now.getMinutes().toString().padStart(2, '0');
- return `${hours}:${minutes}`;
- }
- // 保存日记
- async saveDiary() {
- if (!this.diary.content) {
- console.error('日记内容不能为空');
- return;
- }
- const diaryObj = new CloudObject('Diary');
-
- // 如果是编辑现有日记
- if (this.diary.objectId) {
- diaryObj.id = this.diary.objectId;
- }
-
- // 设置日记数据
- diaryObj.set({
- ...this.diary,
- updatedAt: new Date().toISOString()
- });
- try {
- await diaryObj.save();
- this.navCtrl.back(); // 返回上一页
- } catch (error) {
- console.error('保存日记失败:', error);
- }
- }
- // 删除日记
- async deleteDiary() {
- if (!this.diary.objectId) {
- console.error('无法删除未保存的日记');
- return;
- }
- const diaryObj = new CloudObject('Diary');
- diaryObj.id = this.diary.objectId;
-
- try {
- await diaryObj.destroy();
- this.navCtrl.back(); // 返回上一页
- } catch (error) {
- console.error('删除日记失败:', error);
- }
- }
- }
|