ForumPost.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. /**
  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'] = date('Y-m-d H:i:s', $post['create_time']);
  87. }
  88. if (!empty($post['update_time'])) {
  89. $post['update_time'] = date('Y-m-d H:i:s', $post['update_time']);
  90. }
  91. // 增加浏览次数
  92. $this->where('id', $id)->inc('view_count')->update();
  93. return $post;
  94. }
  95. /**
  96. * 创建帖子
  97. * @param int $uid 用户ID
  98. * @param array $data 帖子数据
  99. * @return array [success, message, data]
  100. */
  101. public function createPost($uid, $data)
  102. {
  103. try {
  104. self::beginTrans();
  105. $insertData = [
  106. 'uid' => $uid,
  107. 'title' => trim($data['title'] ?? ''),
  108. 'content' => trim($data['content'] ?? ''),
  109. 'images' => !empty($data['images']) ? json_encode($data['images'], JSON_UNESCAPED_UNICODE) : '',
  110. 'status' => 0, // 待审核
  111. 'create_time' => time(),
  112. 'update_time' => time(),
  113. ];
  114. $result = $this->insert($insertData);
  115. if (!$result) {
  116. self::rollbackTrans();
  117. return [0, '发布失败'];
  118. }
  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 getUserPosts($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->where('uid', $uid)
  141. ->order('create_time', 'desc')
  142. ->page($page, $pageSize)
  143. ->select()
  144. ->toArray();
  145. foreach ($list as &$item) {
  146. if (!empty($item['images'])) {
  147. $item['images'] = json_decode($item['images'], true) ?: [];
  148. } else {
  149. $item['images'] = [];
  150. }
  151. if (!empty($item['create_time'])) {
  152. $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
  153. }
  154. }
  155. }
  156. return [
  157. 'list' => $list,
  158. 'total' => $total,
  159. 'page' => $page,
  160. 'pageSize' => $pageSize
  161. ];
  162. }
  163. }