comment.js 5.6 KB

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