comment.js 7.7 KB

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