需求落地: - 置顶:fc/post 新增 top 动作(批量置顶/取消置顶 + 站内消息通知),列表加「置顶/取消置顶」工具栏按钮、 行内按钮与置顶状态徽章;置顶信息在用户端首页与「我的发布」中优先展示(Home::feed 按 is_top desc) - 发布与费用配置(新增页面:同城业务 → 基础数据 → 发布与费用配置,fc/publish): 发布模式(免费发布 / 付费发布)、会员单条发布价格、按月(价格 + 含条数)、按年(价格 + 含条数); 真源为 fc_config 的 post.*,保存时同步 fc_plan 的月卡(M1)/年卡(M12) 价格与 post_quota - FcInstall:新增 post.* 六项配置(默认免费发布,避免上线即撞付费墙);fc_user_ext 加 post_quota_left / post_quota_expire(套餐额度与有效期)、fc_plan 加 post_quota;新增 upgradePublish() 幂等升级老库并同步月/年卡 - tools/verify-api.mjs:新增 P9 发布配置 / P8 我的发布 契约校验,P1 发布改为「免费模式/额度内发布」并按付费模式容错 - 语言包与视图同步中文,均通过 php -l 与 node --check
232 行
7.5 KiB
PHP
232 行
7.5 KiB
PHP
<?php
|
||
|
||
namespace app\api\controller;
|
||
|
||
use think\Db;
|
||
|
||
/**
|
||
* 用户端 · 首页与展示(Module-HOME)
|
||
* 路由:
|
||
* GET /api/v1/home/banners H1 轮播广告位
|
||
* GET /api/v1/home/categories H2 分类导航
|
||
* GET /api/v1/home/search/hot H3 热门搜索
|
||
* GET /api/v1/home/feed H4 信息流
|
||
*/
|
||
class Home extends V1
|
||
{
|
||
/**
|
||
* H1 首页轮播广告位
|
||
* GET /api/v1/home/banners → [{id,title,image,link,color}]
|
||
* 规则:仅 enabled=1,按 sort asc;image 为空返回 ''
|
||
*/
|
||
public function banners()
|
||
{
|
||
$rows = Db::name('fc_banner')
|
||
->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('is_top desc, price asc');
|
||
} elseif ($sort === 'hot') {
|
||
$query->order('is_top desc, views desc');
|
||
} else {
|
||
// 后台「置顶」优先展示,其余按发布时间倒序
|
||
$query->order('is_top desc, 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,
|
||
'isTop' => (int)$row['is_top'] === 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';
|
||
}
|
||
}
|