- 新增地址选择插件,支持地图位置选择并返回地址和经纬度 - 新增微信订阅消息发送功能,支持审核结果、提现通知等场景 - 新增订单支付状态查询、提现收款人姓名保存等接口 - 新增服务区域管理,支持按省市区划分并同步到信息发布 - 新增资金流水和营收报表导出功能 - 新增可用优惠券查询、订单退款拆分处理等功能 - 优化后台角标显示、首页数据统计、UI配置下发等 - 修复订单支付并发问题、地区管理逻辑等
212 行
8.7 KiB
PHP
212 行
8.7 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,minWithdraw}
|
||
*/
|
||
public function overview()
|
||
{
|
||
$user = $this->auth();
|
||
$uid = (int)$user['id'];
|
||
|
||
$total = (float)Db::name('fc_commission')->where('user_id', $uid)->where('status', '<>', 2)->sum('amount');
|
||
$settled = (float)Db::name('fc_commission')->where('user_id', $uid)->where('status', 1)->sum('amount');
|
||
$pending = (float)Db::name('fc_commission')->where('user_id', $uid)->where('status', 0)->sum('amount');
|
||
$applied = (float)Db::name('fc_withdraw')->where('user_id', $uid)
|
||
->where('status', 'in', ['auditing', 'paying', '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);
|
||
|
||
// 商家转账确认收款页需要的商户号与小程序 AppID(非敏感,可下发)
|
||
$epay = (array)get_addon_config('epay');
|
||
$wx = isset($epay['wechat']) && is_array($epay['wechat']) ? $epay['wechat'] : [];
|
||
|
||
$this->ok([
|
||
'totalCommission' => round($total, 2),
|
||
'settled' => round($settled, 2),
|
||
'pendingCommission' => round($pending, 2),
|
||
'withdrawable' => round(max(0, $withdrawable), 2),
|
||
'todayCommission' => round($todayCommission, 2),
|
||
'teamCount' => $l1 + $l2,
|
||
'l1Count' => $l1,
|
||
'l2Count' => $l2,
|
||
'inviteCode' => (string)$ext['invite_code'],
|
||
'posterUrl' => '',
|
||
// 最低提现金额(后台「分销设置」下发,替代前端写死的 50)
|
||
'minWithdraw' => (float)$this->cfg('dist.minWithdraw', 50),
|
||
// 提现收款人实名(未填写时前端需先采集)
|
||
'realname' => isset($ext['realname']) ? (string)$ext['realname'] : '',
|
||
'transferMchId' => isset($wx['mch_id']) ? (string)$wx['mch_id'] : '',
|
||
'transferAppId' => isset($wx['miniapp_id']) ? (string)$wx['miniapp_id'] : '',
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* 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'];
|
||
$level = $this->input('level', '');
|
||
|
||
// 闭包重建查询:TP5.0 的 count() 会清空 Query::options,不能复用同一对象
|
||
list($rows, $total, $page, $size) = $this->pageQuery(function ($forCount) use ($uid, $level) {
|
||
$q = Db::name('fc_dist_relation')->alias('r')
|
||
->join('user u', 'u.id = r.user_id')
|
||
->where('r.parent_id', $uid);
|
||
if ($level !== '' && $level !== null) {
|
||
$q->where('r.level', (int)$level);
|
||
}
|
||
if ($forCount) {
|
||
return $q;
|
||
}
|
||
return $q->field('r.id,r.user_id,r.level,r.createtime,u.nickname')
|
||
->order('r.createtime desc');
|
||
});
|
||
|
||
$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'];
|
||
$this->rateLimit('withdraw:' . $uid, 3, 60);
|
||
|
||
$amount = round((float)$this->input('amount', 0), 2);
|
||
$min = (float)$this->cfg('dist.minWithdraw', 50);
|
||
|
||
if ($amount <= 0) {
|
||
$this->fail('提现金额不合法');
|
||
}
|
||
if ($min > 0 && $amount < $min) {
|
||
$this->fail('最低提现金额为 ' . $min . ' 元');
|
||
}
|
||
|
||
// 微信商家转账会校验收款人实名,未填写姓名不允许提现
|
||
$ext = $this->ext($uid);
|
||
if (trim((string)(isset($ext['realname']) ? $ext['realname'] : '')) === '') {
|
||
$this->fail('请先填写收款人真实姓名(需与微信实名一致)');
|
||
}
|
||
|
||
$channel = trim((string)$this->input('channel', ''));
|
||
if ($channel === '') {
|
||
$channel = 'wxpay';
|
||
}
|
||
$withdrawNo = $this->orderNo('WD');
|
||
|
||
try {
|
||
Db::transaction(function () use ($uid, $amount, $channel, $withdrawNo) {
|
||
// 用户扩展行加锁:同一用户的提现申请串行化,避免并发双花
|
||
Db::name('fc_user_ext')->where('user_id', $uid)->lock(true)->find();
|
||
|
||
if ($amount > $this->withdrawable($uid)) {
|
||
throw new \RuntimeException('提现金额超过可提现余额');
|
||
}
|
||
|
||
Db::name('fc_withdraw')->insert([
|
||
'withdraw_no' => $withdrawNo,
|
||
'user_id' => $uid,
|
||
'amount' => $amount,
|
||
'channel' => $channel,
|
||
'status' => 'auditing',
|
||
'reason' => '',
|
||
'paytime' => null,
|
||
'createtime' => time(),
|
||
'updatetime' => time(),
|
||
]);
|
||
});
|
||
} catch (\Throwable $e) {
|
||
$this->fail($e->getMessage() ?: '提现申请失败,请重试');
|
||
}
|
||
|
||
$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'],
|
||
'transferStatus' => (string)(isset($row['transfer_status']) ? $row['transfer_status'] : ''),
|
||
'packageInfo' => (string)(isset($row['package_info']) ? $row['package_info'] : ''),
|
||
'failReason' => (string)(isset($row['fail_reason']) ? $row['fail_reason'] : ''),
|
||
'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', 'paying', 'paid'])->sum('amount');
|
||
return $settled - $applied;
|
||
}
|
||
}
|