商品列表优化
This commit is contained in:
@@ -140,7 +140,7 @@ class ProductController extends BaseController
|
||||
}
|
||||
|
||||
/**
|
||||
* A2 价格矩阵:行=商品(支持 category_id / keyword 过滤),列=全部启用等级,值=price(缺失为 null)
|
||||
* A2 价格矩阵:行=商品(支持 category_id / keyword 过滤 + page/pageSize 服务端分页),列=全部启用等级,值=price(缺失为 null)
|
||||
*/
|
||||
#[GetRoute('/priceMatrix', 'query')]
|
||||
public function priceMatrix(Request $request): JsonResponse
|
||||
@@ -156,14 +156,16 @@ class ProductController extends BaseController
|
||||
->orWhere('spec', 'like', '%' . $keyword . '%');
|
||||
});
|
||||
}
|
||||
$products = $query->orderBy('sort')->orderBy('id')->get();
|
||||
$pageSize = max(1, min(100, (int) $request->input('pageSize', 20)));
|
||||
$page = max(1, (int) $request->input('page', 1));
|
||||
$products = $query->orderBy('sort')->orderBy('id')->paginate($pageSize, ['*'], 'page', $page);
|
||||
|
||||
$levels = CustomerLevelModel::query()
|
||||
->where('status', CustomerLevelModel::STATUS_NORMAL)
|
||||
->orderBy('sort')
|
||||
->get(['id', 'name']);
|
||||
|
||||
$rows = $products->map(function (ProductModel $product) use ($levels) {
|
||||
$rows = $products->getCollection()->map(function (ProductModel $product) use ($levels) {
|
||||
$priceMap = $product->prices->keyBy('level_id');
|
||||
$row = [
|
||||
'id' => $product->id,
|
||||
@@ -181,7 +183,8 @@ class ProductController extends BaseController
|
||||
|
||||
return $this->success([
|
||||
'levels' => $levels->toArray(),
|
||||
'rows' => $rows->toArray(),
|
||||
'rows' => $rows->values()->toArray(),
|
||||
'total' => $products->total(),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user