12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- import { Component } from '@angular/core';
- import { ModalController } from '@ionic/angular';
- import { CloudObject, CloudUser } from 'src/lib/ncloud';
- @Component({
- selector: 'app-edit',
- templateUrl: './edit.page.html',
- styleUrls: ['./edit.page.scss'],
- standalone:false,
- })
- export class EditPage {
- diary: any = {
- Did: 0,
- date: this.getCurrentDate(),
- weekday: this.getCurrentWeekday(),
- time: this.getCurrentTime(),
- content: '',
- weather: '',
- mood: '',
- user: '' // 添加用户字段
- };
- currentUser: CloudUser | null = null;
- constructor(private modalCtrl: ModalController) {}
- ngOnInit() {
- // 生成随机ID
- this.diary.Did = Math.floor(Math.random() * 100000);
- // 设置当前用户
- if (this.currentUser) {
- this.diary.user = this.currentUser.toPointer();
- }
- }
- // 获取当前日期
- private getCurrentDate(): string {
- //return new Date().getDate().toString();
- const now = new Date();
- return `${now.getFullYear()}-${(now.getMonth() + 1).toString().padStart(2, '0')}-${now.getDate().toString().padStart(2, '0')}`;
- }
- // 获取当前星期
- private getCurrentWeekday(): string {
- const weekdays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
- return weekdays[new Date().getDay()];
- }
- // 获取当前时间
- private getCurrentTime(): string {
- const now = new Date();
- return `${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}`;
- }
- async saveDiary() {
- if (!this.diary.user) {
- console.error('用户未登录,无法保存日记');
- this.modalCtrl.dismiss({ saved: false });
- return;
- }
- // 创建新的日记对象
- const newDiary = new CloudObject("Diary");
- newDiary.set(this.diary);
-
- try {
- await newDiary.save();
- // 保存成功,返回并刷新列表
- this.modalCtrl.dismiss({ saved: true });
- } catch (error) {
- console.error('保存日记失败:', error);
- this.modalCtrl.dismiss({ saved: false });
- }
- }
- // 关闭模态框
- dismiss() {
- this.modalCtrl.dismiss({ saved: false });
- }
- }
|