Преглед изворни кода

修改添加礼品卡接口

llp пре 5 дана
родитељ
комит
c848d412eb

+ 16 - 3
packages/Longyi/Gift/src/Http/Controllers/Shop/GiftController.php

@@ -38,11 +38,24 @@ class GiftController extends Controller
     public function add($request): JsonResponse
     {
         $id = $request->input('id');
+        if (!$id) {
+            return response()->json([
+                'success' => false,
+                'message' => 'Gift card not found',
+            ]);
+        }
         $customerId = auth()->user()->id;
-        $result = $this->giftCardsModel->add($id, $customerId);
+        try {
+            $this->giftCardsModel->add($id, $customerId);
+        } catch (\Exception $e) {
+            return response()->json([
+                'success' => false,
+                'message' => $e->getMessage(),
+            ]);
+        }
         return response()->json([
-            'success' => $result ===  true ?: false,
-            'message' => $result === true ? 'Gift card added successfully' : $result,
+            'success' => true,
+            'message' => 'Gift card added successfully',
         ]);
     }
     /**

+ 31 - 6
packages/Longyi/Gift/src/Models/GiftCards.php

@@ -9,6 +9,7 @@ use Illuminate\Database\Eloquent\Model;
 use Longyi\Gift\Contracts\GiftCards as GiftCardsContract;
 use Longyi\RewardPoints\Config\TransactionType;
 use Longyi\RewardPoints\Repositories\RewardPointRepository;
+use function Symfony\Component\Translation\t;
 
 class GiftCards extends Model implements GiftCardsContract
 {
@@ -212,28 +213,52 @@ class GiftCards extends Model implements GiftCardsContract
         ];
     }
 
+    /**
+     * Add a gift card for a customer using points
+     *
+     * @param int $id Gift card ID from giftcards() array
+     * @param int $customerId Customer ID
+     * @return array|true
+     */
     public function add($id, $customerId)
     {
+        if (empty($customerId)) {
+            throw new \Exception('customer id is empty');
+        }
+        if (empty($id)) {
+            throw new \Exception('gift card id is empty');
+        }
         $gifts = $this->giftcards();
         $giftInfo = $gifts[$id] ?? null;
         if (!$giftInfo) {
-            return false;
+            throw new \Exception("gift card not found (ID: {$id})");
+        }
+        $points = $giftInfo['points'] ?? 0;
+        if ($points <= 0) {
+            throw new \Exception("gift card points configuration error (points: {$points})");
         }
         try {
             DB::beginTransaction();
-            self::addGiftCard($customerId, $giftInfo['amount'], $giftInfo['num'], $giftInfo['expirationdate'], $giftInfo['channel'], $giftInfo['notes']);
-            $rewardPointRepository = app(RewardPointRepository::class);//积分兑换
+            self::addGiftCard(
+                $customerId,
+                $giftInfo['amount'],
+                $giftInfo['num'],
+                $giftInfo['expirationdate'],
+                $giftInfo['channel'],
+                $giftInfo['notes']
+            );
+            $rewardPointRepository = app(RewardPointRepository::class);
             $rewardPointRepository->deductPoints(
                 $customerId,
+                $points,
                 TransactionType::GIFT_CARD_REDEEM,
-                $giftInfo['points'],
                 '积分兑换礼品卡'
             );
             DB::commit();
+            return true;
         } catch (\Exception $e) {
             DB::rollBack();
-            return $e->getMessage();
+            throw $e;
         }
-        return true;
     }
 }