feat(commission): 移除二级间推并支持一级直推多层级下钻与防越权鉴权
这个提交包含在:
@@ -123,37 +123,62 @@ class Agent extends Commission
|
||||
$this->success('提交成功');
|
||||
}
|
||||
|
||||
// 我的团队
|
||||
// 我的团队(支持直推查看与安全下钻)
|
||||
public function team()
|
||||
{
|
||||
$agentId = $this->service->user->id;
|
||||
$levelType = $this->request->param('level_type', '1'); // 1=直推, 2=间推, all=全部
|
||||
$drillUserId = (int) $this->request->param('drill_user_id', 0);
|
||||
$filterType = $this->request->param('filter_type', 'all'); // all, partner, active, expiring, inactive
|
||||
$keyword = trim((string) $this->request->param('keyword', ''));
|
||||
$sort = $this->request->param('sort', 'createtime'); // createtime, child_count, consume
|
||||
$listRows = (int) $this->request->param('list_rows', 10);
|
||||
|
||||
// 1. 获取直推一级成员 ID 列表
|
||||
$level1Ids = UserModel::where('parent_user_id', $agentId)
|
||||
$targetUserId = $agentId;
|
||||
$drillUser = null;
|
||||
|
||||
// 1. 如果传了下钻目标成员,严格执行树状归属鉴权,防越权防注入
|
||||
if ($drillUserId > 0 && $drillUserId !== $agentId) {
|
||||
$targetUser = UserModel::where('id', $drillUserId)->where('status', 'normal')->find();
|
||||
if (!$targetUser) {
|
||||
$this->error('指定成员不存在或已注销');
|
||||
}
|
||||
|
||||
// 向上追溯层级,确保目标成员在当前用户的团队树中
|
||||
$isDownline = false;
|
||||
$currParentId = $targetUser->parent_user_id;
|
||||
$depth = 0;
|
||||
while ($currParentId > 0 && $depth < 30) {
|
||||
if ($currParentId == $agentId) {
|
||||
$isDownline = true;
|
||||
break;
|
||||
}
|
||||
$currParentId = UserModel::where('id', $currParentId)->value('parent_user_id') ?: 0;
|
||||
$depth++;
|
||||
}
|
||||
|
||||
if (!$isDownline) {
|
||||
$this->error('无权查看非直属团队成员信息');
|
||||
}
|
||||
|
||||
$targetUserId = $drillUserId;
|
||||
$drillUser = [
|
||||
'id' => $targetUser->id,
|
||||
'nickname' => $targetUser->nickname ?: '微信用户',
|
||||
'avatar' => $targetUser->avatar,
|
||||
'parent_user_id' => $targetUser->parent_user_id,
|
||||
];
|
||||
}
|
||||
|
||||
// 2. 获取目标用户的直推一级成员 ID 列表(全面采用一级直推模型)
|
||||
$level1Ids = UserModel::where('parent_user_id', $targetUserId)
|
||||
->where('status', 'normal')
|
||||
->column('id');
|
||||
|
||||
// 2. 根据 level_type 确定父级范围
|
||||
// 3. 构建直推成员查询
|
||||
$query = UserModel::alias('u')->where('u.status', 'normal');
|
||||
if ($levelType == '2') {
|
||||
if (empty($level1Ids)) {
|
||||
$query->whereRaw('1=0');
|
||||
} else {
|
||||
$query->whereIn('u.parent_user_id', $level1Ids);
|
||||
}
|
||||
} elseif ($levelType == 'all') {
|
||||
$allParentIds = array_merge([$agentId], $level1Ids);
|
||||
$query->whereIn('u.parent_user_id', $allParentIds);
|
||||
} else {
|
||||
$query->where('u.parent_user_id', $agentId);
|
||||
}
|
||||
$query->where('u.parent_user_id', $targetUserId);
|
||||
|
||||
// 3. 关键字搜索 (昵称/手机号/ID)
|
||||
// 4. 关键字搜索 (昵称/手机号/ID)
|
||||
if ($keyword !== '') {
|
||||
$query->where(function ($q) use ($keyword) {
|
||||
if (is_numeric($keyword)) {
|
||||
@@ -166,12 +191,12 @@ class Agent extends Commission
|
||||
});
|
||||
}
|
||||
|
||||
// 4. 计算活跃度时间窗口 (默认30天保活,剩余<=5天为临期)
|
||||
// 5. 计算活跃度时间窗口 (默认30天保活,剩余<=5天为临期)
|
||||
$windowDays = \addons\shopro\service\partner\PartnerConfig::activeDays();
|
||||
$windowStart = (new \addons\shopro\service\partner\ActiveService())->windowStart();
|
||||
$expiringStart = time() - ($windowDays - 5) * 86400;
|
||||
|
||||
// 5. 身份与活跃状态过滤
|
||||
// 6. 身份与活跃状态过滤
|
||||
if ($filterType === 'partner') {
|
||||
$partnerUserIds = AgentModel::where('status', 'in', ['normal', 'freeze'])
|
||||
->where(function ($q) {
|
||||
@@ -207,7 +232,7 @@ class Agent extends Commission
|
||||
}
|
||||
}
|
||||
|
||||
// 6. 排序处理
|
||||
// 7. 排序处理
|
||||
if ($sort === 'child_count') {
|
||||
$agentTable = (new AgentModel())->getTable();
|
||||
$query->join($agentTable . ' a', 'u.id = a.user_id', 'LEFT')
|
||||
@@ -220,7 +245,7 @@ class Agent extends Commission
|
||||
}
|
||||
$query->field('u.*');
|
||||
|
||||
// 7. 分页并加载推荐人与分销商信息
|
||||
// 8. 分页并加载推荐人与分销商信息
|
||||
$data = $query->with([
|
||||
'parentUser' => function ($q) {
|
||||
$q->field('id, nickname, avatar, mobile');
|
||||
@@ -267,14 +292,14 @@ class Agent extends Commission
|
||||
$user->child_user_count_1 = 0;
|
||||
}
|
||||
|
||||
$user->team_level = ($user->parent_user_id == $agentId) ? 1 : 2;
|
||||
$user->team_level = 1;
|
||||
$rawMobile = $user->mobile ? (string) $user->mobile : '';
|
||||
$maskedMobile = $rawMobile ? (strlen($rawMobile) == 11 ? substr_replace($rawMobile, '****', 3, 4) : $rawMobile) : '';
|
||||
$user->mobile = $maskedMobile;
|
||||
$user->mobile_hide = $maskedMobile;
|
||||
$user->mobile_raw = $rawMobile;
|
||||
|
||||
// 间推所属推荐人手机号脱敏
|
||||
// 所属推荐人手机号脱敏
|
||||
if ($user->parentUser && $user->parentUser->mobile) {
|
||||
$pMobile = (string) $user->parentUser->mobile;
|
||||
$pMasked = strlen($pMobile) == 11 ? substr_replace($pMobile, '****', 3, 4) : $pMobile;
|
||||
@@ -283,28 +308,26 @@ class Agent extends Commission
|
||||
}
|
||||
});
|
||||
|
||||
// 9. 计算整体团队健康度与综合收益业绩统计
|
||||
$summary = $this->getTeamHealthSummary($agentId, $level1Ids, $windowStart, $expiringStart);
|
||||
// 9. 计算整体团队健康度与综合收益业绩统计(基于当前直推用户)
|
||||
$summary = $this->getTeamHealthSummary($targetUserId, $level1Ids, $windowStart, $expiringStart);
|
||||
|
||||
$result = $data->toArray();
|
||||
$result['summary'] = $summary;
|
||||
$result['drill_user'] = $drillUser;
|
||||
|
||||
$this->success("", $result);
|
||||
$this->success("获取成功", $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算团队保活健康度与综合收益统计
|
||||
* 计算团队保活健康度与综合收益统计(全面直推口径)
|
||||
*/
|
||||
protected function getTeamHealthSummary($agentId, $level1Ids, $windowStart, $expiringStart)
|
||||
protected function getTeamHealthSummary($targetUserId, $level1Ids, $windowStart, $expiringStart)
|
||||
{
|
||||
$allMembers = array_merge([$agentId], $level1Ids);
|
||||
$teamUserIds = UserModel::whereIn('parent_user_id', $allMembers)
|
||||
->where('status', 'normal')
|
||||
->column('id');
|
||||
$teamUserIds = !empty($level1Ids) ? $level1Ids : [];
|
||||
|
||||
if (empty($teamUserIds)) {
|
||||
$selfAgent = AgentModel::where('user_id', $agentId)->find();
|
||||
$selfUser = UserModel::where('id', $agentId)->find();
|
||||
$selfAgent = AgentModel::where('user_id', $targetUserId)->find();
|
||||
$selfUser = UserModel::where('id', $targetUserId)->find();
|
||||
return [
|
||||
'total_count' => 0,
|
||||
'active_count' => 0,
|
||||
@@ -351,8 +374,8 @@ class Agent extends Commission
|
||||
}
|
||||
|
||||
// 1. 团队累计自购总业绩与团队订单总数(统一使用包含本人自购的分销团队全口径)
|
||||
$selfAgent = AgentModel::where('user_id', $agentId)->find();
|
||||
$selfUser = UserModel::where('id', $agentId)->find();
|
||||
$selfAgent = AgentModel::where('user_id', $targetUserId)->find();
|
||||
$selfUser = UserModel::where('id', $targetUserId)->find();
|
||||
|
||||
$teamTotalConsume = isset($selfAgent->child_order_money_all) && floatval($selfAgent->child_order_money_all) > 0
|
||||
? floatval($selfAgent->child_order_money_all)
|
||||
@@ -362,13 +385,13 @@ class Agent extends Commission
|
||||
? intval($selfAgent->child_order_count_all)
|
||||
: ($teamOrderCount + intval($selfAgent->child_order_count_0 ?? 0));
|
||||
|
||||
// 2. 团队成员为当前用户贡献的累计收益与本月收益
|
||||
// 2. 团队成员为目标用户贡献的累计收益与本月收益
|
||||
$monthStart = strtotime(date('Y-m-01 00:00:00'));
|
||||
$teamTotalReward = RewardModel::where('user_id', $agentId)
|
||||
$teamTotalReward = RewardModel::where('user_id', $targetUserId)
|
||||
->whereIn('buyer_id', $teamUserIds)
|
||||
->where('status', 1)
|
||||
->sum('amount');
|
||||
$teamMonthReward = RewardModel::where('user_id', $agentId)
|
||||
$teamMonthReward = RewardModel::where('user_id', $targetUserId)
|
||||
->whereIn('buyer_id', $teamUserIds)
|
||||
->where('status', 1)
|
||||
->where('createtime', '>=', $monthStart)
|
||||
|
||||
在新工单中引用
屏蔽一个用户