ForumPost.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 ForumPost extends BaseModel
  12. {
  13. protected $pk = 'id';
  14. protected $table = 'table_forum_post';
  15. /**
  16. * 获取帖子列表
  17. * @param int $page 页码
  18. * @param int $pageSize 每页数量
  19. * @param array $where 筛选条件
  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. $where[] = ['p.status', '=', 1];
  27. $query = $this->alias('p')
  28. ->field('p.*, u.nickname as author_name, u.avatar as author_avatar')
  29. ->leftJoin('user u', 'u.uid = p.uid')
  30. ->where($where)
  31. ->order('p.is_top', 'desc')
  32. ->order('p.create_time', 'desc');
  33. $total = $query->count();
  34. $list = [];
  35. if ($total > 0) {
  36. $list = $query->page($page, $pageSize)->select();
  37. if (!empty($list)) {
  38. $list = $list->toArray();
  39. foreach ($list as &$item) {
  40. // 处理图片JSON
  41. if (!empty($item['images'])) {
  42. $item['images'] = json_decode($item['images'], true) ?: [];
  43. } else {
  44. $item['images'] = [];
  45. }
  46. // 格式化时间
  47. if (!empty($item['create_time'])) {
  48. $item['create_time'] = is_numeric($item['create_time'])
  49. ? date('Y-m-d H:i:s', (int)$item['create_time'])
  50. : $item['create_time'];
  51. }
  52. }
  53. }
  54. }
  55. return [
  56. 'list' => $list,
  57. 'total' => $total,
  58. 'page' => $page,
  59. 'pageSize' => $pageSize
  60. ];
  61. }
  62. /**
  63. * 获取帖子详情
  64. * @param int $id 帖子ID
  65. * @return array|null
  66. */
  67. public function getDetail($id)
  68. {
  69. $post = $this->alias('p')
  70. ->field('p.*, u.nickname as author_name, u.avatar as author_avatar')
  71. ->leftJoin('user u', 'u.uid = p.uid')
  72. ->where('p.id', $id)
  73. ->where('p.status', 1)
  74. ->find();
  75. if (empty($post)) {
  76. return null;
  77. }
  78. $post = $post->toArray();
  79. // 处理图片JSON
  80. if (!empty($post['images'])) {
  81. $post['images'] = json_decode($post['images'], true) ?: [];
  82. } else {
  83. $post['images'] = [];
  84. }
  85. // 格式化时间
  86. if (!empty($post['create_time'])) {
  87. $post['create_time'] = is_numeric($post['create_time'])
  88. ? date('Y-m-d H:i:s', (int)$post['create_time'])
  89. : $post['create_time'];
  90. }
  91. if (!empty($post['update_time'])) {
  92. $post['update_time'] = is_numeric($post['update_time'])
  93. ? date('Y-m-d H:i:s', (int)$post['update_time'])
  94. : $post['update_time'];
  95. }
  96. // 增加浏览次数
  97. $this->where('id', $id)->inc('view_count')->update();
  98. // 获取评论列表
  99. $post['comments'] = (new ForumComment())->getList(1, [['c.post_id', '=', $id]], 50);
  100. return $post;
  101. }
  102. /**
  103. * 创建帖子
  104. * @param int $uid 用户ID
  105. * @param array $data 帖子数据
  106. * @return array [success, message, data]
  107. */
  108. public function createPost($uid, $data)
  109. {
  110. try {
  111. self::beginTrans();
  112. $insertData = [
  113. 'uid' => $uid,
  114. 'title' => trim($data['title'] ?? ''),
  115. 'content' => trim($data['content'] ?? ''),
  116. 'images' => !empty($data['images']) ? json_encode($data['images'], JSON_UNESCAPED_UNICODE) : '',
  117. 'status' => 0, // 待审核
  118. 'create_time' => time(),
  119. 'update_time' => time(),
  120. ];
  121. $result = $this->insert($insertData);
  122. if (!$result) {
  123. self::rollbackTrans();
  124. return [0, '发布失败'];
  125. }
  126. self::commitTrans();
  127. return [1, '发布成功,等待审核'];
  128. } catch (\Exception $e) {
  129. self::rollbackTrans();
  130. return [0, '发布失败:' . $e->getMessage()];
  131. }
  132. }
  133. /**
  134. * 获取用户发布的帖子列表
  135. * @param int $uid 用户ID
  136. * @param int $page 页码
  137. * @param int $pageSize 每页数量
  138. * @return array
  139. */
  140. public function getUserPosts($uid, $page = 1, $pageSize = 20)
  141. {
  142. $pageSize = $pageSize > 50 ? 50 : (int)$pageSize;
  143. $page = $page <= 0 ? 1 : (int)$page;
  144. $total = $this->where('uid', $uid)->count();
  145. $list = [];
  146. if ($total > 0) {
  147. $list = $this->where('uid', $uid)
  148. ->order('create_time', 'desc')
  149. ->page($page, $pageSize)
  150. ->select()
  151. ->toArray();
  152. foreach ($list as &$item) {
  153. if (!empty($item['images'])) {
  154. $item['images'] = json_decode($item['images'], true) ?: [];
  155. } else {
  156. $item['images'] = [];
  157. }
  158. if (!empty($item['create_time'])) {
  159. $item['create_time'] = is_numeric($item['create_time'])
  160. ? date('Y-m-d H:i:s', (int)$item['create_time'])
  161. : $item['create_time'];
  162. }
  163. }
  164. }
  165. return [
  166. 'list' => $list,
  167. 'total' => $total,
  168. 'page' => $page,
  169. 'pageSize' => $pageSize
  170. ];
  171. }
  172. }