comment.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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.likeCount || 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. console.log('评论列表',this.data.commentList)
  60. })
  61. });
  62. },
  63. showMore: function showMore(e) {
  64. let index = e.currentTarget.dataset.index || index === 0 ? e.currentTarget.dataset.index : e.target.dataset.index;
  65. let str = `commentList[${index}].showControl`;
  66. this.setData({
  67. [str]: 10
  68. })
  69. console.log('showControl', this.data.commentList[index].showControl)
  70. },
  71. // 评论作品
  72. sendReply: function sendReply(e) {
  73. console.log('sendReply', e.detail.value)
  74. if (this.data.replyType === 'works') {
  75. let data = {
  76. columnId: this.data.commentId,
  77. colunmNames: 'what',
  78. detailDesc: e.detail.value,
  79. }
  80. this.replyWorks(data);
  81. }
  82. if (this.data.replyType === 'comment') {
  83. let data = {
  84. postsId: this.data.postId,
  85. content: e.detail.value
  86. }
  87. console.log(321232123123,data)
  88. this.replyComment(data);
  89. }
  90. },
  91. // 点赞评论
  92. likeComment: function (e) {
  93. console.log(e);
  94. let postId = e.currentTarget.dataset.id ? e.currentTarget.dataset.id : e.target.dataset.id;
  95. let index = e.currentTarget.dataset.index || e.currentTarget.dataset.index === 0 ? e.currentTarget.dataset.index : e.target.dataset.index;
  96. httpRequestApi.likeCommend(postId).success(res => {
  97. console.log('点赞点赞点赞',res.data);
  98. const str = `commentList[${index}].likes`;
  99. const strImg = `commentList[${index}].isLike`;
  100. if(res.data.data){
  101. this.setData({
  102. [str]: res.data.data,
  103. [strImg]: true
  104. })
  105. }
  106. });
  107. },
  108. // 回复作品
  109. replyWorks: function (data) {
  110. httpRequestApi.postReply(data).success(res => {
  111. console.log(res)
  112. this.setData({
  113. pageNo: 1,
  114. commentList: []
  115. }, () => {
  116. this.getReply(this.data.commentId);
  117. this.setData({
  118. inputValue: '',
  119. })
  120. })
  121. });
  122. },
  123. catchComment: function catchComment(e) {
  124. let postId = e.currentTarget.dataset.id ? e.currentTarget.dataset.id : e.target.dataset.id;
  125. let index = e.currentTarget.dataset.index || e.currentTarget.dataset.index === 0 ? e.currentTarget.dataset.index : e.target.dataset.index;
  126. this.setData({
  127. postId: postId,
  128. replyType: 'comment',
  129. ifGetFocus: true,
  130. postIndex: index
  131. })
  132. console.log(this.data.ifGetFocus)
  133. },
  134. // 回复评论
  135. replyComment: function (data) {
  136. httpRequestApi.postReplyComment(data).success(res => {
  137. this.setData({
  138. replyInfo: '',
  139. }, () => {
  140. this.getReplyInner(this.data.commentId);
  141. this.setData({
  142. inputValue: '',
  143. replyType: 'works',
  144. ifGetFocus: false
  145. })
  146. });
  147. });
  148. },
  149. // 发布楼中楼评论后渲染之前列表
  150. getReplyInner: function (columnId) {
  151. httpRequestApi.getReply(this.uid, columnId, 1, 10).success((res) => {
  152. const commentList = res.data.data.list;
  153. commentList.forEach((item, index) => {
  154. console.log('回复列表',index)
  155. console.log('回复列表',this.data.postIndex)
  156. if (index === this.data.postIndex) {
  157. let replyList = item.replyVOList;
  158. console.log('回复列表',replyList)
  159. let str = `commentList[${index}].replyList`;
  160. let strShow = `commentList[${index}].showControl`;
  161. let strReply = `commentList[${index}].replyCount`;
  162. this.setData({
  163. [str]: replyList,
  164. [strShow]: 10,
  165. [strReply]: replyList.length
  166. },()=>{
  167. console.log('刷新后列表',this.data.commentList)
  168. })
  169. return;
  170. };
  171. })
  172. });
  173. },
  174. }
  175. })