| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- <?php
- namespace Longyi\RewardPoints\Http\Controllers;
- use Longyi\RewardPoints\Repositories\RewardPointRepository;
- use Longyi\RewardPoints\Models\RewardPointCustomerSign;
- use Longyi\RewardPoints\Models\RewardActiveRule;
- use Carbon\Carbon;
- use Illuminate\Http\Request;
- use Illuminate\Routing\Controller;
- use Webkul\User\Repositories\RoleRepository;
- use Illuminate\Support\Facades\DB;
- class RewardPointsController extends Controller
- {
- protected $rewardPointRepository;
- protected $_config;
- public function __construct(RewardPointRepository $rewardPointRepository)
- {
- $this->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()
- ]);
- }
- }
|