|
@@ -1,5 +1,7 @@
|
|
|
-import { Component, OnInit } from '@angular/core';
|
|
|
+import { Component, OnInit, ViewChild } from '@angular/core';
|
|
|
import { ActivatedRoute } from '@angular/router';
|
|
|
+import { AlertController, ModalController } from '@ionic/angular';
|
|
|
+import Parse from 'parse';
|
|
|
|
|
|
@Component({
|
|
|
selector: 'app-post-detail',
|
|
@@ -8,28 +10,84 @@ import { ActivatedRoute } from '@angular/router';
|
|
|
})
|
|
|
export class PostDetailPage implements OnInit {
|
|
|
post: any;
|
|
|
- comments: any[]= [];
|
|
|
+ comments: any[] = [];
|
|
|
+ @ViewChild('shareModal', { static: true }) shareModal: any;
|
|
|
|
|
|
- constructor(private route: ActivatedRoute) {}
|
|
|
+ constructor(
|
|
|
+ private route: ActivatedRoute,
|
|
|
+ private alertCtrl: AlertController,
|
|
|
+ private modalCtrl: ModalController
|
|
|
+ ) {}
|
|
|
|
|
|
ngOnInit() {
|
|
|
const postId = this.route.snapshot.paramMap.get('id');
|
|
|
- // 模拟获取数据,根据实际情况从服务器或本地获取数据
|
|
|
- this.post = {
|
|
|
- id: postId,
|
|
|
- author: '发帖人',
|
|
|
- authorAvatar: '发帖人头像URL',
|
|
|
- time: '发帖时间',
|
|
|
- title: `标题${postId}`,
|
|
|
- content: `内容${postId}`,
|
|
|
- image: `图片URL${postId}`
|
|
|
- };
|
|
|
-
|
|
|
- this.comments = [
|
|
|
- { id: 1, author: '用户1', avatar: '用户1头像URL', content: '评论内容1', time: '评论时间1', likes: 10 },
|
|
|
- { id: 2, author: '用户2', avatar: '用户2头像URL', content: '评论内容2', time: '评论时间2', likes: 5 },
|
|
|
- // 添加更多评论数据
|
|
|
- ];
|
|
|
+ if (postId !== null) {
|
|
|
+ this.loadPost(postId);
|
|
|
+ this.loadComments(postId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async loadPost(postId: string) {
|
|
|
+ const query = new Parse.Query('Post240709');
|
|
|
+ query.include('author');
|
|
|
+ try {
|
|
|
+ const post = await query.get(postId);
|
|
|
+ console.log(post);
|
|
|
+ this.post = {
|
|
|
+ id: post.id,
|
|
|
+ author: post.get('author').username,
|
|
|
+ authorAvatar: post.get('author').avatarUrl,
|
|
|
+ time: post.createdAt,
|
|
|
+ title: post.get('title'),
|
|
|
+ content: post.get('content'),
|
|
|
+ image: post.get('imageUrl'),
|
|
|
+ likes: post.get('likes') || 0
|
|
|
+ };
|
|
|
+ } catch (error) {
|
|
|
+ console.error('Error loading post:', error);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async loadComments(postId: string) {
|
|
|
+ const query = new Parse.Query('Comment240709');
|
|
|
+ query.equalTo('post', {
|
|
|
+ __type: 'Pointer',
|
|
|
+ className: 'Post240709',
|
|
|
+ objectId: postId
|
|
|
+ });
|
|
|
+ query.include('author');
|
|
|
+ query.ascending('createdAt');
|
|
|
+ try {
|
|
|
+ const comments = await query.find();
|
|
|
+ this.comments = await Promise.all(
|
|
|
+ comments.map(async (comment, index) => {
|
|
|
+ const repliesQuery = new Parse.Query('Comment240709');
|
|
|
+ repliesQuery.equalTo('parentComment', comment);
|
|
|
+ repliesQuery.include('author');
|
|
|
+ repliesQuery.ascending('createdAt');
|
|
|
+ const replies = await repliesQuery.find();
|
|
|
+ return {
|
|
|
+ id: comment.id,
|
|
|
+ author: comment.get('author').get("username"),
|
|
|
+ authorAvatar: comment.get('author').get("avatarUrl"),
|
|
|
+ content: comment.get('content'),
|
|
|
+ time: comment.createdAt,
|
|
|
+ likes: comment.get('likes'),
|
|
|
+ floor: index + 1,
|
|
|
+ replies: replies.map(reply => ({
|
|
|
+ id: reply.id,
|
|
|
+ author: reply.get('author').get("username"),
|
|
|
+ authorAvatar: reply.get('author').get("avatarUrl"),
|
|
|
+ content: reply.get('content'),
|
|
|
+ time: reply.createdAt,
|
|
|
+ likes: reply.get('likes')
|
|
|
+ }))
|
|
|
+ };
|
|
|
+ })
|
|
|
+ );
|
|
|
+ } catch (error) {
|
|
|
+ console.error('Error loading comments:', error);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
sortComments(event: any) {
|
|
@@ -42,4 +100,187 @@ export class PostDetailPage implements OnInit {
|
|
|
this.comments.sort((a, b) => new Date(b.time).getTime() - new Date(a.time).getTime()); // 时间倒序
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ async toggleLike(comment: any) {
|
|
|
+ comment.likes = comment.likes + 1; // 赞同数+1
|
|
|
+ const query = new Parse.Query('Comment240709');
|
|
|
+ try {
|
|
|
+ const parseComment = await query.get(comment.id);
|
|
|
+ parseComment.increment('likes');
|
|
|
+ await parseComment.save();
|
|
|
+ } catch (error) {
|
|
|
+ console.error('Error while updating like count:', error);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async togglePostLike() {
|
|
|
+ this.post.likes = this.post.likes + 1; // 赞同数+1
|
|
|
+ const query = new Parse.Query('Post240709');
|
|
|
+ try {
|
|
|
+ const parsePost = await query.get(this.post.id);
|
|
|
+ parsePost.increment('likes');
|
|
|
+ await parsePost.save();
|
|
|
+ } catch (error) {
|
|
|
+ console.error('Error while updating like count:', error);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async replyToComment(comment: any) {
|
|
|
+ const alert = await this.alertCtrl.create({
|
|
|
+ header: '回复',
|
|
|
+ inputs: [
|
|
|
+ {
|
|
|
+ name: 'reply',
|
|
|
+ type: 'text',
|
|
|
+ placeholder: '输入您的回复'
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ buttons: [
|
|
|
+ {
|
|
|
+ text: '取消',
|
|
|
+ role: 'cancel',
|
|
|
+ cssClass: 'secondary'
|
|
|
+ },
|
|
|
+ {
|
|
|
+ text: '回复',
|
|
|
+ handler: async (data) => {
|
|
|
+ await this.submitReply(comment.id, data.reply);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ]
|
|
|
+ });
|
|
|
+
|
|
|
+ await alert.present();
|
|
|
+ }
|
|
|
+
|
|
|
+ async replyToPost() {
|
|
|
+ const alert = await this.alertCtrl.create({
|
|
|
+ header: '回复帖子',
|
|
|
+ inputs: [
|
|
|
+ {
|
|
|
+ name: 'reply',
|
|
|
+ type: 'text',
|
|
|
+ placeholder: '输入您的回复'
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ buttons: [
|
|
|
+ {
|
|
|
+ text: '取消',
|
|
|
+ role: 'cancel',
|
|
|
+ cssClass: 'secondary'
|
|
|
+ },
|
|
|
+ {
|
|
|
+ text: '回复',
|
|
|
+ handler: async (data) => {
|
|
|
+ await this.submitPostReply(data.reply);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ]
|
|
|
+ });
|
|
|
+
|
|
|
+ await alert.present();
|
|
|
+ }
|
|
|
+
|
|
|
+ async submitReply(commentId: string, content: string) {
|
|
|
+ const currentUser = Parse.User.current();
|
|
|
+ if (!currentUser) {
|
|
|
+ console.error('No user logged in. Please log in to reply.');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const Comment240709 = Parse.Object.extend('Comment240709');
|
|
|
+ const comment = new Comment240709();
|
|
|
+
|
|
|
+ comment.set('author', currentUser);
|
|
|
+ comment.set('content', content);
|
|
|
+ comment.set('parentComment', {
|
|
|
+ __type: 'Pointer',
|
|
|
+ className: 'Comment240709',
|
|
|
+ objectId: commentId
|
|
|
+ });
|
|
|
+
|
|
|
+ try {
|
|
|
+ await comment.save();
|
|
|
+ console.log('Reply saved successfully.');
|
|
|
+ this.loadComments(this.post.id); // Reload comments to show the new reply
|
|
|
+ } catch (error) {
|
|
|
+ console.error('Error while saving reply:', error);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async submitPostReply(content: string) {
|
|
|
+ const currentUser = Parse.User.current();
|
|
|
+ if (!currentUser) {
|
|
|
+ console.error('No user logged in. Please log in to reply.');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const Comment240709 = Parse.Object.extend('Comment240709');
|
|
|
+ const comment = new Comment240709();
|
|
|
+
|
|
|
+ comment.set('author', currentUser);
|
|
|
+ comment.set('content', content);
|
|
|
+ comment.set('post', {
|
|
|
+ __type: 'Pointer',
|
|
|
+ className: 'Post240709',
|
|
|
+ objectId: this.post.id
|
|
|
+ });
|
|
|
+
|
|
|
+ try {
|
|
|
+ await comment.save();
|
|
|
+ console.log('Reply to post saved successfully.');
|
|
|
+ this.loadComments(this.post.id); // Reload comments to show the new reply
|
|
|
+ } catch (error) {
|
|
|
+ console.error('Error while saving reply:', error);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ sharePost() {
|
|
|
+ this.shareModal.present();
|
|
|
+ }
|
|
|
+
|
|
|
+ async generateTestComments() {
|
|
|
+ const currentUser = Parse.User.current();
|
|
|
+ if (!currentUser) {
|
|
|
+ console.error('No user logged in. Please log in to create test comments.');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const hotPhrases = [
|
|
|
+ '这个可以有',
|
|
|
+ '我觉得可以',
|
|
|
+ '前排围观',
|
|
|
+ '沙发',
|
|
|
+ '楼主威武',
|
|
|
+ '说得好',
|
|
|
+ '赞同楼上',
|
|
|
+ '有理有据',
|
|
|
+ '请继续',
|
|
|
+ '哈哈哈哈'
|
|
|
+ ];
|
|
|
+
|
|
|
+ const Comment240709 = Parse.Object.extend('Comment240709');
|
|
|
+
|
|
|
+ for (let i = 0; i < 5; i++) {
|
|
|
+ const comment = new Comment240709();
|
|
|
+ comment.set('author', currentUser);
|
|
|
+ comment.set('content', hotPhrases[Math.floor(Math.random() * hotPhrases.length)]);
|
|
|
+ comment.set('post', {
|
|
|
+ __type: 'Pointer',
|
|
|
+ className: 'Post240709',
|
|
|
+ objectId: this.post.id
|
|
|
+ });
|
|
|
+ comment.set('likes', Math.floor(Math.random() * 100));
|
|
|
+ comment.set('floor', this.comments.length + i + 1);
|
|
|
+
|
|
|
+ try {
|
|
|
+ await comment.save();
|
|
|
+ console.log('Test comment saved successfully.');
|
|
|
+ } catch (error) {
|
|
|
+ console.error('Error while saving test comment:', error);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ this.loadComments(this.post.id); // Reload comments to show the new test comments
|
|
|
+ }
|
|
|
}
|