getIdentityList(); $this->success('获取成功', $identities); } catch (Exception $e) { $this->error($e->getMessage()); } } /** * 检查代理商身份是否需要地区信息 */ public function checkAreaRequirement() { try { $identityId = $this->request->param('agent_identity_id'); if (empty($identityId)) { $this->error('代理商身份ID不能为空'); } $requirement = AgentApplyValidate::getAreaRequirement($identityId); $this->success('获取成功', $requirement); } catch (Exception $e) { $this->error($e->getMessage()); } } /** * 提交代理商申请 */ public function apply() { try { $user = auth_user(); $data = $this->request->param(); // 使用验证器验证参数 $validate = new AgentApplyValidate(); if (!$validate->scene('apply')->check($data)) { $this->error($validate->getError()); } $service = new AgentApplyService(); $apply = $service->submitApply($user->id, $data); if ($apply->status == ApplyModel::STATUS_APPROVED) { $this->success('申请提交成功,您已成为代理商!', $apply); } else { $this->success('申请提交成功,请等待审核!', $apply); } } catch (Exception $e) { $this->error($e->getMessage()); } } /** * 获取申请状态 */ public function status() { try { $user = auth_user(); $service = new AgentApplyService(); $apply = $service->getUserApply($user->id); if (!$apply) { $this->success('未找到申请记录', null); } // 增强申请数据信息 $data = $apply->toArray(); $this->success('获取成功', $data); } catch (Exception $e) { $this->error($e->getMessage()); } } /** * 绑定上级代理商(通过邀请码) */ public function bindParent() { try { $user = auth_user(); $data = $this->request->param(); // 使用验证器验证参数 $validate = new AgentApplyValidate(); if (!$validate->scene('bindParent')->check($data)) { $this->error($validate->getError()); } $inviteCode = $data['invite_code']; // 查询邀请码对应的代理商 $parentAgent = AgentModel::where([ 'invite_code' => $inviteCode, 'status' => AgentModel::AGENT_STATUS_NORMAL ])->find(); if (!$parentAgent) { $this->error('邀请码无效或代理商状态异常'); } // 获取上级用户信息 $parentUser = UserModel::find($parentAgent->user_id); if (!$parentUser) { $this->error('上级用户不存在'); } // 不能绑定自己 if ($parentAgent->user_id == $user->id) { $this->error('不能绑定自己为上级'); } // 检查是否已经有上级了 if (!empty($user->parent_user_id)) { $this->error('您已经有上级了,无法重复绑定'); } // 防止循环绑定:检查目标上级是否已经是当前用户的下级 if ($this->isUserInDownline($user->id, $parentAgent->user_id)) { $this->error('不能绑定下级用户为上级'); } // 更新用户的上级关系 $user->parent_user_id = $parentAgent->user_id; $user->bind_time = time(); $user->save(); // 从请求头获取platform参数(与生成二维码接口保持一致) $requestPlatform = $this->request->header('platform', ''); if (!$requestPlatform) { $requestPlatform = 'WechatMiniProgram'; // 默认微信小程序 } // 从请求头获取from参数(来源平台) $requestFrom = $this->request->header('from', $requestPlatform); // 将ChannelEnum映射到ShareEnum平台常量 $shareEnumPlatform = $this->mapChannelToSharePlatform($requestPlatform); $shareEnumFrom = $this->mapChannelToSharePlatform($requestFrom); // 使用ShareEnum获取平台ID $platformId = ShareEnum::getPlatformId($shareEnumPlatform); $fromPlatformId = ShareEnum::getPlatformId($shareEnumFrom); // 构造分享记录参数,模拟通过邀请海报访问 $spmParams = sprintf('%s.%s.%s.%s.%s', $parentAgent->user_id, // shareId: 邀请人ID PageTypeEnum::AGENT_POSTER, // pageType: 分销海报页 $parentAgent->user_id, // query: 代理商ID $platformId, // platform: 从请求头获取 $fromPlatformId // from: 从请求头获取 ); // 添加分享记录 $shareParams = [ 'spm' => $spmParams, 'shareId' => $parentAgent->user_id, 'page' => PageTypeEnum::AGENT_POSTER, 'query' => $parentAgent->user_id, 'platform' => $shareEnumPlatform, 'from' => ShareEnum::FROM_POSTER ]; $shareResult = ShareService::addShareLog($user->id, $shareParams); $this->success('绑定上级成功', [ 'parent_info' => [ 'user_id' => $parentAgent->user_id, 'nickname' => $parentUser->nickname ?? '', 'avatar' => $parentUser->avatar ?? '', 'invite_code' => $inviteCode ], 'share_record' => $shareResult ? true : false ]); } catch (Exception $e) { $this->error($e->getMessage()); } } /** * 将ChannelEnum映射到ShareEnum平台常量 * @param string $channelPlatform * @return string */ private function mapChannelToSharePlatform($channelPlatform) { $channelToShareMap = [ 'H5' => ShareEnum::PLATFORM_H5, 'WechatOfficialAccount' => ShareEnum::PLATFORM_WECHAT_OFFICIAL_ACCOUNT, 'WechatMiniProgram' => ShareEnum::PLATFORM_WECHAT_MINI_PROGRAM, 'IosApp' => ShareEnum::PLATFORM_APP, 'AndroidApp' => ShareEnum::PLATFORM_APP, ]; return $channelToShareMap[$channelPlatform] ?? ShareEnum::PLATFORM_WECHAT_MINI_PROGRAM; } /** * 检查目标用户是否在指定用户的下级链条中(防止循环绑定) * @param int $parentUserId 上级用户ID * @param int $targetUserId 要检查的目标用户ID * @param int $maxDepth 最大检查深度,防止无限递归 * @param int $currentDepth 当前递归深度 * @return bool true-目标用户在下级链条中,false-不在 */ private function isUserInDownline($parentUserId, $targetUserId, $maxDepth = 10, $currentDepth = 0) { // 防止无限递归 if ($currentDepth >= $maxDepth) { return false; } // 查找所有以 $parentUserId 为上级的用户 $childrenUsers = UserModel::where('parent_user_id', $parentUserId)->column('id'); if (empty($childrenUsers)) { return false; } // 直接检查:目标用户是否是直接下级 if (in_array($targetUserId, $childrenUsers)) { return true; } // 递归检查:目标用户是否在任何一个下级的下级链条中 foreach ($childrenUsers as $childUserId) { if ($this->isUserInDownline($childUserId, $targetUserId, $maxDepth, $currentDepth + 1)) { return true; } } return false; } }