|
@@ -46,7 +46,37 @@ export class PostDetailComponent implements OnInit {
|
|
await this.loadComments(); // 组件初始化时加载评论
|
|
await this.loadComments(); // 组件初始化时加载评论
|
|
}
|
|
}
|
|
|
|
|
|
- toggleFollow() {
|
|
|
|
|
|
+ //关注
|
|
|
|
+ async toggleFollow() {
|
|
|
|
+ const user=new CloudUser();
|
|
|
|
+ const userId = user.toPointer(); // 获取当前用户ID
|
|
|
|
+ if(user){
|
|
|
|
+ if (this.isFollowed ) {
|
|
|
|
+ // 如果已经关注,删除用户的关注记录
|
|
|
|
+ const query = new CloudQuery('attention'); // 创建查询对象
|
|
|
|
+ query.equalTo('attentionID', this.post.get('UserID').objectId); // 根据帖子作者ID查询
|
|
|
|
+ query.equalTo('UserID', userId); // 根据用户ID查询
|
|
|
|
+ const attentionRecord = await query.first(); // 获取关注记录对象
|
|
|
|
+ //console.log(likeRecord);
|
|
|
|
+ if(attentionRecord){
|
|
|
|
+ await attentionRecord.destroy().then(() => {
|
|
|
|
+ console.log('关注记录已删除:');
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+// 创建新的关注对象
|
|
|
|
+const attention = new CloudObject("attention");
|
|
|
|
+// 设置点赞记录的字段
|
|
|
|
+await attention.set({
|
|
|
|
+ UserID: new CloudUser().toPointer(), // 指向当前用户
|
|
|
|
+ attentionID: { "__type": "Pointer", "className": "_User", "objectId":this.post.get('UserID').objectId }, // 关注对象指针
|
|
|
|
+});
|
|
|
|
+// 保存关注记录
|
|
|
|
+await attention.save().then(() => {
|
|
|
|
+console.log('关注记录已保存:');
|
|
|
|
+})
|
|
|
|
+}
|
|
|
|
+ }
|
|
this.isFollowed = !this.isFollowed; // 切换关注状态
|
|
this.isFollowed = !this.isFollowed; // 切换关注状态
|
|
}
|
|
}
|
|
/**
|
|
/**
|
|
@@ -58,7 +88,27 @@ export class PostDetailComponent implements OnInit {
|
|
query.equalTo("objectId", this.postId);
|
|
query.equalTo("objectId", this.postId);
|
|
let result = await query.find();
|
|
let result = await query.find();
|
|
this.post = result[0];
|
|
this.post = result[0];
|
|
- console.log(this.post);
|
|
|
|
|
|
+ // 判断用户有没有点赞过该帖子
|
|
|
|
+ const Query = new CloudQuery('postLikesRecord'); // 创建查询对象
|
|
|
|
+ Query.equalTo('UserID', new CloudUser().toPointer()); // 根据用户ID查询
|
|
|
|
+ Query.equalTo('postID', this.post.toPointer()); // 根据帖子指针查询
|
|
|
|
+ const likeRecord = await Query.first(); // 获取点赞记录
|
|
|
|
+ if (likeRecord) {
|
|
|
|
+ this.post.postisLiked = true; // 如果存在点赞记录,则帖子被当前用户点赞过
|
|
|
|
+ } else {
|
|
|
|
+ this.post.postisLiked = false;
|
|
|
|
+ }
|
|
|
|
+ //console.log(this.post.get('UserID').objectId);
|
|
|
|
+ // 判断用户有没有关注过该帖子发布者,显示相应的状态
|
|
|
|
+ const query1 = new CloudQuery('attention'); // 创建查询对象
|
|
|
|
+ query1.equalTo('attentionID', this.post.get('UserID').objectId); // 根据帖子作者ID查询
|
|
|
|
+ query1.equalTo('UserID', new CloudUser().toPointer()); // 根据用户ID查询
|
|
|
|
+ const attentionRecord = await query1.first(); // 获取关注记录对象
|
|
|
|
+ if(attentionRecord){
|
|
|
|
+ this.isFollowed = true; // 如果存在关注记录,则帖子发布者被当前用户关注过
|
|
|
|
+ }else{
|
|
|
|
+ this.isFollowed = false; // 如果不存在关注记录,则帖子发布者未被当前用户关注过
|
|
|
|
+ }
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
@@ -70,8 +120,130 @@ export class PostDetailComponent implements OnInit {
|
|
this.comments = await query.find(); // 获取评论
|
|
this.comments = await query.find(); // 获取评论
|
|
console.log('加载的评论:', this.comments);
|
|
console.log('加载的评论:', this.comments);
|
|
|
|
|
|
|
|
+ for (const comment of this.comments) {
|
|
|
|
+ // 判断用户有没有点赞过该评论
|
|
|
|
+ const Query = new CloudQuery('commentLikesRecord'); // 创建查询对象
|
|
|
|
+ Query.equalTo('UserID', new CloudUser().toPointer()); // 根据用户ID查询
|
|
|
|
+ Query.equalTo('commentID', comment.toPointer()); // 根据评论指针查询
|
|
|
|
+ const likeRecord = await Query.first(); // 获取点赞记录
|
|
|
|
+ if (likeRecord) {
|
|
|
|
+ comment.commentisLiked = true; // 如果存在点赞记录,则该评论被当前用户点赞过
|
|
|
|
+ } else { // 如果不存在点赞记录,则该评论未被当前用户点赞过
|
|
|
|
+ comment.commentisLiked = false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 切换评论点赞状态并更新数据库
|
|
|
|
+async toggleLike(comment: any) {
|
|
|
|
+ comment.commentisLiked = !comment.commentisLiked; // 切换当前评论的点赞状态
|
|
|
|
+ const commentId = comment.id; // 获取评论ID
|
|
|
|
+ const userId = new CloudUser().toPointer() // 获取当前用户ID
|
|
|
|
+
|
|
|
|
+ if (comment.commentisLiked) { // 点赞
|
|
|
|
+ comment.data.like += 1; // 增加点赞数
|
|
|
|
+ await this.updateCommentLikeCount(commentId, 1,'comment'); // 更新数据库中的点赞数
|
|
|
|
+ await this.saveCommentLikeRecord( commentId,"commentLikesRecord",true); // 保存点赞记录
|
|
|
|
+ } else {
|
|
|
|
+ // 取消点赞
|
|
|
|
+ comment.data.like -= 1; // 减少点赞数
|
|
|
|
+ await this.updateCommentLikeCount(commentId, -1,'comment'); // 更新数据库中的点赞数
|
|
|
|
+ await this.removeCommentLikeRecord(userId, commentId,'commentLikesRecord',true); // 删除点赞记录
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+// 切换帖子点赞状态并更新数据库
|
|
|
|
+async toLike() {
|
|
|
|
+ this.post.postisLiked = !this.post.postisLiked; // 切换当前帖子的点赞状态
|
|
|
|
+ const postId = this.post.id; // 获取帖子ID
|
|
|
|
+ const userId = new CloudUser().toPointer() // 获取当前用户ID
|
|
|
|
+
|
|
|
|
+ if (this.post.postisLiked) { // 点赞
|
|
|
|
+ this.post.data.like += 1; // 增加点赞数
|
|
|
|
+ await this.updateCommentLikeCount(postId, 1,'post'); // 更新数据库中的点赞数
|
|
|
|
+ await this.saveCommentLikeRecord( postId,"postLikesRecord",false); // 保存点赞记录
|
|
|
|
+ } else {
|
|
|
|
+ // 取消点赞
|
|
|
|
+ this.post.data.like -= 1; // 减少点赞数
|
|
|
|
+ await this.updateCommentLikeCount(postId, -1,'post'); // 更新数据库中的点赞数
|
|
|
|
+ await this.removeCommentLikeRecord(userId, postId,'postLikesRecord',false); // 删除点赞记录
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+// 保存点赞记录
|
|
|
|
+async saveCommentLikeRecord( Id:any,RecordTable:any,flag:boolean){
|
|
|
|
+ // 创建新的点赞对象
|
|
|
|
+ const likeRecord = new CloudObject(RecordTable);
|
|
|
|
+ const user = new CloudUser();
|
|
|
|
+ const currentUser = user.current(); // 获取当前用户信息
|
|
|
|
+if (currentUser!=null) {
|
|
|
|
+// 设置点赞记录的字段
|
|
|
|
+if(flag){//如果为true则保存评论点赞记录
|
|
|
|
+await likeRecord.set({
|
|
|
|
+ UserID: new CloudUser().toPointer(), // 指向当前用户
|
|
|
|
+ commentID: { "__type": "Pointer", "className": "comment", "objectId": Id }, // 评论指针
|
|
|
|
+
|
|
|
|
+});
|
|
|
|
+}
|
|
|
|
+else{ //否则保存帖子点赞记录
|
|
|
|
+ await likeRecord.set({
|
|
|
|
+ UserID: new CloudUser().toPointer(), // 指向当前用户
|
|
|
|
+ postID: { "__type": "Pointer", "className": "post", "objectId":Id }, // 帖子指针
|
|
|
|
+
|
|
|
|
+ });
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+console.log(likeRecord);
|
|
|
|
+
|
|
|
|
+// 保存点赞记录
|
|
|
|
+await likeRecord.save().then(() => {
|
|
|
|
+ console.log('点赞记录已保存:');
|
|
|
|
+})
|
|
|
|
+}
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// 更新评论点赞数
|
|
|
|
+async updateCommentLikeCount(Id:any, count:number,Table:any){
|
|
|
|
+ const query = new CloudQuery(Table); // 创建查询对象
|
|
|
|
+ query.equalTo('objectId', Id); // 根据帖子ID查询
|
|
|
|
+ //console.log(postId);
|
|
|
|
+ const comment = await query.first(); // 获取对象
|
|
|
|
+ //console.log(post);
|
|
|
|
+ if(comment){
|
|
|
|
+ comment.data['like'] += count; // 更新点赞数
|
|
|
|
+ comment.id=Id;
|
|
|
|
+ await comment.save().then(() => {
|
|
|
|
+ console.log('评论点赞数已更新:');
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+ // 删除点赞记录
|
|
|
|
+ async removeCommentLikeRecord(userId:any, Id:any,Table:any,flag:boolean){
|
|
|
|
+ const query = new CloudQuery(Table); // 创建查询对象
|
|
|
|
+ if(flag){
|
|
|
|
+ query.equalTo('commentID', Id); // 根据评论ID查询
|
|
|
|
+ }
|
|
|
|
+ else{
|
|
|
|
+ query.equalTo('postID', Id); // 根据帖子ID查询
|
|
|
|
+ }
|
|
|
|
+ query.equalTo('UserID', userId); // 根据用户ID查询
|
|
|
|
+ const likeRecord = await query.first(); // 获取点赞记录对象
|
|
|
|
+ //console.log(likeRecord);
|
|
|
|
+ if(likeRecord){
|
|
|
|
+ await likeRecord.destroy().then(() => {
|
|
|
|
+ console.log('点赞记录已删除:');
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
|
|
tocomment() {
|
|
tocomment() {
|
|
this.isfooter = false; // 切换底部栏的显示样式
|
|
this.isfooter = false; // 切换底部栏的显示样式
|
|
@@ -94,7 +266,7 @@ export class PostDetailComponent implements OnInit {
|
|
UserID: new CloudUser().toPointer(), // 指向当前用户
|
|
UserID: new CloudUser().toPointer(), // 指向当前用户
|
|
postID: { "__type": "Pointer", "className": "post", "objectId": this.post.id }, // 帖子指针
|
|
postID: { "__type": "Pointer", "className": "post", "objectId": this.post.id }, // 帖子指针
|
|
content: this.newComment.trim(), // 评论内容
|
|
content: this.newComment.trim(), // 评论内容
|
|
- heart: 0 // 初始点赞数为0
|
|
|
|
|
|
+ like: 0 // 初始点赞数为0
|
|
});
|
|
});
|
|
|
|
|
|
// 保存评论
|
|
// 保存评论
|