CommentSection.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <template>
  2. <view class="comment-section">
  3. <view class="section-header">
  4. <text class="fa fa-comments"></text>
  5. <text class="section-title">评论区</text>
  6. <text class="comment-count">({{ tableTotal }})</text>
  7. </view>
  8. <CComment ref="ccRef" :myInfo="myInfo" :userInfo="userInfo" :tableData="tableData"
  9. :tableTotal.sync="tableTotal" :deleteMode="deleteMode" @likeFun="likeFun" @replyFun="replyFun"
  10. @deleteFun="deleteFun"></CComment>
  11. <view class="comment-button" @tap="openComment">
  12. <text class="fa fa-pencil"></text>
  13. <text>发表新评论</text>
  14. </view>
  15. </view>
  16. </template>
  17. <script>
  18. import CComment from "@/components/cc-comment/cc-comment.vue";
  19. export default {
  20. name: 'CommentSection',
  21. components: {
  22. CComment
  23. },
  24. props: {
  25. myInfo: {
  26. type: Object,
  27. required: true
  28. },
  29. userInfo: {
  30. type: Object,
  31. required: true
  32. },
  33. articleId: {
  34. type: [Number, String],
  35. required: true
  36. },
  37. type: {
  38. type: String,
  39. default: 'work'
  40. }
  41. },
  42. data() {
  43. return {
  44. deleteMode: "all",
  45. tableTotal: 0,
  46. tableData: []
  47. }
  48. },
  49. created() {
  50. this.loadCommentData();
  51. },
  52. methods: {
  53. loadCommentData() {
  54. uni.request({
  55. url: this.$apiHost + '/Article/getcomments',
  56. data: {
  57. uuid: getApp().globalData.uuid,
  58. type: this.type,
  59. id: this.articleId,
  60. page: 1,
  61. limit: 10
  62. },
  63. header: {
  64. "content-type": "application/json",
  65. 'sign': getApp().globalData.headerSign
  66. },
  67. success: (res) => {
  68. console.log("评论列表:", res.data);
  69. if (res.data.success == "yes") {
  70. this.$set(this, 'tableData', res.data.list);
  71. this.tableTotal = res.data.total;
  72. console.log("tabddd", this.tableData)
  73. } else {
  74. uni.showToast({
  75. title: '获取评论列表失败',
  76. icon: 'none'
  77. });
  78. }
  79. },
  80. fail: (e) => {
  81. console.log("----e:", e);
  82. }
  83. });
  84. },
  85. openComment() {
  86. let ccRef = this.$refs["ccRef"];
  87. ccRef.newCommentFun();
  88. },
  89. likeFun({params}, callback) {
  90. console.log("likeFun", params);
  91. uni.request({
  92. url: this.$apiHost + '/Article/zanComment',
  93. data: {
  94. uuid: getApp().globalData.uuid,
  95. id: params.id
  96. },
  97. header: {
  98. "content-type": "application/json",
  99. 'sign': getApp().globalData.headerSign
  100. },
  101. success: (res) => {
  102. console.log("点赞结果:", res.data);
  103. if (res.data.success !== "yes") {
  104. callback(res); // 请求失败时重置点赞效果
  105. }
  106. },
  107. fail: (e) => {
  108. console.log("点赞失败:", e);
  109. callback(e); // 请求失败时重置点赞效果
  110. }
  111. });
  112. },
  113. replyFun({params}, callback) {
  114. console.log("replyFun", {
  115. uuid: getApp().globalData.uuid,
  116. type: this.type,
  117. article_id: this.articleId,
  118. content: params.user_content,
  119. parent_id: params.parent_id || 0,
  120. reply_id: params.reply_id || 0,
  121. reply_name: params.reply_name || ''
  122. });
  123. uni.request({
  124. url: this.$apiHost + '/Article/newComment',
  125. data: {
  126. uuid: getApp().globalData.uuid,
  127. type: this.type,
  128. article_id: this.articleId,
  129. content: params.user_content,
  130. parent_id: params.parent_id || 0,
  131. reply_id: params.reply_id || 0,
  132. reply_name: params.reply_name || ''
  133. },
  134. header: {
  135. "content-type": "application/json",
  136. 'sign': getApp().globalData.headerSign
  137. },
  138. success: (res) => {
  139. console.log("评论结果:", res.data);
  140. if (res.data.success === "yes") {
  141. callback(res.data);
  142. this.loadCommentData(); // 重新加载评论列表
  143. }
  144. },
  145. fail: (e) => {
  146. console.log("评论失败:", e);
  147. uni.showToast({
  148. title: '评论失败,请重试',
  149. icon: 'none'
  150. });
  151. }
  152. });
  153. },
  154. deleteFun({params, mode}, callback) {
  155. console.log("deleteFun", {params, mode});
  156. const idsString = Array.isArray(params) ? params.join(',') : params.toString();
  157. console.log("删除评论", idsString, mode)
  158. uni.request({
  159. url: this.$apiHost + '/Article/delComment',
  160. data: {
  161. uuid: getApp().globalData.uuid,
  162. ids: idsString,
  163. mode: mode
  164. },
  165. header: {
  166. "content-type": "application/json",
  167. 'sign': getApp().globalData.headerSign
  168. },
  169. success: (res) => {
  170. console.log("删除结果:", res.data);
  171. if (res.data.success === "yes") {
  172. callback(res);
  173. this.loadCommentData(); // 重新加载评论列表
  174. }
  175. },
  176. fail: (e) => {
  177. console.log("删除失败:", e);
  178. uni.showToast({
  179. title: '删除失败,请重试',
  180. icon: 'none'
  181. });
  182. }
  183. });
  184. }
  185. }
  186. }
  187. </script>
  188. <style lang="scss">
  189. .comment-section {
  190. background-color: #fff;
  191. padding: 20rpx;
  192. margin-top: 20rpx;
  193. border-radius: 12rpx;
  194. .section-header {
  195. display: flex;
  196. align-items: center;
  197. margin-bottom: 20rpx;
  198. .fa {
  199. font-size: 36rpx;
  200. color: #666;
  201. margin-right: 10rpx;
  202. }
  203. .section-title {
  204. font-size: 32rpx;
  205. font-weight: bold;
  206. color: #333;
  207. }
  208. .comment-count {
  209. font-size: 28rpx;
  210. color: #999;
  211. margin-left: 10rpx;
  212. }
  213. }
  214. .comment-button {
  215. display: flex;
  216. align-items: center;
  217. justify-content: center;
  218. height: 80rpx;
  219. margin-top: 20rpx;
  220. background-color: #f8f8f8;
  221. border-radius: 40rpx;
  222. cursor: pointer;
  223. .fa {
  224. font-size: 32rpx;
  225. color: #666;
  226. margin-right: 10rpx;
  227. }
  228. text {
  229. font-size: 28rpx;
  230. color: #666;
  231. }
  232. &:active {
  233. background-color: #f0f0f0;
  234. }
  235. }
  236. }
  237. </style>