comment.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // component/comment.js
  2. import httpRequestApi from '../../utils/APIClient';
  3. import {
  4. formatDate
  5. } from '../../utils/util';
  6. Component({
  7. /**
  8. * 组件的属性列表
  9. */
  10. properties: {
  11. commentId: {
  12. type: Number,
  13. observer: function observer(id) {
  14. this.getReply(id)
  15. }
  16. }
  17. },
  18. /**
  19. * 组件的初始数据
  20. */
  21. data: {
  22. inputValue: '',
  23. showControl: 0,
  24. commentNum: 0,
  25. commentList: [],
  26. ifGetFocus: false,
  27. replyType: 'works' // 回复类型,works是回复作品,comment是回复评论
  28. },
  29. /**
  30. * 组件的方法列表
  31. */
  32. methods: {
  33. // 获取评论信息
  34. getReply: function (columnId) {
  35. httpRequestApi.getReply(this.uid, columnId, 1, 10).success((res) => {
  36. console.log('reply', res)
  37. const commentList = res.data.data.list;
  38. const commentNum = res.data.data.totalSize;
  39. console.log('评论数量', commentNum)
  40. commentList.forEach((item) => {
  41. const temp = {};
  42. temp.nickName = item.user.wechatName;
  43. temp.avatar = item.user.avatar;
  44. temp.uid = item.user.uid;
  45. temp.text = item.detailDesc;
  46. temp.id = item.id;
  47. temp.replyCount = item.replyCount;
  48. temp.time = formatDate(item.gmtCreated, 3);
  49. temp.likes = item.postsAttributeInfo.favors || 0;
  50. temp.isLike = item.isLike;
  51. temp.replyList = item.replyVOList;
  52. temp.showControl = 0;
  53. this.data.commentList.push(temp);
  54. });
  55. this.setData({
  56. commentList: this.data.commentList,
  57. commentNum: commentNum
  58. })
  59. });
  60. },
  61. showMore: function showMore(e) {
  62. let index = e.currentTarget.dataset.index || index === 0 ? e.currentTarget.dataset.index : e.target.dataset.index;
  63. let str = `commentList[${index}].showControl`;
  64. this.setData({
  65. [str]: 10
  66. })
  67. console.log('showControl', this.data.commentList[index].showControl)
  68. },
  69. // 评论作品
  70. sendReply: function sendReply(e) {
  71. console.log('sendReply', e.detail.value)
  72. if (this.data.replyType === 'works') {
  73. let data = {
  74. columnId: this.data.commentId,
  75. colunmNames: 'what',
  76. detailDesc: e.detail.value,
  77. }
  78. this.replyWorks(data);
  79. }
  80. if (this.data.replyType === 'comment') {
  81. let data = {
  82. postsId: this.data.postId,
  83. content: e.detail.value
  84. }
  85. this.replyComment(data);
  86. }
  87. },
  88. // 点赞评论
  89. likeComment: function (e) {
  90. console.log(e);
  91. let postId = e.currentTarget.dataset.id ? e.currentTarget.dataset.id : e.target.dataset.id;
  92. let index = e.currentTarget.dataset.index || index === 0 ? e.currentTarget.dataset.index : e.target.dataset.index;
  93. httpRequestApi.likeCommend(postId).success(res => {
  94. console.log(res);
  95. const str = `commentList[${index}].likes`;
  96. const strImg = `commentList[${index}].isLike`;
  97. this.setData({
  98. [str]: res.data.data.favors,
  99. [strImg]: true
  100. })
  101. });
  102. },
  103. // 回复作品
  104. replyWorks: function (data) {
  105. httpRequestApi.postReply(data).success(res => {
  106. console.log(res)
  107. this.setData({
  108. pageNo: 1,
  109. commentList: []
  110. }, () => {
  111. this.getReply(this.data.commentId);
  112. this.setData({
  113. inputValue: '',
  114. })
  115. })
  116. });
  117. },
  118. catchComment: function catchComment(e) {
  119. let postId = e.currentTarget.dataset.id ? e.currentTarget.dataset.id : e.target.dataset.id;
  120. let index = e.currentTarget.dataset.index || index === 0 ? e.currentTarget.dataset.index : e.target.dataset.index;
  121. this.setData({
  122. postId: postId,
  123. replyType: 'comment',
  124. ifGetFocus: true,
  125. postIndex: index
  126. })
  127. console.log(this.data.ifGetFocus)
  128. },
  129. // 回复评论
  130. replyComment: function (data) {
  131. httpRequestApi.postReplyComment(data).success(res => {
  132. this.setData({
  133. replyInfo: '',
  134. }, () => {
  135. this.getReply(this.data.commentId);
  136. this.setData({
  137. inputValue: '',
  138. replyType: 'works',
  139. ifGetFocus: false
  140. })
  141. });
  142. });
  143. },
  144. // 发布楼中楼评论后渲染之前列表
  145. getReplyInner: function (columnId) {
  146. httpRequestApi.getReply(this.uid, columnId, 1, 10).success((res) => {
  147. const commentList = res.data.data.list;
  148. commentList.forEach((item, index) => {
  149. if (index === this.data.postIndex) {
  150. let replyList = item.replyVOList;
  151. let str = `commentList[${index}].replyVOList`;
  152. this.setData({
  153. [str]: replyList
  154. })
  155. return;
  156. };
  157. })
  158. });
  159. },
  160. }
  161. })