comment.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. if (res.data.count > 0) {
  113. this.setData({
  114. flowerNum: res.data.count
  115. })
  116. this.flowerAnimationHandler();
  117. }
  118. });
  119. },
  120. // 回复作品
  121. replyWorks: function (data) {
  122. httpRequestApi.postReply(data).success(res => {
  123. console.log(res)
  124. this.setData({
  125. pageNo: 1,
  126. commentList: []
  127. }, () => {
  128. this.getReply(this.data.commentId);
  129. this.setData({
  130. inputValue: '',
  131. })
  132. if (res.data.count > 0) {
  133. this.setData({
  134. flowerNum: res.data.count
  135. })
  136. this.flowerAnimationHandler();
  137. }
  138. })
  139. });
  140. },
  141. catchComment: function catchComment(e) {
  142. let postId = e.currentTarget.dataset.id ? e.currentTarget.dataset.id : e.target.dataset.id;
  143. let index = e.currentTarget.dataset.index || e.currentTarget.dataset.index === 0 ? e.currentTarget.dataset.index : e.target.dataset.index;
  144. this.setData({
  145. postId: postId,
  146. replyType: 'comment',
  147. ifGetFocus: true,
  148. postIndex: index
  149. })
  150. console.log(this.data.ifGetFocus)
  151. },
  152. // 回复评论
  153. replyComment: function (data) {
  154. httpRequestApi.postReplyComment(data).success(res => {
  155. this.setData({
  156. replyInfo: '',
  157. }, () => {
  158. this.getReplyInner(this.data.commentId);
  159. this.setData({
  160. inputValue: '',
  161. replyType: 'works',
  162. ifGetFocus: false
  163. })
  164. if (res.data.count > 0) {
  165. this.setData({
  166. flowerNum: res.data.count
  167. })
  168. this.flowerAnimationHandler();
  169. }
  170. });
  171. });
  172. },
  173. // 发布楼中楼评论后渲染之前列表
  174. getReplyInner: function (columnId) {
  175. httpRequestApi.getReply(this.uid, columnId, 1, 10).success((res) => {
  176. const commentList = res.data.data.list;
  177. commentList.forEach((item, index) => {
  178. console.log('回复列表', index)
  179. console.log('回复列表', this.data.postIndex)
  180. if (index === this.data.postIndex) {
  181. let replyList = item.replyVOList;
  182. console.log('回复列表', replyList)
  183. let str = `commentList[${index}].replyList`;
  184. let strShow = `commentList[${index}].showControl`;
  185. let strReply = `commentList[${index}].replyCount`;
  186. this.setData({
  187. [str]: replyList,
  188. [strShow]: 10,
  189. [strReply]: replyList.length
  190. }, () => {
  191. console.log('刷新后列表', this.data.commentList)
  192. })
  193. return;
  194. };
  195. })
  196. });
  197. },
  198. flowerAnimationHandler: function () {
  199. this.flowerBox = this.selectComponent("#flower-toast");
  200. console.log('this.flower', this.flowerBox)
  201. this.flowerBox.comeOut();
  202. },
  203. }
  204. })