需求落地: - 置顶: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
220 行
7.3 KiB
PHP
220 行
7.3 KiB
PHP
<?php
|
||
|
||
namespace app\api\controller;
|
||
|
||
use think\Db;
|
||
|
||
/**
|
||
* 用户端 · 会员中心(Module-MEMBER)
|
||
* 路由:
|
||
* GET /api/v1/member/plans M1 会员套餐
|
||
* GET /api/v1/member/benefits M2 会员权益
|
||
* POST /api/v1/member/open M3 开通会员
|
||
* GET /api/v1/member/orders M4 我的订单
|
||
* GET /api/v1/member/assets M5 我的资产
|
||
*/
|
||
class Member extends V1
|
||
{
|
||
/**
|
||
* M1 会员套餐
|
||
* GET /api/v1/member/plans → [{planId,name,price,originPrice,days,tag}]
|
||
* 规则:仅 enabled=1,按 sort asc
|
||
*/
|
||
public function plans()
|
||
{
|
||
$rows = Db::name('fc_plan')
|
||
->where('enabled', 1)
|
||
->order('sort asc')
|
||
->select();
|
||
|
||
$list = [];
|
||
foreach ($rows as $row) {
|
||
$list[] = [
|
||
'planId' => (string)$row['plan_code'],
|
||
'name' => (string)$row['name'],
|
||
'price' => (float)$row['price'],
|
||
'originPrice' => (float)$row['origin_price'],
|
||
'days' => (int)$row['days'],
|
||
'tag' => (string)$row['tag'],
|
||
// 套餐含发布条数(后台「发布与费用配置」同步;0 表示不含发布额度)
|
||
'postQuota' => isset($row['post_quota']) ? (int)$row['post_quota'] : 0,
|
||
];
|
||
}
|
||
$this->ok($list);
|
||
}
|
||
|
||
/**
|
||
* M2 会员权益
|
||
* GET /api/v1/member/benefits → {freeView,postDiscount,distRate,benefits[]}
|
||
* 规则:读配置 member.benefit,未配置返回默认
|
||
*/
|
||
public function benefits()
|
||
{
|
||
$default = [
|
||
'freeView' => 20,
|
||
'postDiscount' => '8 折',
|
||
'distRate' => '一级 20% / 二级 10%',
|
||
'benefits' => ['每日免费查看 20 条联系方式', '发布信息 8 折', '专属会员标识', '分销佣金加速结算'],
|
||
];
|
||
|
||
$cfg = $this->cfg('member.benefit', null);
|
||
if (!is_array($cfg)) {
|
||
$cfg = $default;
|
||
}
|
||
|
||
$benefits = isset($cfg['benefits']) && is_array($cfg['benefits']) && $cfg['benefits']
|
||
? array_values($cfg['benefits'])
|
||
: $default['benefits'];
|
||
|
||
$this->ok([
|
||
'freeView' => isset($cfg['freeView']) ? (int)$cfg['freeView'] : $default['freeView'],
|
||
'postDiscount' => isset($cfg['postDiscount']) ? (string)$cfg['postDiscount'] : $default['postDiscount'],
|
||
'distRate' => isset($cfg['distRate']) ? (string)$cfg['distRate'] : $default['distRate'],
|
||
'benefits' => $benefits,
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* M3 开通会员
|
||
* POST /api/v1/member/open {planId,payType} → {orderNo,status,expireAt}
|
||
* 规则 BR-07;套餐按 plan_code 查;余额支付扣 fa_user.money
|
||
*/
|
||
public function open()
|
||
{
|
||
$user = $this->auth();
|
||
|
||
$planId = trim((string)$this->input('planId', ''));
|
||
if ($planId === '') {
|
||
$this->fail('请选择会员套餐');
|
||
}
|
||
|
||
$payType = (string)$this->input('payType', '');
|
||
if (!in_array($payType, ['wxpay', 'balance'], true)) {
|
||
$this->fail('支付方式不支持');
|
||
}
|
||
|
||
$plan = Db::name('fc_plan')->where('plan_code', $planId)->find();
|
||
if (!$plan) {
|
||
$this->fail('套餐不存在');
|
||
}
|
||
|
||
$amount = round((float)$plan['price'], 2);
|
||
$now = time();
|
||
|
||
// 余额校验必须在落订单之前:否则余额不足时已插入一条 status=paid 的假订单
|
||
$balance = (float)$user['money'];
|
||
if ($payType === 'balance' && $balance < $amount) {
|
||
$this->fail('余额不足');
|
||
}
|
||
|
||
$orderNo = $this->orderNo('MO');
|
||
$orderId = Db::name('fc_order')->insertGetId([
|
||
'order_no' => $orderNo,
|
||
'user_id' => $user['id'],
|
||
'type' => 'member',
|
||
'title' => (string)$plan['name'],
|
||
'amount' => $amount,
|
||
'pay_type' => $payType,
|
||
'status' => 'paid',
|
||
'paytime' => $now,
|
||
'related_id' => 0,
|
||
'createtime' => $now,
|
||
'updatetime' => $now,
|
||
]);
|
||
|
||
if ($payType === 'balance') {
|
||
$balance = round($balance - $amount, 2);
|
||
Db::name('user')->where('id', $user['id'])->update([
|
||
'money' => $balance,
|
||
'updatetime' => $now,
|
||
]);
|
||
}
|
||
|
||
$ext = $this->ext($user['id']);
|
||
$base = (int)$ext['member_expiretime'];
|
||
if ($base < $now) {
|
||
$base = $now;
|
||
}
|
||
$expire = $base + (int)$plan['days'] * 86400;
|
||
|
||
// 发布套餐额度:月/年卡赠送「含发布条数」,额度随会员有效期(过期后由发布接口自动清零)
|
||
$quota = isset($plan['post_quota']) ? max(0, (int)$plan['post_quota']) : 0;
|
||
$left = (int)(isset($ext['post_quota_left']) ? $ext['post_quota_left'] : 0);
|
||
$quotaExpire = (int)(isset($ext['post_quota_expire']) ? $ext['post_quota_expire'] : 0);
|
||
if ($quotaExpire > 0 && $quotaExpire < $now) {
|
||
$left = 0; // 上一周期额度已过期,不再累计
|
||
}
|
||
$left += $quota;
|
||
|
||
Db::name('fc_user_ext')->where('user_id', $user['id'])->update([
|
||
'is_member' => 1,
|
||
'member_level' => (string)$plan['name'],
|
||
'member_expiretime' => $expire,
|
||
'post_quota_left' => $left,
|
||
'post_quota_expire' => $expire,
|
||
'updatetime' => $now,
|
||
]);
|
||
|
||
$this->flow('income', '会员开通', $amount, 0, $orderId);
|
||
|
||
$this->ok([
|
||
'orderNo' => $orderNo,
|
||
'status' => 'paid',
|
||
'expireAt' => $this->iso($expire),
|
||
'postQuota' => $quota,
|
||
'quotaLeft' => $left,
|
||
'quotaExpireAt' => $this->iso($expire),
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* M4 我的订单
|
||
* GET /api/v1/member/orders {page,size} → {list,page,size,total,hasMore}
|
||
* 规则:按 createtime desc
|
||
*/
|
||
public function orders()
|
||
{
|
||
$user = $this->auth();
|
||
list($page, $size) = $this->pager();
|
||
|
||
$total = Db::name('fc_order')->where('user_id', $user['id'])->count();
|
||
|
||
$rows = Db::name('fc_order')
|
||
->where('user_id', $user['id'])
|
||
->order('createtime desc')
|
||
->page($page, $size)
|
||
->select();
|
||
|
||
$list = [];
|
||
foreach ($rows as $row) {
|
||
$list[] = [
|
||
'id' => (int)$row['id'],
|
||
'orderNo' => (string)$row['order_no'],
|
||
'type' => (string)$row['type'],
|
||
'title' => (string)$row['title'],
|
||
'amount' => (float)$row['amount'],
|
||
'payType' => (string)$row['pay_type'],
|
||
'status' => (string)$row['status'],
|
||
'createdAt' => $this->iso($row['createtime']),
|
||
];
|
||
}
|
||
|
||
$this->ok($this->paginate($list, $total, $page, $size));
|
||
}
|
||
|
||
/**
|
||
* M5 我的资产
|
||
* GET /api/v1/member/assets → {balance,coupons:[],points}
|
||
*/
|
||
public function assets()
|
||
{
|
||
$user = $this->auth();
|
||
|
||
$this->ok([
|
||
'balance' => (float)$user['money'],
|
||
'coupons' => [],
|
||
'points' => (int)$user['score'],
|
||
]);
|
||
}
|
||
}
|