一、基础设施
- route.php:58 条路由覆盖 /api/v1/**;:id 用 \d+ 约束、长路径优先
- api/controller/V1.php:响应适配为 {code,message,data}(FastAdmin 原生 {code,msg,time,data} 不兼容);
Authorization Bearer 鉴权;统一分页;业务公共 userInfo/ext/flow/notify/adminLog
- admin/command/FcInstall.php:19 张 fa_fc_* 表 + 种子数据,幂等可重跑
二、控制器 17 个
- 用户端 Auth/Home/Post/Member/Distribution/Message/Setting
- 后台端 AdminDashboard/AdminMerchant/AdminMember/AdminPost/AdminComment/
AdminDist/AdminOrder/AdminFinance/AdminRbac/AdminSetting
三、契约一致性
- 字段与小程序 common/mock/data.js 逐字段对齐,Mock 与真实接口可对拍
- 实现 BR-01~BR-12,错误文案与前端 mock 完全一致
- 详情联系方式强制脱敏,付费/会员方可解锁
四、验证
- tools/verify-api.mjs:63 端点契约 + 12 条业务规则 + 8 步双端闭环
- docs/05-后端实现规范.md
174 行
6.2 KiB
PHP
174 行
6.2 KiB
PHP
<?php
|
||
|
||
namespace app\api\controller;
|
||
|
||
use think\Db;
|
||
|
||
/**
|
||
* 用户端 · 分销中心(Module-DIST)
|
||
* 路由:
|
||
* GET /api/v1/distribution/overview D1 分销概览
|
||
* GET /api/v1/distribution/team D2 我的团队
|
||
* POST /api/v1/distribution/withdraw D3 申请提现
|
||
* GET /api/v1/distribution/withdraw-records D4 提现记录
|
||
*/
|
||
class Distribution extends V1
|
||
{
|
||
/**
|
||
* D1 分销概览
|
||
* GET /api/v1/distribution/overview → {totalCommission,settled,withdrawable,todayCommission,teamCount,l1Count,l2Count,inviteCode,posterUrl}
|
||
*/
|
||
public function overview()
|
||
{
|
||
$user = $this->auth();
|
||
$uid = (int)$user['id'];
|
||
|
||
$total = (float)Db::name('fc_commission')->where('user_id', $uid)->sum('amount');
|
||
$settled = (float)Db::name('fc_commission')->where('user_id', $uid)->where('status', 1)->sum('amount');
|
||
$applied = (float)Db::name('fc_withdraw')->where('user_id', $uid)
|
||
->where('status', 'in', ['auditing', 'paid'])->sum('amount');
|
||
$withdrawable = $settled - $applied;
|
||
|
||
$todayStart = strtotime(date('Y-m-d'));
|
||
$todayCommission = (float)Db::name('fc_commission')->where('user_id', $uid)
|
||
->where('createtime', '>=', $todayStart)->sum('amount');
|
||
|
||
$l1 = (int)Db::name('fc_dist_relation')->where('parent_id', $uid)->where('level', 1)->count();
|
||
$l2 = (int)Db::name('fc_dist_relation')->where('parent_id', $uid)->where('level', 2)->count();
|
||
|
||
$ext = $this->ext($uid);
|
||
|
||
$this->ok([
|
||
'totalCommission' => round($total, 2),
|
||
'settled' => round($settled, 2),
|
||
'withdrawable' => round($withdrawable, 2),
|
||
'todayCommission' => round($todayCommission, 2),
|
||
'teamCount' => $l1 + $l2,
|
||
'l1Count' => $l1,
|
||
'l2Count' => $l2,
|
||
'inviteCode' => (string)$ext['invite_code'],
|
||
'posterUrl' => '',
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* D2 我的团队
|
||
* GET /api/v1/distribution/team 入参 {level?,page,size}
|
||
* → 分页 {list:[{id,nickname,level,joinedAt,performance,commission}],page,size,total,hasMore}
|
||
*/
|
||
public function team()
|
||
{
|
||
$user = $this->auth();
|
||
$uid = (int)$user['id'];
|
||
list($page, $size) = $this->pager();
|
||
$level = $this->input('level', '');
|
||
|
||
$query = Db::name('fc_dist_relation')->alias('r')
|
||
->join('user u', 'u.id = r.user_id')
|
||
->where('r.parent_id', $uid);
|
||
if ($level !== '' && $level !== null) {
|
||
$query->where('r.level', (int)$level);
|
||
}
|
||
$total = $query->count();
|
||
$rows = $query->field('r.id,r.user_id,r.level,r.createtime,u.nickname')
|
||
->order('r.createtime desc')
|
||
->page($page, $size)
|
||
->select();
|
||
|
||
$list = [];
|
||
foreach ($rows as $row) {
|
||
$childId = (int)$row['user_id'];
|
||
$commission = (float)Db::name('fc_commission')->where('from_user_id', $childId)->sum('amount');
|
||
$orderAmount = (float)Db::name('fc_order')->where('user_id', $childId)->where('status', 'paid')->sum('amount');
|
||
$performance = $orderAmount > 0 ? $orderAmount : $commission * 5;
|
||
$list[] = [
|
||
'id' => $childId,
|
||
'nickname' => (string)$row['nickname'],
|
||
'level' => (int)$row['level'],
|
||
'joinedAt' => $this->iso($row['createtime']),
|
||
'performance' => round($performance, 2),
|
||
'commission' => round($commission, 2),
|
||
];
|
||
}
|
||
|
||
$this->ok($this->paginate($list, $total, $page, $size));
|
||
}
|
||
|
||
/**
|
||
* D3 申请提现
|
||
* POST /api/v1/distribution/withdraw 入参 {amount,channel?} → {withdrawNo,status:'auditing'}
|
||
* 规则 BR-09:金额必填且 >0、且不超过可提现余额
|
||
*/
|
||
public function withdraw()
|
||
{
|
||
$user = $this->auth();
|
||
$uid = (int)$user['id'];
|
||
$amount = (float)$this->input('amount', 0);
|
||
|
||
if ($amount <= 0) {
|
||
$this->fail('提现金额不合法');
|
||
}
|
||
if ($amount > $this->withdrawable($uid)) {
|
||
$this->fail('提现金额超过可提现余额');
|
||
}
|
||
|
||
$channel = trim((string)$this->input('channel', ''));
|
||
if ($channel === '') {
|
||
$channel = 'wxpay';
|
||
}
|
||
$amount = round($amount, 2);
|
||
$withdrawNo = $this->orderNo('WD');
|
||
|
||
Db::name('fc_withdraw')->insert([
|
||
'withdraw_no' => $withdrawNo,
|
||
'user_id' => $uid,
|
||
'amount' => $amount,
|
||
'channel' => $channel,
|
||
'status' => 'auditing',
|
||
'reason' => '',
|
||
'paytime' => null,
|
||
'createtime' => time(),
|
||
'updatetime' => time(),
|
||
]);
|
||
|
||
$this->notify($uid, 'system', '提现申请已提交', '提现 ¥' . $amount . ' 已提交,预计 T+7 到账');
|
||
|
||
$this->ok(['withdrawNo' => $withdrawNo, 'status' => 'auditing']);
|
||
}
|
||
|
||
/**
|
||
* D4 提现记录
|
||
* GET /api/v1/distribution/withdraw-records → [{id,withdrawNo,amount,status,createdAt}]
|
||
*/
|
||
public function withdrawRecords()
|
||
{
|
||
$user = $this->auth();
|
||
$rows = Db::name('fc_withdraw')->where('user_id', $user['id'])
|
||
->order('createtime desc')->select();
|
||
|
||
$list = [];
|
||
foreach ($rows as $row) {
|
||
$list[] = [
|
||
'id' => (int)$row['id'],
|
||
'withdrawNo' => (string)$row['withdraw_no'],
|
||
'amount' => round((float)$row['amount'], 2),
|
||
'status' => (string)$row['status'],
|
||
'createdAt' => $this->iso($row['createtime']),
|
||
];
|
||
}
|
||
|
||
$this->ok($list);
|
||
}
|
||
|
||
/**
|
||
* 可提现余额 = Σ已结算佣金(status=1) - Σ(申请中/已打款)提现
|
||
*/
|
||
protected function withdrawable($uid)
|
||
{
|
||
$settled = (float)Db::name('fc_commission')->where('user_id', $uid)->where('status', 1)->sum('amount');
|
||
$applied = (float)Db::name('fc_withdraw')->where('user_id', $uid)
|
||
->where('status', 'in', ['auditing', 'paid'])->sum('amount');
|
||
return $settled - $applied;
|
||
}
|
||
}
|