comment.js 6.7 KB

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