RewardPointsController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. namespace Longyi\RewardPoints\Http\Controllers;
  3. use Longyi\RewardPoints\Repositories\RewardPointRepository;
  4. use Longyi\RewardPoints\Models\RewardPointCustomerSign;
  5. use Longyi\RewardPoints\Models\RewardActiveRule;
  6. use Carbon\Carbon;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Routing\Controller;
  9. use Webkul\User\Repositories\RoleRepository;
  10. use Illuminate\Support\Facades\DB;
  11. class RewardPointsController extends Controller
  12. {
  13. protected $rewardPointRepository;
  14. protected $_config;
  15. public function __construct(RewardPointRepository $rewardPointRepository)
  16. {
  17. $this->rewardPointRepository = $rewardPointRepository;
  18. $this->_config = request('_config');
  19. }
  20. public function index()
  21. {
  22. $customer = auth()->guard('customer')->user();
  23. if (!$customer) {
  24. return redirect()->route('customer.session.index');
  25. }
  26. $points = $this->rewardPointRepository->getCustomerPoints($customer->id);
  27. $history = $this->rewardPointRepository->getHistory($customer->id);
  28. return view($this->_config['view'], compact('points', 'history'));
  29. }
  30. public function signIn()
  31. {
  32. $customer = auth()->guard('customer')->user();
  33. if (!$customer) {
  34. return response()->json(['error' => 'Please login first'], 401);
  35. }
  36. $today = Carbon::now()->format('Y-m-d');
  37. $existingSign = RewardPointCustomerSign::where('customer_id', $customer->id)
  38. ->where('sign_date', $today)
  39. ->first();
  40. if ($existingSign) {
  41. return response()->json(['error' => 'Already signed in today'], 400);
  42. }
  43. $lastSign = RewardPointCustomerSign::where('customer_id', $customer->id)
  44. ->orderBy('sign_date', 'desc')
  45. ->first();
  46. $countDate = 1;
  47. if ($lastSign) {
  48. $lastSignDate = Carbon::parse($lastSign->sign_date);
  49. $yesterday = Carbon::yesterday();
  50. if ($lastSignDate->toDateString() === $yesterday->toDateString()) {
  51. $countDate = $lastSign->count_date + 1;
  52. } elseif ($lastSignDate->toDateString() !== $today) {
  53. $countDate = 1;
  54. }
  55. }
  56. $rule = RewardActiveRule::where('type_of_transaction', RewardActiveRule::TYPE_SIGN_IN)
  57. ->where('status', 1)
  58. ->first();
  59. if ($rule) {
  60. $points = $rule->reward_point;
  61. } else {
  62. $points = $this->getSignInPointsByDay($countDate);
  63. }
  64. $sign = RewardPointCustomerSign::create([
  65. 'customer_id' => $customer->id,
  66. 'sign_date' => $today,
  67. 'count_date' => $countDate,
  68. 'point' => $points,
  69. 'code' => uniqid('SIGN_'),
  70. 'created' => Carbon::now(),
  71. 'updated' => Carbon::now()
  72. ]);
  73. $this->rewardPointRepository->addPoints(
  74. $customer->id,
  75. RewardActiveRule::TYPE_SIGN_IN,
  76. $points,
  77. null,
  78. "每日签到奖励(第 {$countDate} 天)"
  79. );
  80. return response()->json([
  81. 'success' => true,
  82. 'points' => $points,
  83. 'streak' => $countDate,
  84. 'total_points' => $this->rewardPointRepository->getCustomerPoints($customer->id)
  85. ]);
  86. }
  87. /**
  88. * 根据签到天数获取对应的积分
  89. */
  90. protected function getSignInPointsByDay($day)
  91. {
  92. $settingRepository = app(\Longyi\RewardPoints\Repositories\RewardPointSettingRepository::class);
  93. $dayPointsMap = [
  94. 1 => $settingRepository->getConfigValue('signin_day1_points', 10),
  95. 2 => $settingRepository->getConfigValue('signin_day2_points', 10),
  96. 3 => $settingRepository->getConfigValue('signin_day3_points', 15),
  97. 4 => $settingRepository->getConfigValue('signin_day4_points', 25),
  98. 5 => $settingRepository->getConfigValue('signin_day5_points', 50),
  99. 6 => $settingRepository->getConfigValue('signin_day6_points', 70),
  100. 7 => $settingRepository->getConfigValue('signin_day7_points', 70),
  101. ];
  102. if ($day <= 7) {
  103. return (int) $dayPointsMap[$day];
  104. }
  105. return (int) $settingRepository->getConfigValue('signin_day8_plus_points', 70);
  106. }
  107. public function getSignStatus()
  108. {
  109. $customer = auth()->guard('customer')->user();
  110. if (!$customer) {
  111. return response()->json(['error' => 'Please login first'], 401);
  112. }
  113. $today = Carbon::now()->format('Y-m-d');
  114. $signedToday = RewardPointCustomerSign::where('customer_id', $customer->id)
  115. ->where('sign_date', $today)
  116. ->exists();
  117. $lastSign = RewardPointCustomerSign::where('customer_id', $customer->id)
  118. ->orderBy('sign_date', 'desc')
  119. ->first();
  120. return response()->json([
  121. 'signed_today' => $signedToday,
  122. 'current_streak' => $lastSign ? $lastSign->count_date : 0,
  123. 'total_points' => $this->rewardPointRepository->getCustomerPoints($customer->id)
  124. ]);
  125. }
  126. /**
  127. * Apply reward points to cart
  128. */
  129. public function applyPoints(Request $request)
  130. {
  131. $points = $request->input('points', 0);
  132. $cartRewardPoints = app('cartrewardpoints');
  133. $validation = $cartRewardPoints->validatePoints($points);
  134. if ($validation !== true) {
  135. return response()->json([
  136. 'success' => false,
  137. 'message' => $validation
  138. ], 400);
  139. }
  140. $result = $cartRewardPoints->applyPoints($points);
  141. if ($result) {
  142. $discountDetails = $cartRewardPoints->getDiscountDetails();
  143. return response()->json([
  144. 'success' => true,
  145. 'message' => 'Reward points applied successfully',
  146. 'discount' => $discountDetails
  147. ]);
  148. }
  149. return response()->json([
  150. 'success' => false,
  151. 'message' => 'Failed to apply reward points'
  152. ], 400);
  153. }
  154. /**
  155. * Remove reward points from cart
  156. */
  157. public function removePoints()
  158. {
  159. $cartRewardPoints = app('cartrewardpoints');
  160. $cartRewardPoints->removePoints();
  161. return response()->json([
  162. 'success' => true,
  163. 'message' => 'Reward points removed successfully'
  164. ]);
  165. }
  166. /**
  167. * Get points information for cart
  168. */
  169. public function getPointsInfo()
  170. {
  171. $cartRewardPoints = app('cartrewardpoints');
  172. return response()->json([
  173. 'available_points' => $cartRewardPoints->getAvailablePoints(),
  174. 'points_used' => $cartRewardPoints->getPointsUsed(),
  175. 'discount_amount' => $cartRewardPoints->getDiscountAmount(),
  176. 'points_value' => $cartRewardPoints->getPointsValue(1),
  177. 'max_points_allowed' => $cartRewardPoints->getMaxPointsByCartTotal()
  178. ]);
  179. }
  180. }