From f98b9f1ff7635051f9bf5f7fdf7ea5537a36a121 Mon Sep 17 00:00:00 2001 From: ctexthuang Date: Sun, 23 Mar 2025 20:32:15 +0800 Subject: [PATCH] feat: config --- app/Listener/FirstRegistrationListener.php | 68 +++++++++++++++++++++- 1 file changed, 65 insertions(+), 3 deletions(-) diff --git a/app/Listener/FirstRegistrationListener.php b/app/Listener/FirstRegistrationListener.php index b7fc154..ab44198 100644 --- a/app/Listener/FirstRegistrationListener.php +++ b/app/Listener/FirstRegistrationListener.php @@ -4,18 +4,43 @@ declare(strict_types=1); namespace App\Listener; +use App\Cache\Redis\Common\ConfigCache; +use App\Constants\Common\CouponCode; +use App\Constants\ConfigCode; use App\Event\RegistrationEvent; +use App\Exception\ErrException; +use App\Model\CouponTemplate; +use App\Model\UserCoupon; +use Hyperf\Di\Annotation\Inject; use Hyperf\Event\Annotation\Listener; +use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; use Hyperf\Event\Contract\ListenerInterface; +use Psr\Container\NotFoundExceptionInterface; #[Listener] class FirstRegistrationListener implements ListenerInterface { - public function __construct(protected ContainerInterface $container) - { - } + /** + * @var ConfigCache + */ + #[Inject] + protected ConfigCache $configCache; + /** + * @var CouponTemplate + */ + #[Inject] + protected CouponTemplate $couponTemplateModel; + + /** + * @param ContainerInterface $container + */ + public function __construct(protected ContainerInterface $container) {} + + /** + * @return class-string[] + */ public function listen(): array { return [ @@ -24,7 +49,44 @@ class FirstRegistrationListener implements ListenerInterface ]; } + /** + * @param object $event + * @return void + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ public function process(object $event): void { + $userId = $event->userId; + + //todo 判断是不是新人 + + $couponTemplateId = $this->configCache->getConfigValueByKey(ConfigCode::COUPONS_FOR_NEWCOMERS); + $couponValidity = $this->configCache->getConfigValueByKey(ConfigCode::NEWBIE_COUPON_VALIDITY); + + // 随便一个为0代表不赠送 + if ($couponValidity == 0 || $couponTemplateId == 0) return; + + $couponTemplateId = explode(',', $couponTemplateId); + $couponTemplateList = $this->couponTemplateModel->getDataByIds($couponTemplateId); + + $insertCoupon = []; + foreach ($couponTemplateId as $one){ + if ($couponTemplateList[$one]['status'] == CouponCode::COUPON_TEMPLATE_STATUS_ENABLE) continue; + + $insertCoupon[] = [ + 'coupon_template_id' => $one, + 'coupon_dispense_id' => CouponCode::SYSTEMIC_DISTRIBUTION, + 'user_id' => $userId, + 'status' => CouponCode::COUPON_STATUS_UNUSED, + 'coupon_name' => $couponTemplateList[$one]['name'], + 'validity_start_time' => date('Y-m-d H:i:s'), + 'validity_end_time' => date('Y-m-d', strtotime('+'.$couponValidity.' days')), + ]; + } + + if (empty($insertCoupon)) return; + + if (!(new UserCoupon)->insert($insertCoupon)) throw new ErrException('注册失败-00003'); } }