page-lesson-detail.component.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { Component } from '@angular/core';
  2. import { ActivatedRoute } from '@angular/router';
  3. // 引入Parse第三方库
  4. import * as Parse from "parse"
  5. @Component({
  6. selector: 'app-page-lesson-detail',
  7. templateUrl: './page-lesson-detail.component.html',
  8. styleUrls: ['./page-lesson-detail.component.scss']
  9. })
  10. export class PageLessonDetailComponent {
  11. course: any = {}
  12. constructor(private route: ActivatedRoute) {
  13. this.route.queryParams.subscribe(params => {
  14. this.initPage(params["id"]);
  15. });
  16. }
  17. lessonPointer: any;//获取当前指针
  18. async getCourseById(id: string) {
  19. let query = new Parse.Query("HrmGift")
  20. query.include('courseAuthor')//查询指针
  21. this.course = await query.get(id);
  22. this.lessonPointer = {
  23. __type: 'Pointer',
  24. className: 'HrmGift',
  25. objectId: this.course.id
  26. };
  27. }
  28. //用户输入评论保存到数据库
  29. currentUser = Parse.User.current()
  30. commentContent = ''//用户评论,双向绑定commentCoutent实现保存该评论
  31. async saveComment() {
  32. let CourseComment = Parse.Object.extend('CourseComment')
  33. let courseComment = new CourseComment()
  34. courseComment.set('user', { __type: 'Pointer', className: '_User', objectId: this.currentUser?.id })
  35. courseComment.set('commentContent', this.commentContent)
  36. courseComment.set('lesson', this.lessonPointer); // 设置指向QwlMenu表的指针类型字段
  37. this.commentContent = '';
  38. return courseComment.save().then((saveObject: any) => {
  39. // 处理Promise成功的情况
  40. console.log(saveObject);
  41. //评论立即刷新到页面上
  42. this.getCommentData().then((commentList: Array<Parse.Object>) => {
  43. this.commentList = commentList;
  44. this.commentCount = this.commentList.length; // 更新评论数量
  45. });
  46. },
  47. (error: string) => {
  48. // 处理Promise失败的情况
  49. console.log("失败:" + error);
  50. })
  51. }
  52. // 评论数据加载相关函数
  53. commentCount: number = 0
  54. commentList: Array<Parse.Object> = []
  55. async initPage(id: string) {
  56. await this.getCourseById(id);
  57. this.commentList = await this.getCommentData()
  58. this.commentCount = this.commentList.length;
  59. }
  60. async getCommentData() {
  61. let query = new Parse.Query("CourseComment");
  62. query.equalTo("lesson", this.lessonPointer);
  63. query.descending('createdAt')
  64. let list = await query.find();
  65. return list
  66. }
  67. }