AgentApply.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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([
  106. 'invite_code' => $inviteCode,
  107. 'status' => AgentModel::AGENT_STATUS_NORMAL
  108. ])->find();
  109. if (!$parentAgent) {
  110. $this->error('邀请码无效或代理商状态异常');
  111. }
  112. // 获取上级用户信息
  113. $parentUser = UserModel::find($parentAgent->user_id);
  114. if (!$parentUser) {
  115. $this->error('上级用户不存在');
  116. }
  117. // 不能绑定自己
  118. if ($parentAgent->user_id == $user->id) {
  119. $this->error('不能绑定自己为上级');
  120. }
  121. // 检查是否已经有上级了
  122. if (!empty($user->parent_user_id)) {
  123. $this->error('您已经有上级了,无法重复绑定');
  124. }
  125. // 防止循环绑定:检查目标上级是否已经是当前用户的下级
  126. if ($this->isUserInDownline($user->id, $parentAgent->user_id)) {
  127. $this->error('不能绑定下级用户为上级');
  128. }
  129. // 更新用户的上级关系
  130. $user->parent_user_id = $parentAgent->user_id;
  131. $user->bind_time = time();
  132. $user->save();
  133. // 从请求头获取platform参数(与生成二维码接口保持一致)
  134. $requestPlatform = $this->request->header('platform', '');
  135. if (!$requestPlatform) {
  136. $requestPlatform = 'WechatMiniProgram'; // 默认微信小程序
  137. }
  138. // 从请求头获取from参数(来源平台)
  139. $requestFrom = $this->request->header('from', $requestPlatform);
  140. // 将ChannelEnum映射到ShareEnum平台常量
  141. $shareEnumPlatform = $this->mapChannelToSharePlatform($requestPlatform);
  142. $shareEnumFrom = $this->mapChannelToSharePlatform($requestFrom);
  143. // 使用ShareEnum获取平台ID
  144. $platformId = ShareEnum::getPlatformId($shareEnumPlatform);
  145. $fromPlatformId = ShareEnum::getPlatformId($shareEnumFrom);
  146. // 构造分享记录参数,模拟通过邀请海报访问
  147. $spmParams = sprintf('%s.%s.%s.%s.%s',
  148. $parentAgent->user_id, // shareId: 邀请人ID
  149. PageTypeEnum::AGENT_POSTER, // pageType: 分销海报页
  150. $parentAgent->user_id, // query: 代理商ID
  151. $platformId, // platform: 从请求头获取
  152. $fromPlatformId // from: 从请求头获取
  153. );
  154. // 添加分享记录
  155. $shareParams = [
  156. 'spm' => $spmParams,
  157. 'shareId' => $parentAgent->user_id,
  158. 'page' => PageTypeEnum::AGENT_POSTER,
  159. 'query' => $parentAgent->user_id,
  160. 'platform' => $shareEnumPlatform,
  161. 'from' => ShareEnum::FROM_POSTER
  162. ];
  163. $shareResult = ShareService::addShareLog($user->id, $shareParams);
  164. $this->success('绑定上级成功', [
  165. 'parent_info' => [
  166. 'user_id' => $parentAgent->user_id,
  167. 'nickname' => $parentUser->nickname ?? '',
  168. 'avatar' => $parentUser->avatar ?? '',
  169. 'invite_code' => $inviteCode
  170. ],
  171. 'share_record' => $shareResult ? true : false
  172. ]);
  173. } catch (Exception $e) {
  174. $this->error($e->getMessage());
  175. }
  176. }
  177. /**
  178. * 将ChannelEnum映射到ShareEnum平台常量
  179. * @param string $channelPlatform
  180. * @return string
  181. */
  182. private function mapChannelToSharePlatform($channelPlatform)
  183. {
  184. $channelToShareMap = [
  185. 'H5' => ShareEnum::PLATFORM_H5,
  186. 'WechatOfficialAccount' => ShareEnum::PLATFORM_WECHAT_OFFICIAL_ACCOUNT,
  187. 'WechatMiniProgram' => ShareEnum::PLATFORM_WECHAT_MINI_PROGRAM,
  188. 'IosApp' => ShareEnum::PLATFORM_APP,
  189. 'AndroidApp' => ShareEnum::PLATFORM_APP,
  190. ];
  191. return $channelToShareMap[$channelPlatform] ?? ShareEnum::PLATFORM_WECHAT_MINI_PROGRAM;
  192. }
  193. /**
  194. * 检查目标用户是否在指定用户的下级链条中(防止循环绑定)
  195. * @param int $parentUserId 上级用户ID
  196. * @param int $targetUserId 要检查的目标用户ID
  197. * @param int $maxDepth 最大检查深度,防止无限递归
  198. * @param int $currentDepth 当前递归深度
  199. * @return bool true-目标用户在下级链条中,false-不在
  200. */
  201. private function isUserInDownline($parentUserId, $targetUserId, $maxDepth = 10, $currentDepth = 0)
  202. {
  203. // 防止无限递归
  204. if ($currentDepth >= $maxDepth) {
  205. return false;
  206. }
  207. // 查找所有以 $parentUserId 为上级的用户
  208. $childrenUsers = UserModel::where('parent_user_id', $parentUserId)->column('id');
  209. if (empty($childrenUsers)) {
  210. return false;
  211. }
  212. // 直接检查:目标用户是否是直接下级
  213. if (in_array($targetUserId, $childrenUsers)) {
  214. return true;
  215. }
  216. // 递归检查:目标用户是否在任何一个下级的下级链条中
  217. foreach ($childrenUsers as $childUserId) {
  218. if ($this->isUserInDownline($childUserId, $targetUserId, $maxDepth, $currentDepth + 1)) {
  219. return true;
  220. }
  221. }
  222. return false;
  223. }
  224. }