ForumComment.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 = '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'] = is_numeric($item['create_time'])
  45. ? date('Y-m-d H:i:s', (int)$item['create_time'])
  46. : $item['create_time'];
  47. }
  48. }
  49. }
  50. }
  51. return [
  52. 'list' => $list,
  53. 'total' => $total,
  54. 'page' => $page,
  55. 'pageSize' => $pageSize
  56. ];
  57. }
  58. /**
  59. * 添加评论
  60. * @param int $uid 用户ID
  61. * @param int $postId 帖子ID
  62. * @param string $content 评论内容
  63. * @return array [success, message, data]
  64. */
  65. public function addComment($uid, $postId, $content)
  66. {
  67. try {
  68. self::beginTrans();
  69. // 检查帖子是否存在且状态正常
  70. $post = Db::name('forum_post')->where('id', $postId)->where('status', 1)->find();
  71. if (!$post) {
  72. self::rollbackTrans();
  73. return [0, '帖子不存在或已被禁用'];
  74. }
  75. $insertData = [
  76. 'post_id' => $postId,
  77. 'uid' => $uid,
  78. 'content' => trim($content),
  79. 'status' => 1,
  80. 'create_time' => time(),
  81. 'update_time' => time(),
  82. ];
  83. $result = $this->insert($insertData);
  84. if (!$result) {
  85. self::rollbackTrans();
  86. return [0, '评论失败'];
  87. }
  88. // 更新帖子评论数
  89. Db::name('forum_post')->where('id', $postId)->inc('comment_count')->update();
  90. self::commitTrans();
  91. return [1, '评论成功', ['post_id' => $postId]];
  92. } catch (\Exception $e) {
  93. self::rollbackTrans();
  94. return [0, '评论失败:' . $e->getMessage()];
  95. }
  96. }
  97. /**
  98. * 删除评论
  99. * @param int $id 评论ID
  100. * @param int $uid 用户ID
  101. * @return array [success, message]
  102. */
  103. public function deleteComment($id, $uid)
  104. {
  105. try {
  106. self::beginTrans();
  107. $comment = $this->where('id', $id)->where('uid', $uid)->find();
  108. if (!$comment) {
  109. self::rollbackTrans();
  110. return [0, '评论不存在或无权删除'];
  111. }
  112. // 删除评论
  113. $this->where('id', $id)->delete();
  114. // 更新帖子评论数
  115. Db::name('forum_post')->where('id', $comment['post_id'])->dec('comment_count')->update();
  116. self::commitTrans();
  117. return [1, '删除成功'];
  118. } catch (\Exception $e) {
  119. self::rollbackTrans();
  120. return [0, '删除失败:' . $e->getMessage()];
  121. }
  122. }
  123. /**
  124. * 获取用户评论列表
  125. * @param int $uid 用户ID
  126. * @param int $page 页码
  127. * @param int $pageSize 每页数量
  128. * @return array
  129. */
  130. public function getUserComments($uid, $page = 1, $pageSize = 20)
  131. {
  132. $pageSize = $pageSize > 50 ? 50 : (int)$pageSize;
  133. $page = $page <= 0 ? 1 : (int)$page;
  134. $total = $this->where('uid', $uid)->count();
  135. $list = [];
  136. if ($total > 0) {
  137. $list = $this->alias('c')
  138. ->field('c.*, p.title as post_title, p.id as post_id')
  139. ->leftJoin('forum_post p', 'p.id = c.post_id')
  140. ->where('c.uid', $uid)
  141. ->order('c.create_time', 'desc')
  142. ->page($page, $pageSize)
  143. ->select();
  144. if (!empty($list)) {
  145. $list = $list->toArray();
  146. foreach ($list as &$item) {
  147. if (!empty($item['create_time'])) {
  148. $item['create_time'] = is_numeric($item['create_time'])
  149. ? date('Y-m-d H:i:s', (int)$item['create_time'])
  150. : $item['create_time'];
  151. }
  152. }
  153. }
  154. }
  155. return [
  156. 'list' => $list,
  157. 'total' => $total,
  158. 'page' => $page,
  159. 'pageSize' => $pageSize
  160. ];
  161. }
  162. }