From f26160d48db79d03f5affd7afd43bb92a4792f01 Mon Sep 17 00:00:00 2001 From: ctexthuang Date: Mon, 31 Mar 2025 16:55:21 +0800 Subject: [PATCH] fix : coupon --- app/Cache/Redis/Api/ApiRedisKey.php | 9 +++++++++ app/Service/Api/Order/RefundOrderService.php | 19 ++++++++++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/app/Cache/Redis/Api/ApiRedisKey.php b/app/Cache/Redis/Api/ApiRedisKey.php index c65ec74..6f17782 100644 --- a/app/Cache/Redis/Api/ApiRedisKey.php +++ b/app/Cache/Redis/Api/ApiRedisKey.php @@ -64,4 +64,13 @@ class ApiRedisKey { return 'lock:pay:user_id:'. $userId; } + + /** + * @param int $userId + * @return string + */ + public static function refundLock(int $userId) + { + return 'lock:refund:user_id:'. $userId; + } } \ No newline at end of file diff --git a/app/Service/Api/Order/RefundOrderService.php b/app/Service/Api/Order/RefundOrderService.php index e7e7e04..b826df5 100644 --- a/app/Service/Api/Order/RefundOrderService.php +++ b/app/Service/Api/Order/RefundOrderService.php @@ -10,6 +10,8 @@ declare(strict_types=1); namespace App\Service\Api\Order; +use App\Cache\Redis\Api\ApiRedisKey; +use App\Cache\Redis\RedisCache; use App\Constants\Common\OrderCode; use App\Constants\Common\RefundCode; use App\Exception\ErrException; @@ -39,6 +41,12 @@ class RefundOrderService extends BaseService #[Inject] protected PayOrder $payOrderModel; + /** + * @var RedisCache + */ + #[Inject] + protected RedisCache $redis; + /** * @return array * @throws ContainerExceptionInterface @@ -46,8 +54,12 @@ class RefundOrderService extends BaseService */ public function handle(): array { - //todo 考虑是否枷锁 + // 考虑是否枷锁 $orderId = (int)$this->request->input('order_id'); + $refundLockKey = ApiRedisKey::refundLock($this->userId); + if (0 == ($this->redis->addLock($refundLockKey))){ + throw new ErrException('请勿重复点击'); + } $orderInfo = $this->orderModel->getInfoById($orderId); @@ -64,10 +76,11 @@ class RefundOrderService extends BaseService $service->type = RefundCode::FULL_GOOD_REFUND; $service->handle(); - - return $this->return->success(); }catch (Exception $e) { + throw new ErrException($e->getMessage()); } + $this->redis->delLock($refundLockKey); + return $this->return->success(); } } \ No newline at end of file