瀏覽代碼

生日接口

bianjunhui 1 周之前
父節點
當前提交
96480477dc

+ 47 - 0
packages/Longyi/RewardPoints/src/Repositories/RewardPointRepository.php

@@ -234,6 +234,10 @@ class RewardPointRepository extends Repository
                 $customerPoints->last_checkout = Carbon::now();
                 $customerPoints->save();
 
+                // 按 FIFO(先过期先扣)扣减收入记录的 point_remaining,
+                // 避免这些积分到期时被 checkExpiredPoints 再次扣除
+                $this->deductFromEarningRecords($customerId, (int) $amount);
+
                 // 创建历史记录
                 $history = $this->create([
                     'customer_id' => $customerId,
@@ -267,6 +271,49 @@ class RewardPointRepository extends Repository
             throw $e;
         }
     }
+    /**
+     * 按 FIFO(先过期先扣)顺序扣减收入记录的 point_remaining。
+     *
+     * 当用户消费积分时,需要同步扣减历史"获得积分"记录的剩余可过期积分,
+     * 否则这些积分到期时会被 checkExpiredPoints 再次扣除,导致重复扣分。
+     *
+     * @param  int  $customerId
+     * @param  int  $amount  需要扣减的积分总数(正数)
+     */
+    protected function deductFromEarningRecords($customerId, int $amount): void
+    {
+        if ($amount <= 0) {
+            return;
+        }
+
+        // 只处理有剩余积分、有到期时间、未过期、状态为 COMPLETED 的"获得积分"记录
+        // 优先扣减先到期的记录(FIFO),符合用户利益
+        $earningRecords = $this->where('customer_id', $customerId)
+            ->where('amount', '>', 0)
+            ->whereNotNull('expired_time')
+            ->where('expired_time', '>', Carbon::now())
+            ->where('status', RewardPointHistory::STATUS_COMPLETED)
+            ->where('point_remaining', '>', 0)
+            ->orderByRaw('expired_time ASC, transaction_time ASC, history_id ASC')
+            ->lockForUpdate()
+            ->get();
+
+        $remaining = $amount;
+
+        foreach ($earningRecords as $record) {
+            if ($remaining <= 0) {
+                break;
+            }
+
+            $deduct = min((int) $record->point_remaining, $remaining);
+
+            $record->point_remaining = (int) $record->point_remaining - $deduct;
+            $record->save();
+
+            $remaining -= $deduct;
+        }
+    }
+
     public function getHistory($customerId, $limit = 20, $page = null,$amountType = '')
     {
         $query = $this->where('customer_id', $customerId)

+ 29 - 7
packages/Webkul/Shop/src/Http/Controllers/API/CustomerController.php

@@ -59,13 +59,15 @@ class CustomerController extends APIController
 
         return ApiResponse::success([
             'user' => [
-                'id'         => $customer->id,
-                'name'       => $customer->name,
-                'first_name' => $customer->first_name,
-                'last_name'  => $customer->last_name,
-                'email'      => $customer->email,
-                'phone'      => $customer->phone,
-                'image_url'  => $customer->image_url,
+                'id'            => $customer->id,
+                'name'          => $customer->name,
+                'first_name'    => $customer->first_name,
+                'last_name'     => $customer->last_name,
+                'email'         => $customer->email,
+                'phone'         => $customer->phone,
+                'image_url'     => $customer->image_url,
+                'gender'        => $customer->gender,
+                'date_of_birth' => $customer->date_of_birth,
             ],
             'growth' => [
                 'growth_value'          => (int) $growth['growth_value'],
@@ -86,6 +88,26 @@ class CustomerController extends APIController
         ]);
     }
 
+    /**
+     * 获取用户生日(需登录)
+     *
+     * GET /api/customer/birthday
+     *
+     * @return JsonResponse
+     */
+    public function birthday(): JsonResponse
+    {
+        $customer = auth()->guard('customer')->user();
+
+        if (! $customer) {
+            return ApiResponse::unauthorized();
+        }
+
+        return ApiResponse::success([
+            'date_of_birth' => $customer->date_of_birth,
+        ]);
+    }
+
     /**
      * 验证邮箱是否已注册(无需登录)
      *

+ 1 - 0
packages/Webkul/Shop/src/Routes/api-token.php

@@ -38,6 +38,7 @@ Route::group(['prefix' => 'api'], function () {
         ->prefix('customer')
         ->group(function () {
             Route::get('profile', 'profile')->name('shop.api.token.customer.profile');
+            Route::get('birthday', 'birthday')->name('shop.api.token.customer.birthday');
         });
 
     /** 客户地址管理(Bearer Token 认证) */