where('enabled', 1) ->order('sort asc') ->select(); $list = []; foreach ($rows as $row) { $list[] = [ 'id' => (int)$row['id'], 'title' => (string)$row['title'], 'image' => (string)$row['image'], 'link' => (string)$row['link'], 'color' => (string)$row['color'], ]; } $this->ok($list); } /** * H2 分类导航 * GET /api/v1/home/categories → [{id,name,iconKey,icon,color,count}] * 规则:仅 enabled=1,按 sort asc;icon 为固定 /static/icons/ 文件名(按 id 映射) */ public function categories() { $rows = Db::name('fc_category') ->where('enabled', 1) ->order('sort asc') ->select(); $list = []; foreach ($rows as $row) { $id = (int)$row['id']; $list[] = [ 'id' => $id, 'name' => (string)$row['name'], 'iconKey' => (string)$row['icon_key'], 'icon' => '/static/icons/' . $this->iconFile($id), 'color' => (string)$row['color'], 'count' => (int)$row['post_count'], ]; } $this->ok($list); } /** * H3 热门搜索 * GET /api/v1/home/search/hot → [string] * 规则:读配置 home.hotKeywords,未配置则返回默认 */ public function hotKeywords() { $default = ['租房 一室一厅', '二手电动车', '急招快递员', '钢琴家教', '空调维修']; $keywords = $this->cfg('home.hotKeywords', $default); if (!is_array($keywords) || !$keywords) { $keywords = $default; } $this->ok(array_values($keywords)); } /** * H4 信息流 * GET /api/v1/home/feed {categoryId?,keyword?,area?,sort?,page,size} * → {list,page,size,total,hasMore} * 规则:仅 audit_status=1 AND status=1 AND deletetime IS NULL */ public function feed() { list($page, $size) = $this->pager(); $categoryId = (int)$this->input('categoryId', 0); $keyword = trim((string)$this->input('keyword', '')); $area = trim((string)$this->input('area', '')); $sort = (string)$this->input('sort', ''); $query = Db::name('fc_post') ->where('audit_status', 1) ->where('status', 1) ->whereNull('deletetime'); if ($categoryId > 0) { $query->where('category_id', $categoryId); } if ($keyword !== '') { $query->where('title', 'like', '%' . $keyword . '%'); } if ($area !== '') { $query->where('area', $area); } if ($sort === 'price') { $query->order('price asc'); } elseif ($sort === 'hot') { $query->order('views desc'); } else { $query->order('createtime desc'); } $counter = Db::name('fc_post') ->where('audit_status', 1) ->where('status', 1) ->whereNull('deletetime'); if ($categoryId > 0) { $counter->where('category_id', $categoryId); } if ($keyword !== '') { $counter->where('title', 'like', '%' . $keyword . '%'); } if ($area !== '') { $counter->where('area', $area); } $total = $counter->count(); $rows = $query->page($page, $size)->select(); $list = []; foreach ($rows as $row) { $list[] = $this->buildPost($row); } $this->ok($this->paginate($list, $total, $page, $size)); } /** * 组装 Post 结构(与前端 mock 数据字段严格对齐) * @param array $row fc_post 行 * @return array */ protected function buildPost($row) { $categoryName = ''; if (!empty($row['category_id'])) { $cat = Db::name('fc_category')->where('id', $row['category_id'])->find(); if ($cat) { $categoryName = (string)$cat['name']; } } $publisher = ['id' => 0, 'name' => '', 'avatar' => '', 'isMerchant' => false, 'verified' => false]; if (!empty($row['user_id'])) { $pub = Db::name('user')->where('id', $row['user_id'])->find(); if ($pub) { $pubExt = Db::name('fc_user_ext')->where('user_id', $pub['id'])->find(); $isMerchant = $pubExt && (int)$pubExt['user_type'] === 2; $publisher = [ 'id' => (int)$pub['id'], 'name' => (string)$pub['nickname'], 'avatar' => (string)$pub['avatar'], 'isMerchant' => (bool)$isMerchant, 'verified' => (bool)$isMerchant, ]; } } $tags = []; if (!empty($row['tags'])) { foreach (explode(',', (string)$row['tags']) as $tag) { $tag = trim($tag); if ($tag !== '') { $tags[] = $tag; } } } return [ 'id' => (int)$row['id'], 'categoryId' => (int)$row['category_id'], 'categoryName' => $categoryName, 'title' => (string)$row['title'], 'content' => (string)$row['content'], 'cover' => (string)$row['cover'], 'price' => (float)$row['price'], 'priceUnit' => (string)$row['price_unit'], 'area' => (string)$row['area'], 'address' => (string)$row['address'], 'contact' => $this->maskPhone($row['phone']), 'views' => (int)$row['views'], 'likes' => (int)$row['likes'], 'isPaid' => (int)$row['is_paid'] === 1, 'auditStatus' => (int)$row['audit_status'], 'createdAt' => $this->iso($row['createtime']), 'tags' => $tags, 'publisher' => $publisher, 'commentCount' => (int)$row['comment_count'], 'distance' => (int)$row['distance'], ]; } /** * 分类 id → 固定图标文件名 * @param int $id * @return string */ protected function iconFile($id) { $map = [ 1 => 'cat-job.png', 2 => 'cat-house.png', 3 => 'cat-goods.png', 4 => 'cat-car.png', 5 => 'cat-service.png', 6 => 'cat-education.png', 7 => 'cat-clean.png', 8 => 'cat-friend.png', ]; return isset($map[$id]) ? $map[$id] : 'cat-job.png'; } }