comment.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. isScroll: true
  29. },
  30. /**
  31. * 组件的方法列表
  32. */
  33. methods: {
  34. // 获取评论信息
  35. getReply: function (columnId) {
  36. httpRequestApi.getReply(this.uid, columnId, 1, 10).success((res) => {
  37. console.log('reply', res)
  38. const commentList = res.data.data.list;
  39. const commentNum = res.data.data.totalSize;
  40. console.log('评论数量', commentNum)
  41. commentList.forEach((item) => {
  42. const temp = {};
  43. temp.nickName = item.user.wechatName;
  44. temp.avatar = item.user.avatar;
  45. temp.uid = item.user.uid;
  46. temp.text = item.detailDesc;
  47. temp.id = item.id;
  48. temp.replyCount = item.replyCount;
  49. temp.time = formatDate(item.gmtCreated, 3);
  50. temp.likes = item.likeCount || 0;
  51. temp.isLike = item.isLike;
  52. temp.replyList = item.replyVOList;
  53. temp.showControl = 0;
  54. this.data.commentList.push(temp);
  55. });
  56. this.setData({
  57. commentList: this.data.commentList,
  58. commentNum: commentNum
  59. }, () => {
  60. console.log('评论列表', this.data.commentList)
  61. })
  62. });
  63. },
  64. showMore: function showMore(e) {
  65. let index = e.currentTarget.dataset.index || index === 0 ? e.currentTarget.dataset.index : e.target.dataset.index;
  66. let str = `commentList[${index}].showControl`;
  67. this.setData({
  68. [str]: 10
  69. })
  70. console.log('showControl', this.data.commentList[index].showControl)
  71. },
  72. textInput: function (e) {
  73. console.log('键入', e.detail.value)
  74. this.setData({
  75. inputValue: e.detail.value
  76. })
  77. },
  78. // 获取焦点事件
  79. bindfocus(e) {
  80. this.setData({
  81. isScroll: false
  82. })
  83. // this.triggerEvent('inputFocus');
  84. },
  85. // 失去焦点事件
  86. closeblur(e) {
  87. this.setData({
  88. isScroll: true
  89. })
  90. // this.triggerEvent('inputBlur');
  91. },
  92. // 评论作品
  93. sendReply: function sendReply(e) {
  94. console.log('sendReply', e.detail.value)
  95. if (this.data.replyType === 'works') {
  96. let data = {
  97. columnId: this.data.commentId,
  98. colunmNames: 'what',
  99. detailDesc: e.detail.value ? e.detail.value : this.data.inputValue,
  100. }
  101. this.replyWorks(data);
  102. }
  103. if (this.data.replyType === 'comment') {
  104. let data = {
  105. postsId: this.data.postId,
  106. content: e.detail.value ? e.detail.value : this.data.inputValue
  107. }
  108. console.log(321232123123, data)
  109. this.replyComment(data);
  110. }
  111. },
  112. // 点赞评论
  113. likeComment: function (e) {
  114. console.log(e);
  115. let postId = e.currentTarget.dataset.id ? e.currentTarget.dataset.id : e.target.dataset.id;
  116. let index = e.currentTarget.dataset.index || e.currentTarget.dataset.index === 0 ? e.currentTarget.dataset.index : e.target.dataset.index;
  117. httpRequestApi.likeCommend(postId).success(res => {
  118. console.log('点赞点赞点赞', res.data);
  119. const str = `commentList[${index}].likes`;
  120. const strImg = `commentList[${index}].isLike`;
  121. if (res.data.data) {
  122. this.setData({
  123. [str]: res.data.data,
  124. [strImg]: true
  125. })
  126. }
  127. if (res.data.count > 0) {
  128. this.setData({
  129. flowerNum: res.data.count
  130. })
  131. this.flowerAnimationHandler();
  132. }
  133. });
  134. },
  135. // 回复作品
  136. replyWorks: function (data) {
  137. httpRequestApi.postReply(data).success(res => {
  138. console.log(res)
  139. this.setData({
  140. pageNo: 1,
  141. commentList: []
  142. }, () => {
  143. this.getReply(this.data.commentId);
  144. this.setData({
  145. inputValue: '',
  146. })
  147. if (res.data.count > 0) {
  148. this.setData({
  149. flowerNum: res.data.count
  150. })
  151. this.flowerAnimationHandler();
  152. }
  153. })
  154. });
  155. },
  156. catchComment: function catchComment(e) {
  157. let postId = e.currentTarget.dataset.id ? e.currentTarget.dataset.id : e.target.dataset.id;
  158. let index = e.currentTarget.dataset.index || e.currentTarget.dataset.index === 0 ? e.currentTarget.dataset.index : e.target.dataset.index;
  159. this.setData({
  160. postId: postId,
  161. replyType: 'comment',
  162. ifGetFocus: true,
  163. postIndex: index
  164. })
  165. console.log(this.data.ifGetFocus)
  166. },
  167. // 回复评论
  168. replyComment: function (data) {
  169. httpRequestApi.postReplyComment(data).success(res => {
  170. this.setData({
  171. replyInfo: '',
  172. }, () => {
  173. this.getReplyInner(this.data.commentId);
  174. this.setData({
  175. inputValue: '',
  176. replyType: 'works',
  177. ifGetFocus: false
  178. })
  179. if (res.data.count > 0) {
  180. this.setData({
  181. flowerNum: res.data.count
  182. })
  183. this.flowerAnimationHandler();
  184. }
  185. });
  186. });
  187. },
  188. // 发布楼中楼评论后渲染之前列表
  189. getReplyInner: function (columnId) {
  190. httpRequestApi.getReply(this.uid, columnId, 1, 10).success((res) => {
  191. const commentList = res.data.data.list;
  192. commentList.forEach((item, index) => {
  193. console.log('回复列表', index)
  194. console.log('回复列表', this.data.postIndex)
  195. if (index === this.data.postIndex) {
  196. let replyList = item.replyVOList;
  197. console.log('回复列表', replyList)
  198. let str = `commentList[${index}].replyList`;
  199. let strShow = `commentList[${index}].showControl`;
  200. let strReply = `commentList[${index}].replyCount`;
  201. this.setData({
  202. [str]: replyList,
  203. [strShow]: 10,
  204. [strReply]: replyList.length
  205. }, () => {
  206. console.log('刷新后列表', this.data.commentList)
  207. })
  208. return;
  209. };
  210. })
  211. });
  212. },
  213. flowerAnimationHandler: function () {
  214. this.flowerBox = this.selectComponent("#flower-toast");
  215. console.log('this.flower', this.flowerBox)
  216. this.flowerBox.comeOut();
  217. },
  218. }
  219. })