From 2d40e77720df57c958313a93e2013c5612edc09f Mon Sep 17 00:00:00 2001 From: panliang <1163816051@qq.com> Date: Tue, 13 Sep 2022 14:11:03 +0800 Subject: [PATCH] goods filter --- README.md | 2 ++ src/Filters/GoodsFilter.php | 22 ++------------------ src/Http/Api/GoodsCategoryController.php | 12 ++--------- src/Http/Api/GoodsController.php | 2 +- src/Http/Resources/GoodsCategoryResource.php | 3 --- src/Http/Resources/GoodsTinyResource.php | 5 +---- 6 files changed, 8 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index dd85c95..9a9abbf 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,8 @@ $menus = [ ]; ``` +## API接口 + ## 数据表 ### 商品分类: goods_category diff --git a/src/Filters/GoodsFilter.php b/src/Filters/GoodsFilter.php index 58741ad..ee762aa 100644 --- a/src/Filters/GoodsFilter.php +++ b/src/Filters/GoodsFilter.php @@ -3,7 +3,6 @@ namespace Peidikeji\Goods\Filters; use EloquentFilter\ModelFilter; -use Peidikeji\Merchant\Enums\CheckStatus; class GoodsFilter extends ModelFilter { @@ -19,15 +18,6 @@ class GoodsFilter extends ModelFilter }); } - public function merchant($v) - { - if (! $v) { - $this->whereNull('merchant_id'); - } else { - $this->where('merchant_id', $v); - } - } - public function recommend($v) { $this->where('is_recommend', $v); @@ -35,20 +25,12 @@ class GoodsFilter extends ModelFilter public function category($v) { - $this->where('category_id', $v); + // $this->where('category_id', $v); + $this->whereHas('category', fn($q) => $q->where('id', $v)->orWhere('path', 'like', '%-'.$v.'-%')); } public function sort($v) { $this->orderBy($v, request('sort_by', 'asc')); } - - public function state($state): GoodsFilter - { - return match ((int) $state) { - default => $this->where('on_sale', true), - 2 => $this->where('check_status', CheckStatus::Processing), - 3 => $this->where('on_sale', false) - }; - } } diff --git a/src/Http/Api/GoodsCategoryController.php b/src/Http/Api/GoodsCategoryController.php index d2bd74e..6e17744 100644 --- a/src/Http/Api/GoodsCategoryController.php +++ b/src/Http/Api/GoodsCategoryController.php @@ -3,21 +3,13 @@ namespace Peidikeji\Goods\Http\Api; use App\Http\Controllers\Controller; -use Peidikeji\Goods\Http\Resources\GoodsCategoryResource; use Peidikeji\Goods\Models\GoodsCategory; class GoodsCategoryController extends Controller { public function index() { - $query = GoodsCategory::with([ - 'children' => function ($q) { - $q->show()->sort(); - }, - ])->filter(['level' => 2])->show()->sort(); - - $list = $query->get(); - - return $this->json(GoodsCategoryResource::collection($list)); + $data = (new GoodsCategory())->toTree(GoodsCategory::show()->get()->all()); + return $this->json($data); } } diff --git a/src/Http/Api/GoodsController.php b/src/Http/Api/GoodsController.php index 9b6e4f4..55e25f5 100644 --- a/src/Http/Api/GoodsController.php +++ b/src/Http/Api/GoodsController.php @@ -13,7 +13,7 @@ class GoodsController extends Controller { public function index(Request $request) { - $query = Goods::with(['merchant'])->filter($request->all()); + $query = Goods::filter($request->all()); $list = $query->show()->sort()->paginate($request->input('per_page')); diff --git a/src/Http/Resources/GoodsCategoryResource.php b/src/Http/Resources/GoodsCategoryResource.php index e210d97..5b1f64b 100644 --- a/src/Http/Resources/GoodsCategoryResource.php +++ b/src/Http/Resources/GoodsCategoryResource.php @@ -3,7 +3,6 @@ namespace Peidikeji\Goods\Http\Resources; use Illuminate\Http\Resources\Json\JsonResource; -use Illuminate\Support\Str; class GoodsCategoryResource extends JsonResource { @@ -19,8 +18,6 @@ class GoodsCategoryResource extends JsonResource 'path' => $this->path, 'children' => static::collection($this->whenLoaded('children')), - - 'type' => Str::startsWith($this->path, '-2-') ? 'shop' : '', ]; } } diff --git a/src/Http/Resources/GoodsTinyResource.php b/src/Http/Resources/GoodsTinyResource.php index dd72d4e..f54957d 100644 --- a/src/Http/Resources/GoodsTinyResource.php +++ b/src/Http/Resources/GoodsTinyResource.php @@ -11,10 +11,8 @@ class GoodsTinyResource extends JsonResource { return [ 'id' => $this->id, - 'merchant_id' => $this->merchant_id, - 'merchant' => MerchantTinyResource::make($this->whenLoaded('merchant')), 'category_id' => $this->category_id, - 'type_id' => $this->category_id, + 'type_id' => $this->type_id, 'brand_id' => $this->brand_id, 'goods_sn' => $this->goods_sn, 'name' => $this->name, @@ -22,7 +20,6 @@ class GoodsTinyResource extends JsonResource 'images' => $this->images, 'description' => $this->description, 'price' => floatval($this->price), - 'vip_price' => floatval($this->vip_price), 'on_sale' => $this->on_sale, 'sold_count' => $this->sold_count, 'stock' => $this->stock,