ForumPost.php 5.7 KB

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