feat(admin): 信息置顶 + 发布与费用配置(免费/付费开关、单条与月年卡价格含条数)

需求落地:
- 置顶: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
这个提交包含在:
linda
2026-10-15 23:24:03 +08:00
父节点 f806dc269b
当前提交 043227dee7
共有 13 个文件被更改,包括 844 次插入 和 34 次删除

查看文件

@@ -7,13 +7,20 @@ use think\Db;
/**
* 用户端 · 信息发布与详情(Module-POST)
* 路由:
* POST /api/v1/post/create P1 发布信息
* POST /api/v1/post/create P1 发布信息(服务端定价)
* GET /api/v1/post/mine P8 我的发布(含审核状态与计数)
* GET /api/v1/post/publish-config P9 发布配置(发布模式/价格/我的剩余额度)
* GET /api/v1/post/drafts P2 草稿列表
* POST /api/v1/post/draft P3 保存草稿
* DELETE /api/v1/post/:id P4 删除信息
* GET /api/v1/post/:id P5 信息详情
* POST /api/v1/post/:id/pay-view P6 付费查看联系方式
* POST /api/v1/post/:id/comment P7 发表评价
*
* 计费口径(后台「发布与费用配置」维护,真源=fc_config);
* 金额一律由服务端按配置计算,**不接受客户端传入金额**;
* 月卡/年卡(含发布条数)的购买复用 POST /member/open,本控制器只处理
* 「套餐额度内免费发布」与「单条付费发布」两种。
*/
class Post extends V1
{
@@ -53,23 +60,56 @@ class Post extends V1
$this->fail('手机号格式不正确');
}
$paid = (bool)$this->input('paid', false);
$payType = (string)$this->input('payType', '');
// ---- 发布计费:金额一律由服务端按后台配置计算,不信任客户端传值 ----
$rule = $this->publishRule();
$plan = trim((string)$this->input('plan', ''));
if ($plan === '' && (bool)$this->input('paid', false)) {
// 兼容 v1.0.6 及更早客户端:paid=true 视为单条付费(金额仍由服务端决定)
$plan = 'single';
}
if ($plan === '') {
$plan = 'free';
}
if (!in_array($plan, ['free', 'single'], true)) {
// 月卡/年卡走会员接口开通(POST /member/open),拿到额度后再以 free 发布
$this->fail('发布方式不支持,请先开通会员套餐');
}
$ext = $this->ext($user['id']);
$quotaLeft = $this->quotaLeft($ext);
$amount = 0;
if ($paid) {
if (!in_array($payType, ['wxpay', 'balance'], true)) {
$this->fail('支付方式不支持');
}
$amount = $this->input('amount', null);
if ($amount === null || $amount === '') {
$amount = $this->cfg('fee.singlePost', 5);
}
$amount = round((float)$amount, 2);
// 余额校验须在落库之前:否则余额不足时会残留一条 is_paid=1 的无效发布
if ($payType === 'balance' && (float)$user['money'] < $amount) {
$this->fail('余额不足');
$payType = '';
$useQuota = false; // 套餐额度内免费
$useFreeQuota = false; // 每日免费额度内免费
if ($rule['mode'] === 'paid') {
if ($plan === 'free') {
if ($quotaLeft > 0) {
$useQuota = true; // 套餐额度内免费发布
} else {
// 每日免费额度(fee.freePostPerDay,0 = 不提供)内同样允许免费发布,
// 避免「付费发布」一开启就变成硬付费墙
$freePerDay = max(0, (int)$this->cfg('fee.freePostPerDay', 0));
$usedToday = $freePerDay > 0 ? $this->todayPostCount($user['id']) : 0;
if ($freePerDay > 0 && $usedToday < $freePerDay) {
$useFreeQuota = true;
} else {
$this->fail('请选择发布方式');
}
}
} else {
$amount = $rule['singlePrice'];
$payType = (string)$this->input('payType', '');
if ($amount > 0 && !in_array($payType, ['wxpay', 'balance'], true)) {
$this->fail('支付方式不支持');
}
// 余额校验须在落库之前:否则余额不足时会残留一条 is_paid=1 的无效发布
if ($amount > 0 && $payType === 'balance' && (float)$user['money'] < $amount) {
$this->fail('余额不足');
}
}
}
$paid = $amount > 0;
$now = time();
$postId = Db::name('fc_post')->insertGetId([
@@ -136,13 +176,187 @@ class Post extends V1
];
}
// 套餐额度内免费发布:落库成功后扣减 1 条额度(先发布后扣,发布失败不白扣)
if ($useQuota) {
$quotaLeft = max(0, $quotaLeft - 1);
Db::name('fc_user_ext')->where('user_id', $user['id'])->update([
'post_quota_left' => $quotaLeft,
'updatetime' => $now,
]);
}
$this->ok([
'postId' => $postId,
'auditStatus' => 0,
'order' => $order,
'plan' => $rule['mode'] === 'free' ? 'free' : ($useQuota ? 'quota' : ($useFreeQuota ? 'free' : 'single')),
'amount' => $amount,
'quotaLeft' => $rule['mode'] === 'paid' ? $quotaLeft : 0,
]);
}
/**
* P8 我的发布
* GET /api/v1/post/mine {status?,page,size}
* → {list,page,size,total,hasMore,counts}
* status: all(默认) / pending 待审核 / passed 已通过 / rejected 已驳回 / offline 已下架
* 规则:仅本人(过滤软删);置顶优先、其次按发布时间倒序;counts 为各状态条数
*/
public function mine()
{
$user = $this->auth();
list($page, $size) = $this->pager();
$status = (string)$this->input('status', 'all');
/** 组合状态筛选(all 不加条件) */
$apply = function ($query, $status) {
if ($status === 'pending') {
$query->where('audit_status', 0)->where('status', 1);
} elseif ($status === 'passed') {
$query->where('audit_status', 1)->where('status', 1);
} elseif ($status === 'rejected') {
$query->where('audit_status', 2);
} elseif ($status === 'offline') {
$query->where('status', 0);
}
return $query;
};
$base = function () use ($user) {
return Db::name('fc_post')->where('user_id', $user['id'])->whereNull('deletetime');
};
$total = $apply($base(), $status)->count();
$rows = $apply($base(), $status)->order('is_top desc, createtime desc')
->page($page, $size)->select();
$list = [];
foreach ($rows as $row) {
$cat = Db::name('fc_category')->where('id', $row['category_id'])->find();
$list[] = [
'id' => (int)$row['id'],
'title' => (string)$row['title'],
'content' => (string)$row['content'],
'categoryId' => (int)$row['category_id'],
'categoryName' => $cat ? (string)$cat['name'] : '',
'cover' => (string)$row['cover'],
'price' => (float)$row['price'],
'priceUnit' => (string)$row['price_unit'],
'area' => (string)$row['area'],
'address' => (string)$row['address'],
'auditStatus' => (int)$row['audit_status'],
'auditReason' => (string)$row['audit_reason'],
'status' => (int)$row['status'],
'isTop' => (int)$row['is_top'] === 1,
'isPaid' => (int)$row['is_paid'] === 1,
'amount' => (float)$row['amount'],
'views' => (int)$row['views'],
'likes' => (int)$row['likes'],
'commentCount' => (int)$row['comment_count'],
'createdAt' => $this->iso($row['createtime']),
];
}
$counts = [
'all' => (int)$base()->count(),
'pending' => (int)$apply($base(), 'pending')->count(),
'passed' => (int)$apply($base(), 'passed')->count(),
'rejected' => (int)$apply($base(), 'rejected')->count(),
'offline' => (int)$apply($base(), 'offline')->count(),
];
$this->ok(array_merge($this->paginate($list, $total, $page, $size), ['counts' => $counts]));
}
/**
* P9 发布配置
* GET /api/v1/post/publish-config
* → {mode,singlePrice,monthPrice,monthQuota,yearPrice,yearQuota,
* quotaLeft,quotaExpireAt,isMember,memberExpireAt}
* 用途:发布页按「免费/付费」呈现并显示套餐价格与剩余额度(价格由后台配置,前端不再写死)
*/
public function publishConfig()
{
$user = $this->auth();
$rule = $this->publishRule();
$ext = $this->ext($user['id']);
$now = time();
$quotaExpire = (int)$ext['post_quota_expire'];
$memberExpire = (int)$ext['member_expiretime'];
// 每日免费额度(付费模式下仍保留的免费次数)
$freePerDay = max(0, (int)$this->cfg('fee.freePostPerDay', 0));
$usedToday = $freePerDay > 0 ? $this->todayPostCount($user['id']) : 0;
$this->ok([
'mode' => $rule['mode'],
'singlePrice' => $rule['singlePrice'],
'monthPrice' => $rule['monthPrice'],
'monthQuota' => $rule['monthQuota'],
'yearPrice' => $rule['yearPrice'],
'yearQuota' => $rule['yearQuota'],
'quotaLeft' => $this->quotaLeft($ext),
'freePerDay' => $freePerDay,
'freeUsedToday' => $usedToday,
'freeLeftToday' => max(0, $freePerDay - $usedToday),
'quotaExpireAt' => $quotaExpire > 0 ? $this->iso($quotaExpire) : '',
'isMember' => (int)$ext['is_member'] === 1 && ($memberExpire === 0 || $memberExpire > $now),
'memberExpireAt' => $memberExpire > 0 ? $this->iso($memberExpire) : '',
]);
}
/**
* 发布规则(后台「发布与费用配置」维护;配置缺失时回落旧 fee.* 再回落内置默认)
* @return array
*/
protected function publishRule()
{
$mode = (string)$this->cfg('post.mode', 'paid');
if (!in_array($mode, ['free', 'paid'], true)) {
$mode = 'paid';
}
return [
'mode' => $mode,
'singlePrice' => round((float)$this->cfg('post.singlePrice', $this->cfg('fee.singlePost', 5)), 2),
'monthPrice' => round((float)$this->cfg('post.monthPrice', $this->cfg('fee.memberMonth', 19.9)), 2),
'monthQuota' => max(0, (int)$this->cfg('post.monthQuota', 30)),
'yearPrice' => round((float)$this->cfg('post.yearPrice', $this->cfg('fee.memberYear', 168)), 2),
'yearQuota' => max(0, (int)$this->cfg('post.yearQuota', 500)),
];
}
/**
* 今日已发布条数(每日免费额度判定用,不含软删)
* @param int $userId
* @return int
*/
protected function todayPostCount($userId)
{
$start = strtotime(date('Y-m-d') . ' 00:00:00');
return (int)Db::name('fc_post')->where('user_id', $userId)
->where('createtime', '>=', $start)->whereNull('deletetime')->count();
}
/**
* 剩余发布条数(超出有效期视为 0,并顺手把过期额度清零)
* @param array $ext fc_user_ext 行
* @return int
*/
protected function quotaLeft($ext)
{
$left = (int)(isset($ext['post_quota_left']) ? $ext['post_quota_left'] : 0);
$expire = (int)(isset($ext['post_quota_expire']) ? $ext['post_quota_expire'] : 0);
if ($left > 0 && $expire > 0 && $expire < time()) {
Db::name('fc_user_ext')->where('user_id', $ext['user_id'])->update([
'post_quota_left' => 0,
'updatetime' => time(),
]);
return 0;
}
return $left;
}
/**
* P2 草稿列表
* GET /api/v1/post/drafts → [{draftId,title,content,categoryId,savedAt}]