feat: config

This commit is contained in:
2025-03-23 20:32:15 +08:00
parent be26c85d82
commit f98b9f1ff7

View File

@@ -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');
}
}