import { Component } from '@angular/core'; import { NavController, AlertController } from '@ionic/angular'; import Parse from 'parse'; @Component({ selector: 'app-post-create', templateUrl: './post-create.page.html', styleUrls: ['./post-create.page.scss'], }) export class PostCreatePage { title: string = ''; content: string = ''; image: string = ''; constructor(private navCtrl: NavController, private alertCtrl: AlertController) {} async uploadImage() { // 实现上传图片的逻辑 const alert = await this.alertCtrl.create({ header: '上传图片', inputs: [ { name: 'imageUrl', type: 'url', placeholder: '输入图片URL' } ], buttons: [ { text: '取消', role: 'cancel' }, { text: '上传', handler: (data) => { this.image = data.imageUrl; } } ] }); await alert.present(); } async submitPost() { if (!this.title || !this.content) { const alert = await this.alertCtrl.create({ header: '错误', message: '标题和正文不能为空', buttons: ['确定'] }); await alert.present(); return; } const currentUser = Parse.User.current(); if (!currentUser) { const alert = await this.alertCtrl.create({ header: '错误', message: '请先登录', buttons: ['确定'] }); await alert.present(); return; } // 获取用户的 username 和 avatarUrl const authorInfo = { username: currentUser.get('username'), avatarUrl: currentUser.get('avatarUrl') || 'assets/images/default-avatar.jpg' // 默认头像路径 }; const Post240709 = Parse.Object.extend('Post240709'); const post = new Post240709(); post.set('title', this.title); post.set('content', this.content); post.set('imageUrl', this.image); post.set('author', authorInfo); // 设置 author 字段为包含 username 和 avatarUrl 的对象 try { const savedPost = await post.save(); const successAlert = await this.alertCtrl.create({ header: '成功', message: '发布成功', buttons: ['确认'] }); await successAlert.present(); // // 延迟一段时间后返回首页 // setTimeout(() => { // this.navCtrl.navigateBack('/home'); // }, 1000); // 1秒后返回首页,可以根据需求调整延迟时间 } catch (error) { console.error('Error while creating post:', error); const alert = await this.alertCtrl.create({ header: '错误', message: '发布失败,请重试', buttons: ['确定'] }); await alert.present(); } } }