feat : phone pool

This commit is contained in:
2025-04-10 13:41:39 +08:00
parent 7b26a6cf6b
commit 37331f114e
5 changed files with 102 additions and 1 deletions

View File

@@ -10,7 +10,10 @@ use App\Constants\ConfigCode;
use App\Event\RegistrationEvent; use App\Event\RegistrationEvent;
use App\Exception\ErrException; use App\Exception\ErrException;
use App\Model\CouponTemplate; use App\Model\CouponTemplate;
use App\Model\PhonePool;
use App\Model\User;
use App\Model\UserCoupon; use App\Model\UserCoupon;
use App\Service\ServiceTrait\Api\PhonePoolTrait;
use Hyperf\Di\Annotation\Inject; use Hyperf\Di\Annotation\Inject;
use Hyperf\Event\Annotation\Listener; use Hyperf\Event\Annotation\Listener;
use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerExceptionInterface;
@@ -18,9 +21,12 @@ use Psr\Container\ContainerInterface;
use Hyperf\Event\Contract\ListenerInterface; use Hyperf\Event\Contract\ListenerInterface;
use Psr\Container\NotFoundExceptionInterface; use Psr\Container\NotFoundExceptionInterface;
#[Listener] #[Listener]
class FirstRegistrationListener implements ListenerInterface class FirstRegistrationListener implements ListenerInterface
{ {
use PhonePoolTrait;
/** /**
* @var ConfigCache * @var ConfigCache
*/ */
@@ -33,6 +39,18 @@ class FirstRegistrationListener implements ListenerInterface
#[Inject] #[Inject]
protected CouponTemplate $couponTemplateModel; protected CouponTemplate $couponTemplateModel;
/**
* @var User
*/
#[Inject]
protected User $userModel;
/**
* @var PhonePool
*/
#[Inject]
protected PhonePool $phonePoolModel;
/** /**
* @param ContainerInterface $container * @param ContainerInterface $container
*/ */
@@ -59,7 +77,14 @@ class FirstRegistrationListener implements ListenerInterface
{ {
$userId = $event->userId; $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); $couponTemplateId = $this->configCache->getConfigValueByKey(ConfigCode::COUPONS_FOR_NEWCOMERS);
$couponValidity = $this->configCache->getConfigValueByKey(ConfigCode::NEWBIE_COUPON_VALIDITY); $couponValidity = $this->configCache->getConfigValueByKey(ConfigCode::NEWBIE_COUPON_VALIDITY);

34
app/Model/PhonePool.php Normal file
View File

@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace App\Model;
use Hyperf\DbConnection\Model\Model;
/**
* @property int $id
* @property string $phone
* @property string $create_time
*/
class PhonePool extends Model
{
/**
* The table associated with the model.
*/
protected ?string $table = 'phone_pool';
/**
* The attributes that are mass assignable.
*/
protected array $fillable = [];
protected array $guarded = [];
/**
* The attributes that should be cast to native types.
*/
protected array $casts = ['id' => 'integer'];
const string CREATED_AT = 'create_time';
const null UPDATED_AT = null;
}

View File

@@ -203,6 +203,7 @@ abstract class LoginBaseService extends BaseService
$model->nickname = '用户'.StringUtil::randStr(6); $model->nickname = '用户'.StringUtil::randStr(6);
$model->avatar_id = 0; $model->avatar_id = 0;
$model->reg_ip = SystemUtil::getClientIp(); $model->reg_ip = SystemUtil::getClientIp();
$model->mobile = $this->mobile;
if (!$model->save()) throw new ErrException('数据保存失败-注册失败'); if (!$model->save()) throw new ErrException('数据保存失败-注册失败');

View File

@@ -15,6 +15,7 @@ use App\Event\RegistrationEvent;
use App\Exception\ErrException; use App\Exception\ErrException;
use App\Model\UserAccount; use App\Model\UserAccount;
use App\Model\UserThird; use App\Model\UserThird;
use App\Service\ServiceTrait\Api\PhonePoolTrait;
use App\Service\ServiceTrait\Api\WxMiniTrait; use App\Service\ServiceTrait\Api\WxMiniTrait;
use Hyperf\DbConnection\Db; use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject; use Hyperf\Di\Annotation\Inject;
@@ -26,6 +27,7 @@ use RedisException;
class WxFastLoginService extends LoginBaseService class WxFastLoginService extends LoginBaseService
{ {
use WxMiniTrait; use WxMiniTrait;
use PhonePoolTrait;
/** /**
* @var EventDispatcherInterface * @var EventDispatcherInterface
@@ -56,6 +58,7 @@ class WxFastLoginService extends LoginBaseService
$wxPhone = $this->jsCodeGetPhoneNumber($phoneCode); $wxPhone = $this->jsCodeGetPhoneNumber($phoneCode);
$this->mobile = $wxPhone['phone_info']['purePhoneNumber'] ?? ''; $this->mobile = $wxPhone['phone_info']['purePhoneNumber'] ?? '';
if ($this->mobile == '') throw new ErrException('手机号获取失败');
$this->checkLock(self::LOGIN_TYPE); $this->checkLock(self::LOGIN_TYPE);
@@ -72,6 +75,8 @@ class WxFastLoginService extends LoginBaseService
/** /**
* @return void * @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/ */
private function updateMobile(): void private function updateMobile(): void
{ {
@@ -82,6 +87,8 @@ class WxFastLoginService extends LoginBaseService
$this->userInfo->mobile = $this->mobile; $this->userInfo->mobile = $this->mobile;
if (!$this->userInfo->save()) throw new ErrException('更新手机号失败'); if (!$this->userInfo->save()) throw new ErrException('更新手机号失败');
$this->addMobilePhone($this->mobile);
} }
/** /**

View File

@@ -0,0 +1,34 @@
<?php
namespace App\Service\ServiceTrait\Api;
use App\Lib\Log;
use App\Model\PhonePool;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
trait PhonePoolTrait
{
/**
* @var Log
*/
#[Inject]
protected Log $log;
/**
* @param string $mobile
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function addMobilePhone(string $mobile): void
{
if (empty($mobile)) return;
$insertModel = new PhonePool();
$insertModel->phone = $mobile;
if (!$insertModel->save()) $this->log->error('添加手机号到池子失败,mobile:'.$mobile);
}
}