AgentApply.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. // 从请求头获取platform参数(与生成二维码接口保持一致)
  130. $requestPlatform = $this->request->header('platform', '');
  131. if (!$requestPlatform) {
  132. $requestPlatform = 'WechatMiniProgram'; // 默认微信小程序
  133. }
  134. // 从请求头获取from参数(来源平台)
  135. $requestFrom = $this->request->header('from', $requestPlatform);
  136. // 将ChannelEnum映射到ShareEnum平台常量
  137. $shareEnumPlatform = $this->mapChannelToSharePlatform($requestPlatform);
  138. $shareEnumFrom = $this->mapChannelToSharePlatform($requestFrom);
  139. // 使用ShareEnum获取平台ID
  140. $platformId = ShareEnum::getPlatformId($shareEnumPlatform);
  141. $fromPlatformId = ShareEnum::getPlatformId($shareEnumFrom);
  142. // 构造分享记录参数,模拟通过邀请海报访问
  143. $spmParams = sprintf('%s.%s.%s.%s.%s',
  144. $parentAgent->user_id, // shareId: 邀请人ID
  145. PageTypeEnum::AGENT_POSTER, // pageType: 分销海报页
  146. $parentAgent->user_id, // query: 代理商ID
  147. $platformId, // platform: 从请求头获取
  148. $fromPlatformId // from: 从请求头获取
  149. );
  150. // 添加分享记录,触发 user_share_after 钩子
  151. // 钩子会自动处理:1.绑定上级关系 2.添加推广日志 3.触发团队统计更新
  152. $shareParams = [
  153. 'spm' => $spmParams,
  154. 'shareId' => $parentAgent->user_id,
  155. 'page' => PageTypeEnum::AGENT_POSTER,
  156. 'query' => $parentAgent->user_id,
  157. 'platform' => $shareEnumPlatform,
  158. 'from' => ShareEnum::FROM_POSTER
  159. ];
  160. $shareResult = ShareService::addShareLog($user->id, $shareParams);
  161. $this->success('绑定上级成功', [
  162. 'parent_info' => [
  163. 'user_id' => $parentAgent->user_id,
  164. 'nickname' => $parentUser->nickname ?? '',
  165. 'avatar' => $parentUser->avatar ?? '',
  166. 'invite_code' => $inviteCode
  167. ],
  168. 'share_record' => $shareResult ? true : false
  169. ]);
  170. } catch (Exception $e) {
  171. $this->error($e->getMessage());
  172. }
  173. }
  174. /**
  175. * 将ChannelEnum映射到ShareEnum平台常量
  176. * @param string $channelPlatform
  177. * @return string
  178. */
  179. private function mapChannelToSharePlatform($channelPlatform)
  180. {
  181. $channelToShareMap = [
  182. 'H5' => ShareEnum::PLATFORM_H5,
  183. 'WechatOfficialAccount' => ShareEnum::PLATFORM_WECHAT_OFFICIAL_ACCOUNT,
  184. 'WechatMiniProgram' => ShareEnum::PLATFORM_WECHAT_MINI_PROGRAM,
  185. 'IosApp' => ShareEnum::PLATFORM_APP,
  186. 'AndroidApp' => ShareEnum::PLATFORM_APP,
  187. ];
  188. return $channelToShareMap[$channelPlatform] ?? ShareEnum::PLATFORM_WECHAT_MINI_PROGRAM;
  189. }
  190. /**
  191. * 检查目标用户是否在指定用户的下级链条中(防止循环绑定)
  192. * @param int $parentUserId 上级用户ID
  193. * @param int $targetUserId 要检查的目标用户ID
  194. * @param int $maxDepth 最大检查深度,防止无限递归
  195. * @param int $currentDepth 当前递归深度
  196. * @return bool true-目标用户在下级链条中,false-不在
  197. */
  198. private function isUserInDownline($parentUserId, $targetUserId, $maxDepth = 10, $currentDepth = 0)
  199. {
  200. // 防止无限递归
  201. if ($currentDepth >= $maxDepth) {
  202. return false;
  203. }
  204. // 查找所有以 $parentUserId 为上级的用户
  205. $childrenUsers = UserModel::where('parent_user_id', $parentUserId)->column('id');
  206. if (empty($childrenUsers)) {
  207. return false;
  208. }
  209. // 直接检查:目标用户是否是直接下级
  210. if (in_array($targetUserId, $childrenUsers)) {
  211. return true;
  212. }
  213. // 递归检查:目标用户是否在任何一个下级的下级链条中
  214. foreach ($childrenUsers as $childUserId) {
  215. if ($this->isUserInDownline($childUserId, $targetUserId, $maxDepth, $currentDepth + 1)) {
  216. return true;
  217. }
  218. }
  219. return false;
  220. }
  221. /**
  222. * 修改代理商申请资料
  223. */
  224. public function update()
  225. {
  226. try {
  227. $user = auth_user();
  228. $data = $this->request->param();
  229. // 检查用户是否已经是代理商
  230. $agent = AgentModel::where('user_id', $user->id)
  231. ->where('status', AgentModel::AGENT_STATUS_NORMAL)
  232. ->find();
  233. if (!$agent) {
  234. $this->error('您还不是代理商,无法修改资料');
  235. }
  236. // 获取用户的申请记录
  237. $apply = ApplyModel::where('user_id', $user->id)
  238. ->where('status', ApplyModel::STATUS_APPROVED)
  239. ->find();
  240. if (!$apply) {
  241. $this->error('未找到您的代理商申请记录');
  242. }
  243. // 先过滤出可更新的字段
  244. $validate = new AgentApplyValidate();
  245. $updateData = $validate->filterUpdatableFields($data, $apply->apply_type);
  246. if (empty($updateData)) {
  247. $this->error('没有有效的更新数据');
  248. }
  249. // 再验证过滤后的字段
  250. $validateResult = $validate->validateUpdateFields($updateData, $apply->apply_type);
  251. if ($validateResult !== true) {
  252. $this->error($validateResult);
  253. }
  254. // 记录修改前的数据(用于日志)
  255. $oldData = $apply->toArray();
  256. // 更新申请记录
  257. $updateData['updatetime'] = time();
  258. // 修改后需要重新审核
  259. $updateData['status'] = ApplyModel::STATUS_PENDING;
  260. $updateData['admin_id'] = null;
  261. $updateData['audit_time'] = null;
  262. $updateData['reject_reason'] = null;
  263. $apply->save($updateData);
  264. // 可以在这里添加操作日志记录
  265. $this->success('资料修改成功,请等待重新审核', [
  266. 'apply_type' => $apply->apply_type,
  267. 'apply_type_text' => $apply->apply_type_text,
  268. 'updated_fields' => array_keys($updateData),
  269. 'updated_data' => $updateData,
  270. 'status' => ApplyModel::STATUS_PENDING,
  271. 'status_text' => '待审核'
  272. ]);
  273. } catch (Exception $e) {
  274. $this->error($e->getMessage());
  275. }
  276. }
  277. }