diff --git a/app/Endpoint/Api/Http/Controllers/Product/PartController.php b/app/Endpoint/Api/Http/Controllers/Product/PartController.php index a02a804c..3b4b5bc9 100644 --- a/app/Endpoint/Api/Http/Controllers/Product/PartController.php +++ b/app/Endpoint/Api/Http/Controllers/Product/PartController.php @@ -3,6 +3,7 @@ namespace App\Endpoint\Api\Http\Controllers\Product; use App\Endpoint\Api\Http\Controllers\Controller; +use App\Endpoint\Api\Http\Resources\CouponResource; use App\Endpoint\Api\Http\Resources\ProductSkuTinyResource; use App\Helpers\Paginator; use App\Models\ProductPart; @@ -17,7 +18,7 @@ class PartController extends Controller * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\JsonResponse */ - public function __invoke(Request $request) + public function index(Request $request) { $part = null; @@ -43,4 +44,17 @@ class PartController extends Controller }) ); } + + public function coupons(Request $request) + { + $part = null; + if (filled($partKey = $request->query('part'))) { + $part = ProductPart::where('key', $partKey)->first(); + } + + return response()->json([ + 'coupons'=> $part ? CouponResource::collection($part->getCoupons()->get()) : [], + 'description' => $part?->description, + ]); + } } diff --git a/app/Endpoint/Api/Http/Resources/CouponResource.php b/app/Endpoint/Api/Http/Resources/CouponResource.php new file mode 100644 index 00000000..0c0f10e2 --- /dev/null +++ b/app/Endpoint/Api/Http/Resources/CouponResource.php @@ -0,0 +1,26 @@ + $this->id, + 'name' => $this->name, + 'type' => $this->type, + 'amount' => $this->amount, + 'threshold' => $this->threshold, + // 'receive_status' => $this->whenLoaded('likesInfo', $this->likesInfo->count() > 0), + ]; + } +} diff --git a/app/Endpoint/Api/routes.php b/app/Endpoint/Api/routes.php index 8ef35d70..42b8d5ce 100644 --- a/app/Endpoint/Api/routes.php +++ b/app/Endpoint/Api/routes.php @@ -57,7 +57,8 @@ Route::group([ // 热销商品 Route::get('product/hot', HotController::class); // 商品分区 - Route::get('product/part', PartController::class); + Route::get('product/part', [PartController::class, 'index']); + Route::get('product/part-coupons', [PartController::class, 'coupons']); // 筛选商品 Route::get('product/products', [ProductSkuController::class, 'index']); // 查看商品 diff --git a/app/Models/ProductPart.php b/app/Models/ProductPart.php index d73151e9..33064b9f 100644 --- a/app/Models/ProductPart.php +++ b/app/Models/ProductPart.php @@ -54,10 +54,19 @@ class ProductPart extends Model } /** - * 此分区购买赠送的券 + * 此分区购买赠送的券关系 */ public function coupons() { return $this->hasMany(ProductPartCoupon::class, 'part_id'); } + + /** + * 返回实际关联的券 + * @return queryBuilder + */ + public function getCoupons() + { + return Coupon::whereIn('id', $this->coupons->pluck('coupon_id')->toArray()); + } }