goods filter
parent
55cc3ace9a
commit
2d40e77720
|
|
@ -40,6 +40,8 @@ $menus = [
|
||||||
];
|
];
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## API接口
|
||||||
|
|
||||||
## 数据表
|
## 数据表
|
||||||
|
|
||||||
### 商品分类: goods_category
|
### 商品分类: goods_category
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@
|
||||||
namespace Peidikeji\Goods\Filters;
|
namespace Peidikeji\Goods\Filters;
|
||||||
|
|
||||||
use EloquentFilter\ModelFilter;
|
use EloquentFilter\ModelFilter;
|
||||||
use Peidikeji\Merchant\Enums\CheckStatus;
|
|
||||||
|
|
||||||
class GoodsFilter extends ModelFilter
|
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)
|
public function recommend($v)
|
||||||
{
|
{
|
||||||
$this->where('is_recommend', $v);
|
$this->where('is_recommend', $v);
|
||||||
|
|
@ -35,20 +25,12 @@ class GoodsFilter extends ModelFilter
|
||||||
|
|
||||||
public function category($v)
|
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)
|
public function sort($v)
|
||||||
{
|
{
|
||||||
$this->orderBy($v, request('sort_by', 'asc'));
|
$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)
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,21 +3,13 @@
|
||||||
namespace Peidikeji\Goods\Http\Api;
|
namespace Peidikeji\Goods\Http\Api;
|
||||||
|
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use Peidikeji\Goods\Http\Resources\GoodsCategoryResource;
|
|
||||||
use Peidikeji\Goods\Models\GoodsCategory;
|
use Peidikeji\Goods\Models\GoodsCategory;
|
||||||
|
|
||||||
class GoodsCategoryController extends Controller
|
class GoodsCategoryController extends Controller
|
||||||
{
|
{
|
||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
$query = GoodsCategory::with([
|
$data = (new GoodsCategory())->toTree(GoodsCategory::show()->get()->all());
|
||||||
'children' => function ($q) {
|
return $this->json($data);
|
||||||
$q->show()->sort();
|
|
||||||
},
|
|
||||||
])->filter(['level' => 2])->show()->sort();
|
|
||||||
|
|
||||||
$list = $query->get();
|
|
||||||
|
|
||||||
return $this->json(GoodsCategoryResource::collection($list));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ class GoodsController extends Controller
|
||||||
{
|
{
|
||||||
public function index(Request $request)
|
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'));
|
$list = $query->show()->sort()->paginate($request->input('per_page'));
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@
|
||||||
namespace Peidikeji\Goods\Http\Resources;
|
namespace Peidikeji\Goods\Http\Resources;
|
||||||
|
|
||||||
use Illuminate\Http\Resources\Json\JsonResource;
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
use Illuminate\Support\Str;
|
|
||||||
|
|
||||||
class GoodsCategoryResource extends JsonResource
|
class GoodsCategoryResource extends JsonResource
|
||||||
{
|
{
|
||||||
|
|
@ -19,8 +18,6 @@ class GoodsCategoryResource extends JsonResource
|
||||||
'path' => $this->path,
|
'path' => $this->path,
|
||||||
|
|
||||||
'children' => static::collection($this->whenLoaded('children')),
|
'children' => static::collection($this->whenLoaded('children')),
|
||||||
|
|
||||||
'type' => Str::startsWith($this->path, '-2-') ? 'shop' : '',
|
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,10 +11,8 @@ class GoodsTinyResource extends JsonResource
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'id' => $this->id,
|
'id' => $this->id,
|
||||||
'merchant_id' => $this->merchant_id,
|
|
||||||
'merchant' => MerchantTinyResource::make($this->whenLoaded('merchant')),
|
|
||||||
'category_id' => $this->category_id,
|
'category_id' => $this->category_id,
|
||||||
'type_id' => $this->category_id,
|
'type_id' => $this->type_id,
|
||||||
'brand_id' => $this->brand_id,
|
'brand_id' => $this->brand_id,
|
||||||
'goods_sn' => $this->goods_sn,
|
'goods_sn' => $this->goods_sn,
|
||||||
'name' => $this->name,
|
'name' => $this->name,
|
||||||
|
|
@ -22,7 +20,6 @@ class GoodsTinyResource extends JsonResource
|
||||||
'images' => $this->images,
|
'images' => $this->images,
|
||||||
'description' => $this->description,
|
'description' => $this->description,
|
||||||
'price' => floatval($this->price),
|
'price' => floatval($this->price),
|
||||||
'vip_price' => floatval($this->vip_price),
|
|
||||||
'on_sale' => $this->on_sale,
|
'on_sale' => $this->on_sale,
|
||||||
'sold_count' => $this->sold_count,
|
'sold_count' => $this->sold_count,
|
||||||
'stock' => $this->stock,
|
'stock' => $this->stock,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue