@gt6/sdk 1.0.29 → 1.0.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -911,12 +911,15 @@ class ProductsAPI {
911
911
  }
912
912
  /**
913
913
  * 4.3. 根据大分类ID获取平台产品列表
914
- * 先从big-product-categories/${categoryId}.json获取产品ID,再分页后用getProduct_p获取详情
914
+ * 适配big-product-categories/${categoryId}.json新结构,支持排序和分页,直接返回数据
915
+ * @param categoryId 分类ID
916
+ * @param options { page, limit, sortBy }
917
+ * sortBy: 'createdAt-desc' | 'createdAt-asc' | 'price-desc' | 'price-asc'
915
918
  */
916
919
  async getProductsByCategory_b(categoryId, options) {
917
- // 从大型分类静态文件获取该分类的产品ID列表
920
+ // 从大型分类静态文件获取该分类的产品信息列表
918
921
  const categoryData = await this.client.request(`/big-product-categories/${categoryId}.json`);
919
- if (!categoryData || !categoryData.productIds || categoryData.productIds.length === 0) {
922
+ if (!categoryData || !categoryData.productInfos || categoryData.productInfos.length === 0) {
920
923
  return {
921
924
  products: [],
922
925
  total: 0,
@@ -924,21 +927,34 @@ class ProductsAPI {
924
927
  limit: options?.limit || 10
925
928
  };
926
929
  }
930
+ // 排序
931
+ let sortedProducts = [...categoryData.productInfos];
932
+ const sortBy = options?.sortBy || 'createdAt-desc';
933
+ if (sortBy === 'createdAt-desc') {
934
+ sortedProducts.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime());
935
+ }
936
+ else if (sortBy === 'createdAt-asc') {
937
+ sortedProducts.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
938
+ }
939
+ else if (sortBy === 'price-desc') {
940
+ sortedProducts.sort((a, b) => parseFloat(b.price) - parseFloat(a.price));
941
+ }
942
+ else if (sortBy === 'price-asc') {
943
+ sortedProducts.sort((a, b) => parseFloat(a.price) - parseFloat(b.price));
944
+ }
927
945
  // 应用分页
928
946
  const page = options?.page || 1;
929
947
  const limit = options?.limit || 10;
930
948
  const offset = (page - 1) * limit;
931
- const paginatedProductIds = categoryData.productIds.slice(offset, offset + limit);
932
- // 获取产品详情(使用上级平台数据)
933
- const products = await Promise.all(paginatedProductIds.map(productId => this.getProduct_p(productId)));
934
- // 应用状态过滤
935
- let filteredProducts = products;
949
+ const paginatedProducts = sortedProducts.slice(offset, offset + limit);
950
+ // 应用状态过滤(如果有status字段)
951
+ let filteredProducts = paginatedProducts;
936
952
  if (options?.status !== undefined) {
937
- filteredProducts = products.filter(product => product.status === options.status);
953
+ filteredProducts = paginatedProducts.filter(product => product.status === options.status);
938
954
  }
939
955
  return {
940
956
  products: filteredProducts,
941
- total: categoryData.productIds.length,
957
+ total: categoryData.productInfos.length,
942
958
  page,
943
959
  limit
944
960
  };