50 ? 50 : (int)$pageSize; $page = $page <= 0 ? 1 : (int)$page; $where[] = ['status', '=', 1]; $query = $this->alias('p') ->field('p.*, u.nickname as author_name, u.avatar as author_avatar') ->leftJoin('user u', 'u.uid = p.uid') ->where($where) ->order('p.is_top', 'desc') ->order('p.create_time', 'desc'); $total = $query->count(); $list = []; if ($total > 0) { $list = $query->page($page, $pageSize)->select(); if (!empty($list)) { $list = $list->toArray(); foreach ($list as &$item) { // 处理图片JSON if (!empty($item['images'])) { $item['images'] = json_decode($item['images'], true) ?: []; } else { $item['images'] = []; } // 格式化时间 if (!empty($item['create_time'])) { $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']); } } } } return [ 'list' => $list, 'total' => $total, 'page' => $page, 'pageSize' => $pageSize ]; } /** * 获取帖子详情 * @param int $id 帖子ID * @return array|null */ public function getDetail($id) { $post = $this->alias('p') ->field('p.*, u.nickname as author_name, u.avatar as author_avatar') ->leftJoin('user u', 'u.uid = p.uid') ->where('p.id', $id) ->where('p.status', 1) ->find(); if (empty($post)) { return null; } $post = $post->toArray(); // 处理图片JSON if (!empty($post['images'])) { $post['images'] = json_decode($post['images'], true) ?: []; } else { $post['images'] = []; } // 格式化时间 if (!empty($post['create_time'])) { $post['create_time'] = date('Y-m-d H:i:s', $post['create_time']); } if (!empty($post['update_time'])) { $post['update_time'] = date('Y-m-d H:i:s', $post['update_time']); } // 增加浏览次数 $this->where('id', $id)->inc('view_count')->update(); return $post; } /** * 创建帖子 * @param int $uid 用户ID * @param array $data 帖子数据 * @return array [success, message, data] */ public function createPost($uid, $data) { try { self::beginTrans(); $insertData = [ 'uid' => $uid, 'title' => trim($data['title'] ?? ''), 'content' => trim($data['content'] ?? ''), 'images' => !empty($data['images']) ? json_encode($data['images'], JSON_UNESCAPED_UNICODE) : '', 'status' => 0, // 待审核 'create_time' => time(), 'update_time' => time(), ]; $result = $this->insert($insertData); if (!$result) { self::rollbackTrans(); return [0, '发布失败']; } self::commitTrans(); return [1, '发布成功,等待审核']; } catch (\Exception $e) { self::rollbackTrans(); return [0, '发布失败:' . $e->getMessage()]; } } /** * 获取用户发布的帖子列表 * @param int $uid 用户ID * @param int $page 页码 * @param int $pageSize 每页数量 * @return array */ public function getUserPosts($uid, $page = 1, $pageSize = 20) { $pageSize = $pageSize > 50 ? 50 : (int)$pageSize; $page = $page <= 0 ? 1 : (int)$page; $total = $this->where('uid', $uid)->count(); $list = []; if ($total > 0) { $list = $this->where('uid', $uid) ->order('create_time', 'desc') ->page($page, $pageSize) ->select() ->toArray(); foreach ($list as &$item) { if (!empty($item['images'])) { $item['images'] = json_decode($item['images'], true) ?: []; } else { $item['images'] = []; } if (!empty($item['create_time'])) { $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']); } } } return [ 'list' => $list, 'total' => $total, 'page' => $page, 'pageSize' => $pageSize ]; } }