|
|
@@ -12,6 +12,8 @@ use Carbon\Carbon;
|
|
|
use Illuminate\Http\Request;
|
|
|
use Illuminate\Routing\Controller;
|
|
|
use Longyi\RewardPoints\Models\RewardPointHistory;
|
|
|
+use Illuminate\Support\Facades\Redis;
|
|
|
+
|
|
|
class RewardPointsController extends Controller
|
|
|
{
|
|
|
protected $rewardPointRepository;
|
|
|
@@ -310,6 +312,113 @@ class RewardPointsController extends Controller
|
|
|
return ApiResponse::success([], 'Reward points removed successfully');
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 浏览产品赠送积分(每个产品每天仅赠送一次)
|
|
|
+ *
|
|
|
+ * @param Request $request
|
|
|
+ * @return \Illuminate\Http\JsonResponse
|
|
|
+ */
|
|
|
+ public function browseProduct(Request $request)
|
|
|
+ {
|
|
|
+ $customer = auth()->guard('customer')->user();
|
|
|
+
|
|
|
+ if (!$customer) {
|
|
|
+ return ApiResponse::unauthorized();
|
|
|
+ }
|
|
|
+
|
|
|
+ $productId = $request->input('categorys');
|
|
|
+
|
|
|
+ if (!$productId) {
|
|
|
+ return ApiResponse::validationError('categorys is required');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查询有效的浏览产品规则
|
|
|
+ $rule = RewardActiveRule::active()
|
|
|
+ ->ofType(RewardActiveRule::TYPE_BROWSE_PRODUCT)
|
|
|
+ ->first();
|
|
|
+
|
|
|
+ if (!$rule) {
|
|
|
+ return ApiResponse::forbidden('Browse product reward is not available');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查规则是否适用于当前客户
|
|
|
+ if (!$rule->isApplicableToCustomer($customer)) {
|
|
|
+ return ApiResponse::forbidden('This reward is not applicable to your account');
|
|
|
+ }
|
|
|
+
|
|
|
+ // Redis Key: browse_product:{customer_id}:{date}:{product_id}
|
|
|
+ $today = Carbon::now()->format('Y-m-d');
|
|
|
+ $redisKey = "browse_product:{$customer->id}:{$today}:{$productId}";
|
|
|
+
|
|
|
+ // Redis 判断该产品今天是否已经浏览过
|
|
|
+ if (Redis::exists($redisKey)) {
|
|
|
+ return ApiResponse::error('You have already earned points for browsing this product today');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取该客户应得的积分
|
|
|
+ $points = $rule->getPointsForCustomer($customer);
|
|
|
+ if ($points <= 0) {
|
|
|
+ $points = (int) $rule->reward_point;
|
|
|
+ }
|
|
|
+ if ($points <= 0) {
|
|
|
+ return ApiResponse::error('No points configured for browse product');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 添加积分
|
|
|
+ $this->rewardPointRepository->addPoints(
|
|
|
+ $customer->id,
|
|
|
+ RewardActiveRule::TYPE_BROWSE_PRODUCT,
|
|
|
+ $points,
|
|
|
+ null,
|
|
|
+ "Browsed categorys #{$productId}",
|
|
|
+ $rule
|
|
|
+ );
|
|
|
+
|
|
|
+ // 写入 Redis,TTL = 到当天结束的剩余秒数
|
|
|
+ $endOfDay = Carbon::now()->endOfDay();
|
|
|
+ $ttl = Carbon::now()->diffInSeconds($endOfDay);
|
|
|
+ Redis::setex($redisKey, $ttl, 1);
|
|
|
+
|
|
|
+ // 同时记录到当天已浏览产品集合(方便 getBrowsedProducts 读取)
|
|
|
+ $setKey = "browse_product:{$customer->id}:{$today}:set";
|
|
|
+ Redis::sadd($setKey, $productId);
|
|
|
+ Redis::expire($setKey, $ttl);
|
|
|
+
|
|
|
+ $totalPoints = $this->rewardPointRepository->getCustomerPoints($customer->id);
|
|
|
+
|
|
|
+ return ApiResponse::success([
|
|
|
+ 'categorys' => $productId,
|
|
|
+ 'points' => $points,
|
|
|
+ 'total_points' => $totalPoints
|
|
|
+ ], "You earned {$points} points for browsing this product!");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取今天已浏览并获得积分的产品列表
|
|
|
+ *
|
|
|
+ * @return \Illuminate\Http\JsonResponse
|
|
|
+ */
|
|
|
+ public function getBrowsedProducts()
|
|
|
+ {
|
|
|
+ $customer = auth()->guard('customer')->user();
|
|
|
+
|
|
|
+ if (!$customer) {
|
|
|
+ return ApiResponse::unauthorized();
|
|
|
+ }
|
|
|
+
|
|
|
+ $today = Carbon::now()->format('Y-m-d');
|
|
|
+ $setKey = "browse_product:{$customer->id}:{$today}:set";
|
|
|
+
|
|
|
+ // 从 Redis Set 中获取今天已浏览的产品ID列表
|
|
|
+ $browsedProductIds = Redis::smembers($setKey);
|
|
|
+ $browsedProductIds = array_map('intval', $browsedProductIds);
|
|
|
+
|
|
|
+ return ApiResponse::success([
|
|
|
+ 'browsed_product_ids' => $browsedProductIds,
|
|
|
+ 'browsed_count' => count($browsedProductIds)
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Get points information for cart
|
|
|
*/
|
|
|
@@ -341,6 +450,8 @@ class RewardPointsController extends Controller
|
|
|
7 => 'Share',
|
|
|
8 => 'Subscription',
|
|
|
9 => 'Login',
|
|
|
+ 13 => 'Browse Product',
|
|
|
+ 14 => 'Follow',
|
|
|
99 => 'admin',
|
|
|
];
|
|
|
|