Browse Source

fix:邀请记录

super-yimizi 11 hours ago
parent
commit
cb96091614

+ 116 - 0
application/api/controller/commission/AgentApply.php

@@ -4,7 +4,13 @@ namespace app\api\controller\commission;
 
 use app\common\Service\Commission\AgentApply as AgentApplyService;
 use app\common\model\commission\Apply as ApplyModel;
+use app\common\model\commission\Agent as AgentModel;
+use app\common\model\User as UserModel;
+use app\common\Service\Share\ShareService;
 use app\api\validate\AgentApply as AgentApplyValidate;
+use app\common\Enum\ShareEnum;
+use app\common\Enum\PageTypeEnum;
+use app\common\Enum\ChannelEnum;
 use think\Exception;
 use app\api\controller\Base;
 class AgentApply extends Base
@@ -95,4 +101,114 @@ class AgentApply extends Base
         }
     }
 
+    /**
+     * 绑定上级代理商(通过邀请码)
+     */
+    public function bindParent()
+    {
+        try {
+            $user = auth_user();
+            $data = $this->request->param();
+            
+            // 使用验证器验证参数
+            $validate = new AgentApplyValidate();
+            if (!$validate->scene('bindParent')->check($data)) {
+                $this->error($validate->getError());
+            }
+            
+            $inviteCode = $data['invite_code'];
+            
+            // 查询邀请码对应的代理商
+            $parentAgent = AgentModel::where('invite_code', $inviteCode)
+                                    ->where('status', AgentModel::AGENT_STATUS_NORMAL)
+                                    ->find();
+            
+            if (!$parentAgent) {
+                $this->error('邀请码无效或代理商状态异常');
+            }
+            
+            // 不能绑定自己
+            if ($parentAgent->user_id == $user->id) {
+                $this->error('不能绑定自己为上级');
+            }
+            
+            // 检查是否已经有上级了
+            if (!empty($user->parent_user_id)) {
+                $this->error('您已经有上级了,无法重复绑定');
+            }            
+            // 更新用户的上级关系
+            $user->parent_user_id = $parentAgent->user_id;
+            $user->save();
+            
+            // 从请求头获取platform参数(与生成二维码接口保持一致)
+            $requestPlatform = $this->request->header('platform', '');
+            if (!$requestPlatform) {
+                $requestPlatform = 'WechatMiniProgram'; // 默认微信小程序
+            }
+            
+            // 从请求头获取from参数(来源平台)
+            $requestFrom = $this->request->header('from', $requestPlatform);
+            
+            // 将ChannelEnum映射到ShareEnum平台常量
+            $shareEnumPlatform = $this->mapChannelToSharePlatform($requestPlatform);
+            $shareEnumFrom = $this->mapChannelToSharePlatform($requestFrom);
+            
+            // 使用ShareEnum获取平台ID
+            $platformId = ShareEnum::getPlatformId($shareEnumPlatform);
+            $fromPlatformId = ShareEnum::getPlatformId($shareEnumFrom);
+            
+            // 构造分享记录参数,模拟通过邀请海报访问
+            $spmParams = sprintf('%s.%s.%s.%s.%s',
+                $parentAgent->user_id,  // shareId: 邀请人ID
+                PageTypeEnum::AGENT_POSTER,  // pageType: 分销海报页
+                $parentAgent->user_id,  // query: 代理商ID
+                $platformId,  // platform: 从请求头获取
+                $fromPlatformId  // from: 从请求头获取
+            );
+            
+            // 添加分享记录
+            $shareParams = [
+                'spm' => $spmParams,
+                'shareId' => $parentAgent->user_id,
+                'page' => PageTypeEnum::AGENT_POSTER,
+                'query' => $parentAgent->user_id,
+                'platform' => $shareEnumPlatform,
+                'from' => ShareEnum::FROM_POSTER
+            ];
+            
+            $shareResult = ShareService::addShareLog($user->id, $shareParams);
+            
+            $this->success('绑定上级成功', [
+                'parent_info' => [
+                    'user_id' => $parentAgent->user_id,
+                    'nickname' => $parentUser->nickname ?? '',
+                    'avatar' => $parentUser->avatar ?? '',
+                    'invite_code' => $inviteCode
+                ],
+                'share_record' => $shareResult ? true : false
+            ]);
+            
+        } catch (Exception $e) {
+            $this->error($e->getMessage());
+        }
+    }
+    
+    /**
+     * 将ChannelEnum映射到ShareEnum平台常量
+     * @param string $channelPlatform
+     * @return string
+     */
+    private function mapChannelToSharePlatform($channelPlatform)
+    {
+        $channelToShareMap = [
+            'H5' => ShareEnum::PLATFORM_H5,
+            'WechatOfficialAccount' => ShareEnum::PLATFORM_WECHAT_OFFICIAL_ACCOUNT,
+            'WechatMiniProgram' => ShareEnum::PLATFORM_WECHAT_MINI_PROGRAM,
+            'IosApp' => ShareEnum::PLATFORM_APP,
+            'AndroidApp' => ShareEnum::PLATFORM_APP,
+        ];
+
+        return $channelToShareMap[$channelPlatform] ?? ShareEnum::PLATFORM_WECHAT_MINI_PROGRAM;
+    }
+
 }

+ 34 - 1
application/api/validate/AgentApply.php

@@ -5,6 +5,7 @@ namespace app\api\validate;
 use think\Validate;
 use app\common\model\commission\Apply as ApplyModel;
 use app\common\model\commission\Identity as IdentityModel;
+use app\common\model\commission\Agent as AgentModel;
 use app\common\Enum\AgentType;
 
 class AgentApply extends Validate
@@ -37,6 +38,9 @@ class AgentApply extends Validate
         'legal_id_front'    => 'max:255',
         'legal_id_back'     => 'max:255',
         'business_license'  => 'max:255',
+        
+        // 绑定上级字段
+        'invite_code'       => 'require|max:50|checkInviteCodeExists',
     ];
 
     /**
@@ -73,13 +77,18 @@ class AgentApply extends Validate
         'legal_id_front.max'        => '法人身份证正面图片路径过长',
         'legal_id_back.max'         => '法人身份证反面图片路径过长',
         'business_license.max'      => '营业执照图片路径过长',
+        
+        // 绑定上级消息
+        'invite_code.require'       => '邀请码不能为空',
+        'invite_code.max'           => '邀请码最多50个字符',
     ];
 
     /**
      * 验证场景
      */
     protected $scene = [
-        'apply' => ['apply_type', 'agent_identity_id']
+        'apply' => ['apply_type', 'agent_identity_id'],
+        'bindParent' => ['invite_code']
     ];
 
     /**
@@ -226,6 +235,30 @@ class AgentApply extends Validate
     }
 
     /**
+     * 检查邀请码是否存在且有效
+     * @param mixed $value
+     * @param mixed $rule
+     * @param array $data
+     * @return bool|string
+     */
+    protected function checkInviteCodeExists($value, $rule, $data)
+    {
+        if (empty($value)) {
+            return '邀请码不能为空';
+        }
+        
+        $agent = AgentModel::where('invite_code', $value)
+                          ->where('status', AgentModel::AGENT_STATUS_NORMAL)
+                          ->find();
+        
+        if (!$agent) {
+            return '邀请码无效或代理商状态异常';
+        }
+        
+        return true;
+    }
+
+    /**
      * 获取代理商身份是否需要地区信息
      * @param int $identityId
      * @return array