ReferralController.php 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace App\Http\Controllers\agent;
  3. use App\Http\Controllers\AgentApiController;
  4. use App\Models\Agent\Agent;
  5. use App\Models\User;
  6. use App\Services\Agent\AgentDomainService;
  7. use App\Services\MemberSuperiorService;
  8. use App\Services\Sharing\ShareRelations;
  9. use Carbon\Carbon;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Facades\DB;
  12. class ReferralController extends AgentApiController
  13. {
  14. public function resolve(Request $request, AgentDomainService $domains)
  15. {
  16. return $this->execute(function () use ($request, $domains) {
  17. $data = $request->validate([
  18. 'agent_code' => ['nullable', 'string', 'max:32', 'required_without:domain'],
  19. 'domain' => ['nullable', 'string', 'max:255', 'required_without:agent_code'],
  20. ]);
  21. $agent = $domains->resolve($data['agent_code'] ?? null, $data['domain'] ?? null);
  22. if (!$agent?->isJoint()) throw new \RuntimeException('代理推广信息不存在或已禁用');
  23. return [
  24. 'agent_id' => (int) $agent->id,
  25. 'agent_code' => $agent->invitation_code,
  26. 'agent_name' => $agent->username,
  27. ];
  28. });
  29. }
  30. public function bind(Request $request, AgentDomainService $domains)
  31. {
  32. return $this->execute(function () use ($request, $domains) {
  33. $data = $request->validate([
  34. 'agent_code' => ['nullable', 'string', 'max:32', 'required_without:domain'],
  35. 'domain' => ['nullable', 'string', 'max:255', 'required_without:agent_code'],
  36. ]);
  37. $agent = $domains->resolve($data['agent_code'] ?? null, $data['domain'] ?? null);
  38. if (!$agent) {
  39. throw new \RuntimeException('代理推广信息不存在或已禁用');
  40. }
  41. // 入股代理的历史邀请码和域名可以传入,但不会改变会员归属。
  42. if (!$agent->isJoint()) {
  43. return ['user_id' => (int) $request->user->id, 'agent_id' => null];
  44. }
  45. return DB::transaction(function () use ($request, $agent) {
  46. ShareRelations::lock();
  47. $user = User::query()->lockForUpdate()->findOrFail($request->user->id);
  48. $agent = Agent::query()->whereKey($agent->id)->where('status', Agent::STATUS_ENABLED)->lockForUpdate()->first();
  49. if (!$agent) throw new \RuntimeException('代理推广信息不存在或已禁用');
  50. if (!$agent->isJoint() || MemberSuperiorService::hasMemberSuperior($user)) {
  51. return ['user_id' => (int) $user->id, 'agent_id' => null];
  52. }
  53. if (MemberSuperiorService::hasJointAgent($user)) {
  54. if ((int) $user->agent_id !== (int) $agent->id) {
  55. throw new \RuntimeException('会员已绑定其他代理,不能重复修改');
  56. }
  57. return ['user_id' => (int) $user->id, 'agent_id' => (int) $agent->id];
  58. }
  59. $createdAt = $user->getRawOriginal('created_at');
  60. if (!$createdAt || Carbon::parse($createdAt)->lt(now()->subMinutes(config('agent.referral_bind_minutes', 30)))) {
  61. throw new \RuntimeException('推广归属只能在新会员注册后及时绑定');
  62. }
  63. $user->agent_id = $agent->id;
  64. $user->agent_user_code = null;
  65. $user->save();
  66. return ['user_id' => (int) $user->id, 'agent_id' => (int) $agent->id];
  67. });
  68. });
  69. }
  70. }