123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- <?php
- 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
- {
- protected $noNeedLogin = ['identities', 'areas', 'checkAreaRequirement'];
- protected $noNeedRight = ['*'];
- /**
- * 获取代理商身份列表
- */
- public function identities()
- {
- try {
- $service = new AgentApplyService();
- $identities = $service->getIdentityList();
- $this->success('获取成功', $identities);
- } catch (Exception $e) {
- $this->error($e->getMessage());
- }
- }
- /**
- * 检查代理商身份是否需要地区信息
- */
- public function checkAreaRequirement()
- {
- try {
- $identityId = $this->request->param('agent_identity_id');
-
- if (empty($identityId)) {
- $this->error('代理商身份ID不能为空');
- }
-
- $requirement = AgentApplyValidate::getAreaRequirement($identityId);
- $this->success('获取成功', $requirement);
- } catch (Exception $e) {
- $this->error($e->getMessage());
- }
- }
- /**
- * 提交代理商申请
- */
- public function apply()
- {
- try {
- $user = auth_user();
- $data = $this->request->param();
-
- // 使用验证器验证参数
- $validate = new AgentApplyValidate();
- if (!$validate->scene('apply')->check($data)) {
- $this->error($validate->getError());
- }
- $service = new AgentApplyService();
- $apply = $service->submitApply($user->id, $data);
-
-
- if ($apply->status == ApplyModel::STATUS_APPROVED) {
- $this->success('申请提交成功,您已成为代理商!', $apply);
- } else {
- $this->success('申请提交成功,请等待审核!', $apply);
- }
- } catch (Exception $e) {
- $this->error($e->getMessage());
- }
- }
- /**
- * 获取申请状态
- */
- public function status()
- {
- try {
- $user = auth_user();
- $service = new AgentApplyService();
- $apply = $service->getUserApply($user->id);
-
- if (!$apply) {
- $this->success('未找到申请记录', null);
- }
-
- // 增强申请数据信息
- $data = $apply->toArray();
- $this->success('获取成功', $data);
- } catch (Exception $e) {
- $this->error($e->getMessage());
- }
- }
- /**
- * 绑定上级代理商(通过邀请码)
- */
- 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('邀请码无效或代理商状态异常');
- }
-
- // 获取上级用户信息
- $parentUser = UserModel::find($parentAgent->user_id);
- if (!$parentUser) {
- $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;
- }
- }
|