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) ->where('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('您已经有上级了,无法重复绑定'); } // 更新用户的上级关系 $user->parent_user_id = $parentAgent->user_id; $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; } }