ForumComment.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. // 获取用户昵称,如果为空则取手机号
  75. $user = Db::name('user')->where('uid', $uid)->find();
  76. $userName = !empty($user['nickname']) ? $user['nickname'] : ($user['mobile'] ?? '匿名用户');
  77. $insertData = [
  78. 'post_id' => $postId,
  79. 'uid' => $uid,
  80. 'user_name' => $userName,
  81. 'content' => trim($content),
  82. 'status' => 1,
  83. 'create_time' => time(),
  84. 'update_time' => time(),
  85. ];
  86. $result = $this->insert($insertData);
  87. if (!$result) {
  88. self::rollbackTrans();
  89. return [0, '评论失败'];
  90. }
  91. // 更新帖子评论数
  92. Db::name('forum_post')->where('id', $postId)->inc('comment_count')->update();
  93. self::commitTrans();
  94. return [1, '评论成功', ['post_id' => $postId]];
  95. } catch (\Exception $e) {
  96. self::rollbackTrans();
  97. return [0, '评论失败:' . $e->getMessage()];
  98. }
  99. }
  100. /**
  101. * 删除评论
  102. * @param int $id 评论ID
  103. * @param int $uid 用户ID
  104. * @return array [success, message]
  105. */
  106. public function deleteComment($id, $uid)
  107. {
  108. try {
  109. self::beginTrans();
  110. $comment = $this->where('id', $id)->where('uid', $uid)->find();
  111. if (!$comment) {
  112. self::rollbackTrans();
  113. return [0, '评论不存在或无权删除'];
  114. }
  115. // 删除评论
  116. $this->where('id', $id)->delete();
  117. // 更新帖子评论数
  118. Db::name('forum_post')->where('id', $comment['post_id'])->dec('comment_count')->update();
  119. self::commitTrans();
  120. return [1, '删除成功'];
  121. } catch (\Exception $e) {
  122. self::rollbackTrans();
  123. return [0, '删除失败:' . $e->getMessage()];
  124. }
  125. }
  126. /**
  127. * 获取用户评论列表
  128. * @param int $uid 用户ID
  129. * @param int $page 页码
  130. * @param int $pageSize 每页数量
  131. * @return array
  132. */
  133. public function getUserComments($uid, $page = 1, $pageSize = 20)
  134. {
  135. $pageSize = $pageSize > 50 ? 50 : (int)$pageSize;
  136. $page = $page <= 0 ? 1 : (int)$page;
  137. $total = $this->where('uid', $uid)->count();
  138. $list = [];
  139. if ($total > 0) {
  140. $list = $this->alias('c')
  141. ->field('c.*, p.title as post_title, p.id as post_id')
  142. ->leftJoin('forum_post p', 'p.id = c.post_id')
  143. ->where('c.uid', $uid)
  144. ->order('c.create_time', 'desc')
  145. ->page($page, $pageSize)
  146. ->select();
  147. if (!empty($list)) {
  148. $list = $list->toArray();
  149. foreach ($list as &$item) {
  150. if (!empty($item['create_time'])) {
  151. $item['create_time'] = is_numeric($item['create_time'])
  152. ? date('Y-m-d H:i:s', (int)$item['create_time'])
  153. : $item['create_time'];
  154. }
  155. }
  156. }
  157. }
  158. return [
  159. 'list' => $list,
  160. 'total' => $total,
  161. 'page' => $page,
  162. 'pageSize' => $pageSize
  163. ];
  164. }
  165. }