comment-community.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <template>
  2. <view class="comment-eg">
  3. <!-- 确认删除 -->
  4. <hb-comment ref="hbComment" @add="add" @del="del" @like="like" @focusOn="focusOn" :deleteTip="$t('common.delete1')"
  5. :cmData="commentData" v-if="commentData"></hb-comment>
  6. </view>
  7. </template>
  8. <script>
  9. export default {
  10. name: 'comment-eg',
  11. props: {
  12. articleId: {
  13. type: String,
  14. default: () => {
  15. return null;
  16. }
  17. }
  18. },
  19. watch: {
  20. articleId: {
  21. handler: function(newVal, oldVal) {
  22. if (newVal) {
  23. this.getComment(newVal);
  24. }
  25. },
  26. immediate: true
  27. }
  28. },
  29. data() {
  30. return {
  31. "commentData": null,
  32. "reqFlag": false // 请求标志,防止重复操作,true表示请求中
  33. }
  34. },
  35. methods: {
  36. showCom() {
  37. this.$refs.hbComment.commentInput()
  38. },
  39. // 登录校验
  40. checkLogin() {
  41. // TODO 此处填写登录校验逻辑
  42. if (true) {
  43. return true;
  44. } else {
  45. uni.showModal({
  46. title: '提示',
  47. content: '请先登录',
  48. confirmText: '前往登录',
  49. success: function(res) {
  50. if (res.confirm) {
  51. uni.redirectTo({
  52. url: '/pages/login/login'
  53. });
  54. }
  55. }
  56. });
  57. return false;
  58. }
  59. },
  60. // 输入框聚焦
  61. focusOn() {
  62. if (this.$store.state.isLogin != 1) {
  63. this.$toUrl('/pages/login/login')
  64. return
  65. }
  66. },
  67. // 获取评论
  68. getComment(articleId) {
  69. console.log('--=');
  70. // TODO 接入真实接口
  71. // this.$u.api.commentList(articleId).then(res => {
  72. // this.commentData = {
  73. // "readNumer": res.readNumer,
  74. // "commentSize": res.commentList.length,
  75. // "comment": this.getTree(res.commentList)
  76. // };
  77. // }).catch(res => {})
  78. uni.$u.http.get('/api/circle/info', {params:{id:articleId}}).then(res => {
  79. this.commentData = {
  80. "readNumer": 0,
  81. "commentSize": res.list.total,
  82. "comment": this.getTree(res.list.data)
  83. };
  84. })
  85. },
  86. // 新增评论
  87. add(req) {
  88. if (this.$store.state.isLogin != 1) {
  89. this.$toUrl('/pages/login/login')
  90. return
  91. }
  92. if (this.reqFlag) {
  93. uni.showToast({
  94. title: this.$t('common.frequent'),
  95. duration: 1000
  96. });
  97. return
  98. }
  99. this.reqFlag = true;
  100. console.log(req);
  101. // TODO 接入真实接口
  102. let params = {
  103. "id": this.articleId,
  104. "cid": req.pId,
  105. "content": req.content
  106. }
  107. uni.$u.http.post('api/Circleuser/comment', params).then(res => {
  108. this.getComment(this.articleId);
  109. this.$refs.hbComment.addComplete();
  110. this.reqFlag = false;
  111. }).catch(res=>{
  112. this.reqFlag = false;
  113. })
  114. },
  115. // 点赞评论
  116. like(commentId) {
  117. if (this.$store.state.isLogin != 1) {
  118. this.$toUrl('/pages/login/login')
  119. return
  120. }
  121. if (this.reqFlag) {
  122. uni.showToast({
  123. title: this.$t('common.frequent'),
  124. duration: 1000
  125. });
  126. return
  127. }
  128. this.reqFlag = true;
  129. uni.$u.http.post('api/Circleuser/commentLikes', {id:commentId}).then(res => {
  130. this.$refs.hbComment.likeComplete(commentId);
  131. this.reqFlag = false;
  132. }).catch(res=>{
  133. this.reqFlag = false;
  134. })
  135. // TODO 接入真实接口
  136. // this.$u.api.commentLike(commentId).then(res => {
  137. // this.$refs.hbComment.likeComplete(commentId);
  138. // this.reqFlag = false;
  139. // }).catch(res => {
  140. // this.reqFlag = false;
  141. // })
  142. // 下边假装请求成功
  143. // this.reqFlag = false;
  144. // this.$refs.hbComment.likeComplete(commentId);
  145. },
  146. // 删除评论
  147. del(commentId) {
  148. if (this.$store.state.isLogin != 1) {
  149. this.$toUrl('/pages/login/login')
  150. return
  151. }
  152. if (this.reqFlag) {
  153. uni.showToast({
  154. title: this.$t('common.frequent'),
  155. duration: 1000
  156. });
  157. return
  158. }
  159. this.reqFlag = true;
  160. // TODO 接入真实接口
  161. // this.$u.api.commentDelete(commentId).then(res => {
  162. // this.reqFlag = false;
  163. // this.$refs.hbComment.deleteComplete(commentId);
  164. // }).catch(res => {
  165. // this.reqFlag = false;
  166. // })
  167. // 下边假装请求成功
  168. this.reqFlag = false;
  169. this.$refs.hbComment.deleteComplete(commentId);
  170. },
  171. // 列表按照父子转换成树
  172. getTree(data) {
  173. let result = [];
  174. let map = {};
  175. data.forEach(item => {
  176. map[item.id] = item;
  177. });
  178. data.forEach(item => {
  179. let parent = map[item.parentId];
  180. if (parent) {
  181. (parent.children || (parent.children = [])).push(item);
  182. } else {
  183. result.push(item);
  184. }
  185. });
  186. return result;
  187. }
  188. }
  189. };
  190. </script>
  191. <style>
  192. </style>