排序
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -47,6 +47,18 @@ class ProductController extends BaseMiniController
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 先按分类树展示顺序(与小程序分类栏一致;不在启用分类树中的商品排最后),再按商品排序
|
||||||
|
$categoryIds = ProductCategoryModel::getTreeOrderedIds(true);
|
||||||
|
if ($categoryIds !== []) {
|
||||||
|
$cases = [];
|
||||||
|
foreach ($categoryIds as $position => $categoryId) {
|
||||||
|
$cases[] = "WHEN {$categoryId} THEN {$position}";
|
||||||
|
}
|
||||||
|
$query->orderByRaw(
|
||||||
|
'CASE category_id ' . implode(' ', $cases) . ' ELSE ' . count($categoryIds) . ' END'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
$pageSize = (int) $request->input('pageSize', 10);
|
$pageSize = (int) $request->input('pageSize', 10);
|
||||||
$paginator = $query->orderBy('sort', 'desc')
|
$paginator = $query->orderBy('sort', 'desc')
|
||||||
->orderBy('id')
|
->orderBy('id')
|
||||||
|
|||||||
@@ -74,6 +74,36 @@ class ProductCategoryModel extends Model
|
|||||||
return static::buildTree($query->get($columns)->toArray());
|
return static::buildTree($query->get($columns)->toArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按树展示顺序(sort/id 深度优先)获取分类 ID 列表,供商品列表等按分类顺序排序
|
||||||
|
*
|
||||||
|
* @param bool $onlyEnabled 是否仅包含启用分类
|
||||||
|
* @return array<int, int>
|
||||||
|
*/
|
||||||
|
public static function getTreeOrderedIds(bool $onlyEnabled = false): array
|
||||||
|
{
|
||||||
|
return static::flattenTreeIds(static::getTreeData(['id', 'parent_id'], $onlyEnabled));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 深度优先展开分类树为有序 ID 列表
|
||||||
|
*
|
||||||
|
* @param array<int, array<string, mixed>> $tree
|
||||||
|
* @return array<int, int>
|
||||||
|
*/
|
||||||
|
private static function flattenTreeIds(array $tree): array
|
||||||
|
{
|
||||||
|
$ids = [];
|
||||||
|
foreach ($tree as $node) {
|
||||||
|
$ids[] = (int) $node['id'];
|
||||||
|
if (!empty($node['children'])) {
|
||||||
|
$ids = [...$ids, ...static::flattenTreeIds($node['children'])];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $ids;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将扁平分类列表组装为树
|
* 将扁平分类列表组装为树
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ namespace Tests\Feature;
|
|||||||
|
|
||||||
use App\Models\CartModel;
|
use App\Models\CartModel;
|
||||||
use App\Models\CustomerLevelModel;
|
use App\Models\CustomerLevelModel;
|
||||||
|
use App\Models\ProductCategoryModel;
|
||||||
use App\Models\ProductModel;
|
use App\Models\ProductModel;
|
||||||
use App\Models\StoreModel;
|
use App\Models\StoreModel;
|
||||||
|
|
||||||
@@ -105,6 +106,27 @@ class MiniProductTest extends ProcurementTestCase
|
|||||||
$this->assertNull($row['price']);
|
$this->assertNull($row['price']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 商品列表:先按分类树展示顺序(深度优先),同分类内按商品 sort 降序、id 升序,未入树分类排最后 */
|
||||||
|
public function test_product_list_ordered_by_category_tree(): void
|
||||||
|
{
|
||||||
|
$rootA = ProductCategoryModel::create(['name' => '蔬菜', 'parent_id' => 0, 'sort' => 0, 'status' => 1]);
|
||||||
|
$rootB = ProductCategoryModel::create(['name' => '水果', 'parent_id' => 0, 'sort' => 1, 'status' => 1]);
|
||||||
|
// 子分类 sort 与创建顺序相反,验证按 sort 而非 id 排序
|
||||||
|
$leafA1 = ProductCategoryModel::create(['name' => '叶菜', 'parent_id' => $rootA->id, 'sort' => 1, 'status' => 1]);
|
||||||
|
$leafA2 = ProductCategoryModel::create(['name' => '根茎', 'parent_id' => $rootA->id, 'sort' => 0, 'status' => 1]);
|
||||||
|
|
||||||
|
$pB = ProductModel::factory()->create(['category_id' => $rootB->id, 'status' => 1]);
|
||||||
|
$pA1 = ProductModel::factory()->create(['category_id' => $leafA1->id, 'status' => 1]);
|
||||||
|
$pA2Low = ProductModel::factory()->create(['category_id' => $leafA2->id, 'status' => 1, 'sort' => 1]);
|
||||||
|
$pA2High = ProductModel::factory()->create(['category_id' => $leafA2->id, 'status' => 1, 'sort' => 9]);
|
||||||
|
$pNone = ProductModel::factory()->create(['category_id' => 0, 'status' => 1]);
|
||||||
|
|
||||||
|
$ids = array_column($this->getJson('/mini/product/list')->assertOk()->json('data.data'), 'id');
|
||||||
|
|
||||||
|
// 树序:rootA → leafA2 → leafA1 → rootB;leafA2 内 sort 降序;无分类排最后
|
||||||
|
$this->assertSame([$pA2High->id, $pA2Low->id, $pA1->id, $pB->id, $pNone->id], $ids);
|
||||||
|
}
|
||||||
|
|
||||||
/** 商品列表:登录门店附加购物车行ID与数量,响应附悬浮球汇总 */
|
/** 商品列表:登录门店附加购物车行ID与数量,响应附悬浮球汇总 */
|
||||||
public function test_product_list_appends_cart_quantity_and_summary(): void
|
public function test_product_list_appends_cart_quantity_and_summary(): void
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user