|
|
@@ -6,13 +6,86 @@ use Illuminate\Http\JsonResponse;
|
|
|
use Illuminate\Http\Request;
|
|
|
use Illuminate\Http\Response;
|
|
|
use Illuminate\Support\Facades\Cookie;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
use Illuminate\Support\Facades\Event;
|
|
|
use Longyi\RewardPoints\Helpers\ApiResponse;
|
|
|
+use Longyi\RewardPoints\Repositories\RewardPointRepository;
|
|
|
+use Longyi\RewardPoints\Services\GrowthValueService;
|
|
|
use Webkul\Customer\Models\Customer;
|
|
|
use Webkul\Shop\Http\Requests\Customer\LoginRequest;
|
|
|
|
|
|
class CustomerController extends APIController
|
|
|
{
|
|
|
+ public function __construct(
|
|
|
+ protected RewardPointRepository $rewardPointRepository,
|
|
|
+ protected GrowthValueService $growthValueService
|
|
|
+ ) {}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 个人中心数据聚合接口(需登录)
|
|
|
+ *
|
|
|
+ * GET /api/customer/profile
|
|
|
+ *
|
|
|
+ * 返回:用户基础信息、成长值(VIP)、积分、优惠券数量、订单总数。
|
|
|
+ *
|
|
|
+ * @return JsonResponse
|
|
|
+ */
|
|
|
+ public function profile(): JsonResponse
|
|
|
+ {
|
|
|
+ $customer = auth()->guard('customer')->user();
|
|
|
+ if (! $customer) {
|
|
|
+ return ApiResponse::unauthorized();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 1. 成长值 / VIP 信息
|
|
|
+ $growth = $this->growthValueService->getCustomerGrowthValue($customer->id);
|
|
|
+
|
|
|
+ // 2. 积分余额
|
|
|
+ $points = (int) $this->rewardPointRepository->getCustomerPoints($customer->id);
|
|
|
+
|
|
|
+ // 3. 优惠券数量(用户专属券,未过期)
|
|
|
+ $couponCount = (int) DB::table('cart_rule_coupons')
|
|
|
+ ->where('customer_id', $customer->id)
|
|
|
+ ->where(function ($query) {
|
|
|
+ $query->whereNull('expired_at')
|
|
|
+ ->orWhere('expired_at', '>=', now());
|
|
|
+ })
|
|
|
+ ->count();
|
|
|
+
|
|
|
+ // 4. 订单总数
|
|
|
+ $orderCount = (int) DB::table('orders')
|
|
|
+ ->where('customer_id', $customer->id)
|
|
|
+ ->count();
|
|
|
+
|
|
|
+ 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,
|
|
|
+ ],
|
|
|
+ 'growth' => [
|
|
|
+ 'growth_value' => (int) $growth['growth_value'],
|
|
|
+ 'level_name' => $growth['growth_level_name'] ?? null,
|
|
|
+ 'discount_rate' => $growth['growth_discount_rate'] ?? 0,
|
|
|
+ 'level_icon' => $growth['growth_level_icon'] ?? null,
|
|
|
+ 'benefits' => is_string($growth['growth_benefits'] ?? null)
|
|
|
+ ? json_decode($growth['growth_benefits'], true)
|
|
|
+ : ($growth['growth_benefits'] ?? []),
|
|
|
+ 'next_level' => $growth['next_level'] ?? null,
|
|
|
+ 'growth_to_next_level' => $growth['points_to_next_level'] ?? null,
|
|
|
+ 'expires_at' => $growth['expires_at'] ?? null,
|
|
|
+ 'first_order_completed' => (bool) ($growth['first_order_completed'] ?? false),
|
|
|
+ ],
|
|
|
+ 'points' => $points,
|
|
|
+ 'coupon_count' => $couponCount,
|
|
|
+ 'order_count' => $orderCount,
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 验证邮箱是否已注册(无需登录)
|
|
|
*
|