@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.
@@ -907,12 +907,15 @@ class ProductsAPI {
907
907
  }
908
908
  /**
909
909
  * 4.3. 根据大分类ID获取平台产品列表
910
- * 先从big-product-categories/${categoryId}.json获取产品ID,再分页后用getProduct_p获取详情
910
+ * 适配big-product-categories/${categoryId}.json新结构,支持排序和分页,直接返回数据
911
+ * @param categoryId 分类ID
912
+ * @param options { page, limit, sortBy }
913
+ * sortBy: 'createdAt-desc' | 'createdAt-asc' | 'price-desc' | 'price-asc'
911
914
  */
912
915
  async getProductsByCategory_b(categoryId, options) {
913
- // 从大型分类静态文件获取该分类的产品ID列表
916
+ // 从大型分类静态文件获取该分类的产品信息列表
914
917
  const categoryData = await this.client.request(`/big-product-categories/${categoryId}.json`);
915
- if (!categoryData || !categoryData.productIds || categoryData.productIds.length === 0) {
918
+ if (!categoryData || !categoryData.productInfos || categoryData.productInfos.length === 0) {
916
919
  return {
917
920
  products: [],
918
921
  total: 0,
@@ -920,21 +923,34 @@ class ProductsAPI {
920
923
  limit: options?.limit || 10
921
924
  };
922
925
  }
926
+ // 排序
927
+ let sortedProducts = [...categoryData.productInfos];
928
+ const sortBy = options?.sortBy || 'createdAt-desc';
929
+ if (sortBy === 'createdAt-desc') {
930
+ sortedProducts.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime());
931
+ }
932
+ else if (sortBy === 'createdAt-asc') {
933
+ sortedProducts.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
934
+ }
935
+ else if (sortBy === 'price-desc') {
936
+ sortedProducts.sort((a, b) => parseFloat(b.price) - parseFloat(a.price));
937
+ }
938
+ else if (sortBy === 'price-asc') {
939
+ sortedProducts.sort((a, b) => parseFloat(a.price) - parseFloat(b.price));
940
+ }
923
941
  // 应用分页
924
942
  const page = options?.page || 1;
925
943
  const limit = options?.limit || 10;
926
944
  const offset = (page - 1) * limit;
927
- const paginatedProductIds = categoryData.productIds.slice(offset, offset + limit);
928
- // 获取产品详情(使用上级平台数据)
929
- const products = await Promise.all(paginatedProductIds.map(productId => this.getProduct_p(productId)));
930
- // 应用状态过滤
931
- let filteredProducts = products;
945
+ const paginatedProducts = sortedProducts.slice(offset, offset + limit);
946
+ // 应用状态过滤(如果有status字段)
947
+ let filteredProducts = paginatedProducts;
932
948
  if (options?.status !== undefined) {
933
- filteredProducts = products.filter(product => product.status === options.status);
949
+ filteredProducts = paginatedProducts.filter(product => product.status === options.status);
934
950
  }
935
951
  return {
936
952
  products: filteredProducts,
937
- total: categoryData.productIds.length,
953
+ total: categoryData.productInfos.length,
938
954
  page,
939
955
  limit
940
956
  };