From 37331f114e846bbeac15fd69b3d21494090b5f77 Mon Sep 17 00:00:00 2001 From: ctexthuang Date: Thu, 10 Apr 2025 13:41:39 +0800 Subject: [PATCH] feat : phone pool --- app/Listener/FirstRegistrationListener.php | 27 ++++++++++++++- app/Model/PhonePool.php | 34 +++++++++++++++++++ app/Service/Api/Login/LoginBaseService.php | 1 + app/Service/Api/Login/WxFastLoginService.php | 7 ++++ .../ServiceTrait/Api/PhonePoolTrait.php | 34 +++++++++++++++++++ 5 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 app/Model/PhonePool.php create mode 100644 app/Service/ServiceTrait/Api/PhonePoolTrait.php diff --git a/app/Listener/FirstRegistrationListener.php b/app/Listener/FirstRegistrationListener.php index ab44198..116391a 100644 --- a/app/Listener/FirstRegistrationListener.php +++ b/app/Listener/FirstRegistrationListener.php @@ -10,7 +10,10 @@ use App\Constants\ConfigCode; use App\Event\RegistrationEvent; use App\Exception\ErrException; use App\Model\CouponTemplate; +use App\Model\PhonePool; +use App\Model\User; use App\Model\UserCoupon; +use App\Service\ServiceTrait\Api\PhonePoolTrait; use Hyperf\Di\Annotation\Inject; use Hyperf\Event\Annotation\Listener; use Psr\Container\ContainerExceptionInterface; @@ -18,9 +21,12 @@ use Psr\Container\ContainerInterface; use Hyperf\Event\Contract\ListenerInterface; use Psr\Container\NotFoundExceptionInterface; + #[Listener] class FirstRegistrationListener implements ListenerInterface { + use PhonePoolTrait; + /** * @var ConfigCache */ @@ -33,6 +39,18 @@ class FirstRegistrationListener implements ListenerInterface #[Inject] protected CouponTemplate $couponTemplateModel; + /** + * @var User + */ + #[Inject] + protected User $userModel; + + /** + * @var PhonePool + */ + #[Inject] + protected PhonePool $phonePoolModel; + /** * @param ContainerInterface $container */ @@ -59,7 +77,14 @@ class FirstRegistrationListener implements ListenerInterface { $userId = $event->userId; - //todo 判断是不是新人 + //todo 此处能做性能优化 异步执行 (异步执行要考虑事务) + $mobile = $this->userModel->where('user_id', $userId)->value('mobile') ?? ''; + if ($mobile == '') return; + + $poolFlag = $this->phonePoolModel->where('phone', $mobile)->first(); + if ($poolFlag) return; + + $this->addMobilePhone($mobile); $couponTemplateId = $this->configCache->getConfigValueByKey(ConfigCode::COUPONS_FOR_NEWCOMERS); $couponValidity = $this->configCache->getConfigValueByKey(ConfigCode::NEWBIE_COUPON_VALIDITY); diff --git a/app/Model/PhonePool.php b/app/Model/PhonePool.php new file mode 100644 index 0000000..7e05ea9 --- /dev/null +++ b/app/Model/PhonePool.php @@ -0,0 +1,34 @@ + 'integer']; + + const string CREATED_AT = 'create_time'; + const null UPDATED_AT = null; +} diff --git a/app/Service/Api/Login/LoginBaseService.php b/app/Service/Api/Login/LoginBaseService.php index f5dcdf5..db54e50 100644 --- a/app/Service/Api/Login/LoginBaseService.php +++ b/app/Service/Api/Login/LoginBaseService.php @@ -203,6 +203,7 @@ abstract class LoginBaseService extends BaseService $model->nickname = '用户'.StringUtil::randStr(6); $model->avatar_id = 0; $model->reg_ip = SystemUtil::getClientIp(); + $model->mobile = $this->mobile; if (!$model->save()) throw new ErrException('数据保存失败-注册失败'); diff --git a/app/Service/Api/Login/WxFastLoginService.php b/app/Service/Api/Login/WxFastLoginService.php index 01533ef..e132de5 100644 --- a/app/Service/Api/Login/WxFastLoginService.php +++ b/app/Service/Api/Login/WxFastLoginService.php @@ -15,6 +15,7 @@ use App\Event\RegistrationEvent; use App\Exception\ErrException; use App\Model\UserAccount; use App\Model\UserThird; +use App\Service\ServiceTrait\Api\PhonePoolTrait; use App\Service\ServiceTrait\Api\WxMiniTrait; use Hyperf\DbConnection\Db; use Hyperf\Di\Annotation\Inject; @@ -26,6 +27,7 @@ use RedisException; class WxFastLoginService extends LoginBaseService { use WxMiniTrait; + use PhonePoolTrait; /** * @var EventDispatcherInterface @@ -56,6 +58,7 @@ class WxFastLoginService extends LoginBaseService $wxPhone = $this->jsCodeGetPhoneNumber($phoneCode); $this->mobile = $wxPhone['phone_info']['purePhoneNumber'] ?? ''; + if ($this->mobile == '') throw new ErrException('手机号获取失败'); $this->checkLock(self::LOGIN_TYPE); @@ -72,6 +75,8 @@ class WxFastLoginService extends LoginBaseService /** * @return void + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface */ private function updateMobile(): void { @@ -82,6 +87,8 @@ class WxFastLoginService extends LoginBaseService $this->userInfo->mobile = $this->mobile; if (!$this->userInfo->save()) throw new ErrException('更新手机号失败'); + + $this->addMobilePhone($this->mobile); } /** diff --git a/app/Service/ServiceTrait/Api/PhonePoolTrait.php b/app/Service/ServiceTrait/Api/PhonePoolTrait.php new file mode 100644 index 0000000..ad75326 --- /dev/null +++ b/app/Service/ServiceTrait/Api/PhonePoolTrait.php @@ -0,0 +1,34 @@ +phone = $mobile; + if (!$insertModel->save()) $this->log->error('添加手机号到池子失败,mobile:'.$mobile); + } +} \ No newline at end of file