فهرست منبع

修改礼品卡使用逻辑

llp 5 روز پیش
والد
کامیت
7f8dd3e113

+ 6 - 5
packages/Longyi/Gift/src/DataGrids/GiftCards/GiftCardsDataGrid.php

@@ -4,6 +4,7 @@ namespace Longyi\Gift\DataGrids\GiftCards;
 
 use Illuminate\Support\Facades\DB;
 use Webkul\DataGrid\DataGrid;
+use Longyi\Gift\Models\GiftCards;
 use Longyi\Gift\Models\GiftCardUsageLog;
 
 class GiftCardsDataGrid extends DataGrid
@@ -146,11 +147,11 @@ class GiftCardsDataGrid extends DataGrid
             'filterable' => true,
             'sortable'   => true,
             'closure'    => function ($row) {
-                if ($row->giftcard_status == 1) {
-                    return '<span class="badge badge-md badge-success">未使用</span>';
-                } else {
-                    return '<span class="badge badge-md badge-warning">已使用</span>';
-                }
+                return match ((int) $row->giftcard_status) {
+                    GiftCards::STATUS_USED => '<span class="badge badge-md badge-warning">'.trans('gift::app.admin.datagrid.used').'</span>',
+                    GiftCards::STATUS_EXPIRED => '<span class="badge badge-md badge-danger">'.trans('gift::app.admin.datagrid.expired').'</span>',
+                    default => '<span class="badge badge-md badge-success">'.trans('gift::app.admin.datagrid.unused').'</span>',
+                };
             },
         ]);
         $this->addColumn([

+ 2 - 2
packages/Longyi/Gift/src/Http/Controllers/Admin/GiftController.php

@@ -66,7 +66,7 @@ class GiftController extends Controller
             'customer_email'   => 'nullable|email',
             'customer_id'      => 'nullable|integer|exists:customers,id',
             'expirationdate'   => 'required|date_format:Y-m-d H:i:s',
-            'giftcard_status'  => 'required|in:1,2',
+            'giftcard_status'  => 'required|in:1,2,3',
         ]);
 
         // 如果填写了邮箱,必须验证通过(customer_id 不能为空)
@@ -130,7 +130,7 @@ class GiftController extends Controller
         $data = request()->validate([
             'id'                => 'required|integer',
             'expirationdate'    => 'required|date_format:Y-m-d H:i:s',
-            'giftcard_status'   => 'required|in:1,2',
+            'giftcard_status'   => 'required|in:1,2,3',
         ]);
 
         $giftCard = $this->giftCardsRepository->find($data['id']);

+ 15 - 2
packages/Longyi/Gift/src/Http/Controllers/Shop/GiftController.php

@@ -32,9 +32,9 @@ class GiftController extends Controller
     public function lists(): JsonResponse
     {
         $customerId = auth()->user()->id;
-        // 获取用户拥有的所有礼品卡
+        // 获取用户拥有的所有礼品卡(已过期的也算领过,避免重复兑换同一渠道)
         $userGiftCards = GiftCards::where('customer_id', $customerId)
-            ->where('giftcard_status', 1)
+            ->whereIn('giftcard_status', GiftCards::CLAIMED_STATUSES)
             ->get()
             ->keyBy('channel');
         $gifts = $this->giftCardsModel->giftcards();
@@ -60,6 +60,15 @@ class GiftController extends Controller
         }
         $customerId = auth()->user()->id;
         try {
+            // 该渠道已经领过(含已过期)的卡,不允许再拿积分重复兑换
+            $gift = $this->giftCardsModel->giftcards()[$id] ?? null;
+            $alreadyClaimed = $gift && GiftCards::where('customer_id', $customerId)
+                ->where('channel', $gift['channel'])
+                ->whereIn('giftcard_status', GiftCards::CLAIMED_STATUSES)
+                ->exists();
+            if ($alreadyClaimed) {
+                return ApiResponse::error('Gift card already claimed.');
+            }
             $this->giftCardsModel->add($id, $customerId);
         } catch (\Exception $e) {
             return ApiResponse::error($e->getMessage());
@@ -116,6 +125,10 @@ class GiftController extends Controller
            if (!$giftCard) {
                return ApiResponse::error('giftcard not found.');
            }
+           // 已过期的礼品卡不能再用
+           if ($giftCard->isExpired()) {
+               return ApiResponse::error('Gift card has expired.');
+           }
            $remainingGiftcardAmount = core()->convertPrice($giftCard->remaining_giftcard_amount);
            if ($remainingGiftcardAmount <= 0) {
                return ApiResponse::error('Gift card already used.');