AgentApply.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. namespace app\api\controller\commission;
  3. use app\common\Service\Commission\AgentApply as AgentApplyService;
  4. use app\common\model\commission\Apply as ApplyModel;
  5. use app\common\model\commission\Agent as AgentModel;
  6. use app\common\model\User as UserModel;
  7. use app\common\Service\Share\ShareService;
  8. use app\api\validate\AgentApply as AgentApplyValidate;
  9. use app\common\Enum\ShareEnum;
  10. use app\common\Enum\PageTypeEnum;
  11. use app\common\Enum\ChannelEnum;
  12. use think\Exception;
  13. use app\api\controller\Base;
  14. class AgentApply extends Base
  15. {
  16. protected $noNeedLogin = ['identities', 'areas', 'checkAreaRequirement'];
  17. protected $noNeedRight = ['*'];
  18. /**
  19. * 获取代理商身份列表
  20. */
  21. public function identities()
  22. {
  23. try {
  24. $service = new AgentApplyService();
  25. $identities = $service->getIdentityList();
  26. $this->success('获取成功', $identities);
  27. } catch (Exception $e) {
  28. $this->error($e->getMessage());
  29. }
  30. }
  31. /**
  32. * 检查代理商身份是否需要地区信息
  33. */
  34. public function checkAreaRequirement()
  35. {
  36. try {
  37. $identityId = $this->request->param('agent_identity_id');
  38. if (empty($identityId)) {
  39. $this->error('代理商身份ID不能为空');
  40. }
  41. $requirement = AgentApplyValidate::getAreaRequirement($identityId);
  42. $this->success('获取成功', $requirement);
  43. } catch (Exception $e) {
  44. $this->error($e->getMessage());
  45. }
  46. }
  47. /**
  48. * 提交代理商申请
  49. */
  50. public function apply()
  51. {
  52. try {
  53. $user = auth_user();
  54. $data = $this->request->param();
  55. // 使用验证器验证参数
  56. $validate = new AgentApplyValidate();
  57. if (!$validate->scene('apply')->check($data)) {
  58. $this->error($validate->getError());
  59. }
  60. $service = new AgentApplyService();
  61. $apply = $service->submitApply($user->id, $data);
  62. if ($apply->status == ApplyModel::STATUS_APPROVED) {
  63. $this->success('申请提交成功,您已成为代理商!', $apply);
  64. } else {
  65. $this->success('申请提交成功,请等待审核!', $apply);
  66. }
  67. } catch (Exception $e) {
  68. $this->error($e->getMessage());
  69. }
  70. }
  71. /**
  72. * 获取申请状态
  73. */
  74. public function status()
  75. {
  76. try {
  77. $user = auth_user();
  78. $service = new AgentApplyService();
  79. $apply = $service->getUserApply($user->id);
  80. if (!$apply) {
  81. $this->success('未找到申请记录', null);
  82. }
  83. // 增强申请数据信息
  84. $data = $apply->toArray();
  85. $this->success('获取成功', $data);
  86. } catch (Exception $e) {
  87. $this->error($e->getMessage());
  88. }
  89. }
  90. /**
  91. * 绑定上级代理商(通过邀请码)
  92. */
  93. public function bindParent()
  94. {
  95. try {
  96. $user = auth_user();
  97. $data = $this->request->param();
  98. // 使用验证器验证参数
  99. $validate = new AgentApplyValidate();
  100. if (!$validate->scene('bindParent')->check($data)) {
  101. $this->error($validate->getError());
  102. }
  103. $inviteCode = $data['invite_code'];
  104. // 查询邀请码对应的代理商
  105. $parentAgent = AgentModel::where('invite_code', $inviteCode)
  106. ->where('status', AgentModel::AGENT_STATUS_NORMAL)
  107. ->find();
  108. if (!$parentAgent) {
  109. $this->error('邀请码无效或代理商状态异常');
  110. }
  111. // 获取上级用户信息
  112. $parentUser = UserModel::find($parentAgent->user_id);
  113. if (!$parentUser) {
  114. $this->error('上级用户不存在');
  115. }
  116. // 不能绑定自己
  117. if ($parentAgent->user_id == $user->id) {
  118. $this->error('不能绑定自己为上级');
  119. }
  120. // 检查是否已经有上级了
  121. if (!empty($user->parent_user_id)) {
  122. $this->error('您已经有上级了,无法重复绑定');
  123. }
  124. // 防止循环绑定:检查目标上级是否已经是当前用户的下级
  125. if ($this->isUserInDownline($user->id, $parentAgent->user_id)) {
  126. $this->error('不能绑定下级用户为上级');
  127. }
  128. // 更新用户的上级关系
  129. $user->parent_user_id = $parentAgent->user_id;
  130. $user->bind_time = time();
  131. $user->save();
  132. // 从请求头获取platform参数(与生成二维码接口保持一致)
  133. $requestPlatform = $this->request->header('platform', '');
  134. if (!$requestPlatform) {
  135. $requestPlatform = 'WechatMiniProgram'; // 默认微信小程序
  136. }
  137. // 从请求头获取from参数(来源平台)
  138. $requestFrom = $this->request->header('from', $requestPlatform);
  139. // 将ChannelEnum映射到ShareEnum平台常量
  140. $shareEnumPlatform = $this->mapChannelToSharePlatform($requestPlatform);
  141. $shareEnumFrom = $this->mapChannelToSharePlatform($requestFrom);
  142. // 使用ShareEnum获取平台ID
  143. $platformId = ShareEnum::getPlatformId($shareEnumPlatform);
  144. $fromPlatformId = ShareEnum::getPlatformId($shareEnumFrom);
  145. // 构造分享记录参数,模拟通过邀请海报访问
  146. $spmParams = sprintf('%s.%s.%s.%s.%s',
  147. $parentAgent->user_id, // shareId: 邀请人ID
  148. PageTypeEnum::AGENT_POSTER, // pageType: 分销海报页
  149. $parentAgent->user_id, // query: 代理商ID
  150. $platformId, // platform: 从请求头获取
  151. $fromPlatformId // from: 从请求头获取
  152. );
  153. // 添加分享记录
  154. $shareParams = [
  155. 'spm' => $spmParams,
  156. 'shareId' => $parentAgent->user_id,
  157. 'page' => PageTypeEnum::AGENT_POSTER,
  158. 'query' => $parentAgent->user_id,
  159. 'platform' => $shareEnumPlatform,
  160. 'from' => ShareEnum::FROM_POSTER
  161. ];
  162. $shareResult = ShareService::addShareLog($user->id, $shareParams);
  163. $this->success('绑定上级成功', [
  164. 'parent_info' => [
  165. 'user_id' => $parentAgent->user_id,
  166. 'nickname' => $parentUser->nickname ?? '',
  167. 'avatar' => $parentUser->avatar ?? '',
  168. 'invite_code' => $inviteCode
  169. ],
  170. 'share_record' => $shareResult ? true : false
  171. ]);
  172. } catch (Exception $e) {
  173. $this->error($e->getMessage());
  174. }
  175. }
  176. /**
  177. * 将ChannelEnum映射到ShareEnum平台常量
  178. * @param string $channelPlatform
  179. * @return string
  180. */
  181. private function mapChannelToSharePlatform($channelPlatform)
  182. {
  183. $channelToShareMap = [
  184. 'H5' => ShareEnum::PLATFORM_H5,
  185. 'WechatOfficialAccount' => ShareEnum::PLATFORM_WECHAT_OFFICIAL_ACCOUNT,
  186. 'WechatMiniProgram' => ShareEnum::PLATFORM_WECHAT_MINI_PROGRAM,
  187. 'IosApp' => ShareEnum::PLATFORM_APP,
  188. 'AndroidApp' => ShareEnum::PLATFORM_APP,
  189. ];
  190. return $channelToShareMap[$channelPlatform] ?? ShareEnum::PLATFORM_WECHAT_MINI_PROGRAM;
  191. }
  192. /**
  193. * 检查目标用户是否在指定用户的下级链条中(防止循环绑定)
  194. * @param int $parentUserId 上级用户ID
  195. * @param int $targetUserId 要检查的目标用户ID
  196. * @param int $maxDepth 最大检查深度,防止无限递归
  197. * @param int $currentDepth 当前递归深度
  198. * @return bool true-目标用户在下级链条中,false-不在
  199. */
  200. private function isUserInDownline($parentUserId, $targetUserId, $maxDepth = 10, $currentDepth = 0)
  201. {
  202. // 防止无限递归
  203. if ($currentDepth >= $maxDepth) {
  204. return false;
  205. }
  206. // 查找所有以 $parentUserId 为上级的用户
  207. $childrenUsers = UserModel::where('parent_user_id', $parentUserId)->column('id');
  208. if (empty($childrenUsers)) {
  209. return false;
  210. }
  211. // 直接检查:目标用户是否是直接下级
  212. if (in_array($targetUserId, $childrenUsers)) {
  213. return true;
  214. }
  215. // 递归检查:目标用户是否在任何一个下级的下级链条中
  216. foreach ($childrenUsers as $childUserId) {
  217. if ($this->isUserInDownline($childUserId, $targetUserId, $maxDepth, $currentDepth + 1)) {
  218. return true;
  219. }
  220. }
  221. return false;
  222. }
  223. }