comment.js 7.8 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. const commentList = res.data.data.list;
  39. const commentNum = res.data.data.totalSize;
  40. console.log('评论数量', commentNum)
  41. commentList.forEach((item) => {
  42. if (item.user) {
  43. const temp = {};
  44. temp.nickName = item.user.nickName || item.user.eid;
  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. });
  58. this.setData({
  59. commentList: this.data.commentList,
  60. commentNum: commentNum
  61. }, () => {
  62. console.log('评论列表', this.data.commentList)
  63. })
  64. });
  65. },
  66. showMore: function showMore(e) {
  67. let index = e.currentTarget.dataset.index || index === 0 ? e.currentTarget.dataset.index : e.target.dataset.index;
  68. let str = `commentList[${index}].showControl`;
  69. this.setData({
  70. [str]: 10
  71. })
  72. console.log('showControl', this.data.commentList[index].showControl)
  73. },
  74. textInput: function(e) {
  75. console.log('键入', e.detail.value)
  76. this.setData({
  77. inputValue: e.detail.value
  78. })
  79. },
  80. // 获取焦点事件
  81. bindfocus(e) {
  82. this.setData({
  83. isScroll: false
  84. })
  85. // this.triggerEvent('inputFocus');
  86. },
  87. // 失去焦点事件
  88. closeblur(e) {
  89. this.setData({
  90. isScroll: true
  91. })
  92. // this.triggerEvent('inputBlur');
  93. },
  94. // 评论作品
  95. sendReply: function sendReply(e) {
  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. }
  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. }
  152. })
  153. });
  154. },
  155. catchComment: function catchComment(e) {
  156. let postId = e.currentTarget.dataset.id ? e.currentTarget.dataset.id : e.target.dataset.id;
  157. let index = e.currentTarget.dataset.index || e.currentTarget.dataset.index === 0 ? e.currentTarget.dataset.index : e.target.dataset.index;
  158. this.setData({
  159. postId: postId,
  160. replyType: 'comment',
  161. ifGetFocus: true,
  162. postIndex: index
  163. })
  164. },
  165. // 回复评论
  166. replyComment: function(data) {
  167. httpRequestApi.postReplyComment(data).success(res => {
  168. this.setData({
  169. replyInfo: '',
  170. }, () => {
  171. this.getReplyInner(this.data.commentId);
  172. this.setData({
  173. inputValue: '',
  174. replyType: 'works',
  175. ifGetFocus: false
  176. })
  177. if (res.data.count > 0) {
  178. this.setData({
  179. flowerNum: res.data.count
  180. })
  181. }
  182. });
  183. });
  184. },
  185. // 发布楼中楼评论后渲染之前列表
  186. getReplyInner: function(columnId) {
  187. httpRequestApi.getReply(this.uid, columnId, 1, 10).success((res) => {
  188. const commentList = res.data.data.list;
  189. commentList.forEach((item, index) => {
  190. console.log('回复列表', index)
  191. console.log('回复列表', this.data.postIndex)
  192. if (index === this.data.postIndex) {
  193. let replyList = item.replyVOList;
  194. console.log('回复列表', replyList)
  195. let str = `commentList[${index}].replyList`;
  196. let strShow = `commentList[${index}].showControl`;
  197. let strReply = `commentList[${index}].replyCount`;
  198. this.setData({
  199. [str]: replyList,
  200. [strShow]: 10,
  201. [strReply]: replyList.length
  202. }, () => {
  203. console.log('刷新后列表', this.data.commentList)
  204. })
  205. return;
  206. };
  207. })
  208. });
  209. },
  210. }
  211. })