ForumPost.php 5.2 KB

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