rewardPointRepository = $rewardPointRepository; $this->_config = request('_config'); } public function index() { $customer = auth()->guard('customer')->user(); if (!$customer) { return redirect()->route('customer.session.index'); } $points = $this->rewardPointRepository->getCustomerPoints($customer->id); $history = $this->rewardPointRepository->getHistory($customer->id); return view($this->_config['view'], compact('points', 'history')); } public function signIn() { $customer = auth()->guard('customer')->user(); if (!$customer) { return response()->json(['error' => 'Please login first'], 401); } $today = Carbon::now()->format('Y-m-d'); $existingSign = RewardPointCustomerSign::where('customer_id', $customer->id) ->where('sign_date', $today) ->first(); if ($existingSign) { return response()->json(['error' => 'Already signed in today'], 400); } $lastSign = RewardPointCustomerSign::where('customer_id', $customer->id) ->orderBy('sign_date', 'desc') ->first(); $countDate = 1; if ($lastSign) { $lastSignDate = Carbon::parse($lastSign->sign_date); $yesterday = Carbon::yesterday(); if ($lastSignDate->toDateString() === $yesterday->toDateString()) { $countDate = $lastSign->count_date + 1; } elseif ($lastSignDate->toDateString() !== $today) { $countDate = 1; } } $rule = RewardActiveRule::where('type_of_transaction', RewardActiveRule::TYPE_SIGN_IN) ->where('status', 1) ->first(); if ($rule) { $points = $rule->reward_point; } else { $points = $this->getSignInPointsByDay($countDate); } $sign = RewardPointCustomerSign::create([ 'customer_id' => $customer->id, 'sign_date' => $today, 'count_date' => $countDate, 'point' => $points, 'code' => uniqid('SIGN_'), 'created' => Carbon::now(), 'updated' => Carbon::now() ]); $this->rewardPointRepository->addPoints( $customer->id, RewardActiveRule::TYPE_SIGN_IN, $points, null, "每日签到奖励(第 {$countDate} 天)" ); return response()->json([ 'success' => true, 'points' => $points, 'streak' => $countDate, 'total_points' => $this->rewardPointRepository->getCustomerPoints($customer->id) ]); } /** * 根据签到天数获取对应的积分 */ protected function getSignInPointsByDay($day) { $settingRepository = app(\Longyi\RewardPoints\Repositories\RewardPointSettingRepository::class); $dayPointsMap = [ 1 => $settingRepository->getConfigValue('signin_day1_points', 10), 2 => $settingRepository->getConfigValue('signin_day2_points', 10), 3 => $settingRepository->getConfigValue('signin_day3_points', 15), 4 => $settingRepository->getConfigValue('signin_day4_points', 25), 5 => $settingRepository->getConfigValue('signin_day5_points', 50), 6 => $settingRepository->getConfigValue('signin_day6_points', 70), 7 => $settingRepository->getConfigValue('signin_day7_points', 70), ]; if ($day <= 7) { return (int) $dayPointsMap[$day]; } return (int) $settingRepository->getConfigValue('signin_day8_plus_points', 70); } public function getSignStatus() { $customer = auth()->guard('customer')->user(); if (!$customer) { return response()->json(['error' => 'Please login first'], 401); } $today = Carbon::now()->format('Y-m-d'); $signedToday = RewardPointCustomerSign::where('customer_id', $customer->id) ->where('sign_date', $today) ->exists(); $lastSign = RewardPointCustomerSign::where('customer_id', $customer->id) ->orderBy('sign_date', 'desc') ->first(); return response()->json([ 'signed_today' => $signedToday, 'current_streak' => $lastSign ? $lastSign->count_date : 0, 'total_points' => $this->rewardPointRepository->getCustomerPoints($customer->id) ]); } /** * Apply reward points to cart */ public function applyPoints(Request $request) { $points = $request->input('points', 0); $cartRewardPoints = app('cartrewardpoints'); $validation = $cartRewardPoints->validatePoints($points); if ($validation !== true) { return response()->json([ 'success' => false, 'message' => $validation ], 400); } $result = $cartRewardPoints->applyPoints($points); if ($result) { $discountDetails = $cartRewardPoints->getDiscountDetails(); return response()->json([ 'success' => true, 'message' => 'Reward points applied successfully', 'discount' => $discountDetails ]); } return response()->json([ 'success' => false, 'message' => 'Failed to apply reward points' ], 400); } /** * Remove reward points from cart */ public function removePoints() { $cartRewardPoints = app('cartrewardpoints'); $cartRewardPoints->removePoints(); return response()->json([ 'success' => true, 'message' => 'Reward points removed successfully' ]); } /** * Get points information for cart */ public function getPointsInfo() { $cartRewardPoints = app('cartrewardpoints'); return response()->json([ 'available_points' => $cartRewardPoints->getAvailablePoints(), 'points_used' => $cartRewardPoints->getPointsUsed(), 'discount_amount' => $cartRewardPoints->getDiscountAmount(), 'points_value' => $cartRewardPoints->getPointsValue(1), 'max_points_allowed' => $cartRewardPoints->getMaxPointsByCartTotal() ]); } }