|
@@ -4,7 +4,13 @@ namespace app\api\controller\commission;
|
|
|
|
|
|
use app\common\Service\Commission\AgentApply as AgentApplyService;
|
|
use app\common\Service\Commission\AgentApply as AgentApplyService;
|
|
use app\common\model\commission\Apply as ApplyModel;
|
|
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\api\validate\AgentApply as AgentApplyValidate;
|
|
|
|
+use app\common\Enum\ShareEnum;
|
|
|
|
+use app\common\Enum\PageTypeEnum;
|
|
|
|
+use app\common\Enum\ChannelEnum;
|
|
use think\Exception;
|
|
use think\Exception;
|
|
use app\api\controller\Base;
|
|
use app\api\controller\Base;
|
|
class AgentApply extends 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;
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|