Browse Source

fix: 不报错

super-yimizi 1 month ago
parent
commit
f3fc1d23d8

+ 17 - 2
application/api/controller/Lottery.php

@@ -411,12 +411,27 @@ class Lottery extends Api
             'total_amount' => $orderInfo->amount,
             'goods' => $goodsList            
         ];
+        // 获取当前正在进行的单个抽奖活动
+        $activity = LotteryActivityService::getCurrentRunningActivity();
+            
+        // 如果没有正在进行的活动,返回null
+        if (!$activity) {
+            $this->success('获取成功', null);
+        }
         //  用户 订单 是否已经分发过 抽奖机会
         $isGranted = LotteryChanceService::isGrantedChanceForOrder($orderInfo, $userId);
         if($isGranted){
-            $this->error('该订单已分发过抽奖机会');
+            //  直接返回 所剩 机会
+            $userChance = LotteryChanceService::getUserChance($activity->id, $userId);
+            $arrReturn = [
+                'lottery_id' => $activity->id,
+                'lottery_type' => $activity->lottery_type,
+                'lottery_name' => $activity->name,
+                'chances' => $userChance->total_chances,
+            ];
+            $this->success('获取成功', $arrReturn);
         }
-        $grantedChances = LotteryChanceService::checkAndGrantChanceForOrderOne($orderInfo, $userId);
+        $grantedChances = LotteryChanceService::checkAndGrantChanceForOrderOne($activity, $orderInfo, $userId);
         $this->success('获取成功', $grantedChances);
     }
 }

+ 3 - 9
application/common/Service/Lottery/LotteryChanceService.php

@@ -38,26 +38,20 @@ class LotteryChanceService
         /**
      * 订单完成后检查并分发抽奖机会(单个活动版本)
      * 查询一个正在进行的活动,返回单个结果
-     * 
+     * @param LotteryActivity $activity 活动对象
      * @param array $orderInfo 订单信息 ['id', 'total_amount', 'goods' => [['goods_id']]]
      * @param int $userId 用户ID
      * @return array|null 获得的抽奖机会信息,如果没有则返回null
      * @throws Exception
      */
-    public static function checkAndGrantChanceForOrderOne($orderInfo, $userId)
+    public static function checkAndGrantChanceForOrderOne($activity, $orderInfo, $userId)
     {
         if (empty($orderInfo) || empty($userId)) {
             throw new BusinessException('订单信息或用户ID不能为空');
         }
 
         // try {
-            // 获取当前正在进行的单个抽奖活动
-            $activity = LotteryActivityService::getCurrentRunningActivity();
-            
-            // 如果没有正在进行的活动,返回null
-            if (!$activity) {
-                return null;
-            }
+      
             
             // 处理当前活动
             $chances = static::processActivityForOrder($activity, $orderInfo, $userId);

+ 3 - 9
application/common/Service/lottery/LotteryChanceService.php

@@ -38,26 +38,20 @@ class LotteryChanceService
         /**
      * 订单完成后检查并分发抽奖机会(单个活动版本)
      * 查询一个正在进行的活动,返回单个结果
-     * 
+     * @param LotteryActivity $activity 活动对象
      * @param array $orderInfo 订单信息 ['id', 'total_amount', 'goods' => [['goods_id']]]
      * @param int $userId 用户ID
      * @return array|null 获得的抽奖机会信息,如果没有则返回null
      * @throws Exception
      */
-    public static function checkAndGrantChanceForOrderOne($orderInfo, $userId)
+    public static function checkAndGrantChanceForOrderOne($activity, $orderInfo, $userId)
     {
         if (empty($orderInfo) || empty($userId)) {
             throw new BusinessException('订单信息或用户ID不能为空');
         }
 
         // try {
-            // 获取当前正在进行的单个抽奖活动
-            $activity = LotteryActivityService::getCurrentRunningActivity();
-            
-            // 如果没有正在进行的活动,返回null
-            if (!$activity) {
-                return null;
-            }
+      
             
             // 处理当前活动
             $chances = static::processActivityForOrder($activity, $orderInfo, $userId);