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