ForumComment.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\model\api;
  4. use library\basic\BaseModel;
  5. use think\Model;
  6. use think\facade\Db;
  7. /**
  8. * 论坛评论模型
  9. * @mixin \think\Model
  10. */
  11. class ForumComment extends BaseModel
  12. {
  13. protected $pk = 'id';
  14. protected $table = 'forum_comment';
  15. /**
  16. * 获取评论列表
  17. * @param int $postId 帖子ID
  18. * @param int $page 页码
  19. * @param int $pageSize 每页数量
  20. * @return array
  21. */
  22. public function getList($postId, $page = 1, $pageSize = 20)
  23. {
  24. $pageSize = $pageSize > 50 ? 50 : (int)$pageSize;
  25. $page = $page <= 0 ? 1 : (int)$page;
  26. $where = [
  27. ['post_id', '=', $postId],
  28. ['status', '=', 1]
  29. ];
  30. $total = $this->where($where)->count();
  31. $list = [];
  32. if ($total > 0) {
  33. $list = $this->alias('c')
  34. ->field('c.*, u.nickname as author_name, u.avatar as author_avatar')
  35. ->leftJoin('user u', 'u.uid = c.uid')
  36. ->where($where)
  37. ->order('c.create_time', 'asc')
  38. ->page($page, $pageSize)
  39. ->select();
  40. if (!empty($list)) {
  41. $list = $list->toArray();
  42. foreach ($list as &$item) {
  43. if (!empty($item['create_time'])) {
  44. $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
  45. }
  46. }
  47. }
  48. }
  49. return [
  50. 'list' => $list,
  51. 'total' => $total,
  52. 'page' => $page,
  53. 'pageSize' => $pageSize
  54. ];
  55. }
  56. /**
  57. * 添加评论
  58. * @param int $uid 用户ID
  59. * @param int $postId 帖子ID
  60. * @param string $content 评论内容
  61. * @return array [success, message, data]
  62. */
  63. public function addComment($uid, $postId, $content)
  64. {
  65. try {
  66. self::beginTrans();
  67. // 检查帖子是否存在且状态正常
  68. $post = Db::name('forum_post')->where('id', $postId)->where('status', 1)->find();
  69. if (!$post) {
  70. self::rollbackTrans();
  71. return [0, '帖子不存在或已被禁用'];
  72. }
  73. $insertData = [
  74. 'post_id' => $postId,
  75. 'uid' => $uid,
  76. 'content' => trim($content),
  77. 'status' => 1,
  78. 'create_time' => time(),
  79. 'update_time' => time(),
  80. ];
  81. $result = $this->insert($insertData);
  82. if (!$result) {
  83. self::rollbackTrans();
  84. return [0, '评论失败'];
  85. }
  86. // 更新帖子评论数
  87. Db::name('forum_post')->where('id', $postId)->inc('comment_count')->update();
  88. self::commitTrans();
  89. return [1, '评论成功', ['post_id' => $postId]];
  90. } catch (\Exception $e) {
  91. self::rollbackTrans();
  92. return [0, '评论失败:' . $e->getMessage()];
  93. }
  94. }
  95. /**
  96. * 删除评论
  97. * @param int $id 评论ID
  98. * @param int $uid 用户ID
  99. * @return array [success, message]
  100. */
  101. public function deleteComment($id, $uid)
  102. {
  103. try {
  104. self::beginTrans();
  105. $comment = $this->where('id', $id)->where('uid', $uid)->find();
  106. if (!$comment) {
  107. self::rollbackTrans();
  108. return [0, '评论不存在或无权删除'];
  109. }
  110. // 删除评论
  111. $this->where('id', $id)->delete();
  112. // 更新帖子评论数
  113. Db::name('forum_post')->where('id', $comment['post_id'])->dec('comment_count')->update();
  114. self::commitTrans();
  115. return [1, '删除成功'];
  116. } catch (\Exception $e) {
  117. self::rollbackTrans();
  118. return [0, '删除失败:' . $e->getMessage()];
  119. }
  120. }
  121. /**
  122. * 获取用户评论列表
  123. * @param int $uid 用户ID
  124. * @param int $page 页码
  125. * @param int $pageSize 每页数量
  126. * @return array
  127. */
  128. public function getUserComments($uid, $page = 1, $pageSize = 20)
  129. {
  130. $pageSize = $pageSize > 50 ? 50 : (int)$pageSize;
  131. $page = $page <= 0 ? 1 : (int)$page;
  132. $total = $this->where('uid', $uid)->count();
  133. $list = [];
  134. if ($total > 0) {
  135. $list = $this->alias('c')
  136. ->field('c.*, p.title as post_title, p.id as post_id')
  137. ->leftJoin('forum_post p', 'p.id = c.post_id')
  138. ->where('c.uid', $uid)
  139. ->order('c.create_time', 'desc')
  140. ->page($page, $pageSize)
  141. ->select();
  142. if (!empty($list)) {
  143. $list = $list->toArray();
  144. foreach ($list as &$item) {
  145. if (!empty($item['create_time'])) {
  146. $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
  147. }
  148. }
  149. }
  150. }
  151. return [
  152. 'list' => $list,
  153. 'total' => $total,
  154. 'page' => $page,
  155. 'pageSize' => $pageSize
  156. ];
  157. }
  158. }