AgentApply.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. $user->parent_user_id = $parentAgent->user_id;
  126. $user->save();
  127. // 从请求头获取platform参数(与生成二维码接口保持一致)
  128. $requestPlatform = $this->request->header('platform', '');
  129. if (!$requestPlatform) {
  130. $requestPlatform = 'WechatMiniProgram'; // 默认微信小程序
  131. }
  132. // 从请求头获取from参数(来源平台)
  133. $requestFrom = $this->request->header('from', $requestPlatform);
  134. // 将ChannelEnum映射到ShareEnum平台常量
  135. $shareEnumPlatform = $this->mapChannelToSharePlatform($requestPlatform);
  136. $shareEnumFrom = $this->mapChannelToSharePlatform($requestFrom);
  137. // 使用ShareEnum获取平台ID
  138. $platformId = ShareEnum::getPlatformId($shareEnumPlatform);
  139. $fromPlatformId = ShareEnum::getPlatformId($shareEnumFrom);
  140. // 构造分享记录参数,模拟通过邀请海报访问
  141. $spmParams = sprintf('%s.%s.%s.%s.%s',
  142. $parentAgent->user_id, // shareId: 邀请人ID
  143. PageTypeEnum::AGENT_POSTER, // pageType: 分销海报页
  144. $parentAgent->user_id, // query: 代理商ID
  145. $platformId, // platform: 从请求头获取
  146. $fromPlatformId // from: 从请求头获取
  147. );
  148. // 添加分享记录
  149. $shareParams = [
  150. 'spm' => $spmParams,
  151. 'shareId' => $parentAgent->user_id,
  152. 'page' => PageTypeEnum::AGENT_POSTER,
  153. 'query' => $parentAgent->user_id,
  154. 'platform' => $shareEnumPlatform,
  155. 'from' => ShareEnum::FROM_POSTER
  156. ];
  157. $shareResult = ShareService::addShareLog($user->id, $shareParams);
  158. $this->success('绑定上级成功', [
  159. 'parent_info' => [
  160. 'user_id' => $parentAgent->user_id,
  161. 'nickname' => $parentUser->nickname ?? '',
  162. 'avatar' => $parentUser->avatar ?? '',
  163. 'invite_code' => $inviteCode
  164. ],
  165. 'share_record' => $shareResult ? true : false
  166. ]);
  167. } catch (Exception $e) {
  168. $this->error($e->getMessage());
  169. }
  170. }
  171. /**
  172. * 将ChannelEnum映射到ShareEnum平台常量
  173. * @param string $channelPlatform
  174. * @return string
  175. */
  176. private function mapChannelToSharePlatform($channelPlatform)
  177. {
  178. $channelToShareMap = [
  179. 'H5' => ShareEnum::PLATFORM_H5,
  180. 'WechatOfficialAccount' => ShareEnum::PLATFORM_WECHAT_OFFICIAL_ACCOUNT,
  181. 'WechatMiniProgram' => ShareEnum::PLATFORM_WECHAT_MINI_PROGRAM,
  182. 'IosApp' => ShareEnum::PLATFORM_APP,
  183. 'AndroidApp' => ShareEnum::PLATFORM_APP,
  184. ];
  185. return $channelToShareMap[$channelPlatform] ?? ShareEnum::PLATFORM_WECHAT_MINI_PROGRAM;
  186. }
  187. }