فهرست منبع

Merge branch 'dev' of http://gogs.hnwmzp.cn/chengwenliang/nshop into dev

chengwl 1 هفته پیش
والد
کامیت
191ad218fd

+ 10 - 0
packages/Webkul/BagistoApi/src/Dto/CategoryProducts/CategoryProductsPageInfoDto.php

@@ -21,4 +21,14 @@ class CategoryProductsPageInfoDto
      * Whether another page exists after the current one.
      */
     public bool $has_next_page = false;
+
+    /**
+     * Current page number (1-based). Available when using the `page` arg.
+     */
+    public int $current_page = 1;
+
+    /**
+     * Total number of pages. Available when using the `page` arg.
+     */
+    public int $total_pages = 1;
 }

+ 1 - 0
packages/Webkul/BagistoApi/src/Models/CategoryProducts.php

@@ -36,6 +36,7 @@ use Webkul\BagistoApi\Resolver\CategoryProductsResolver;
                 'last'    => ['type' => 'Int'],
                 'after'   => ['type' => 'String'],
                 'before'  => ['type' => 'String'],
+                'page'    => ['type' => 'Int', 'description' => '1-based page number for direct page navigation. Overrides after/before.'],
                 'locale'  => ['type' => 'String'],
                 'channel' => ['type' => 'String'],
             ],

+ 25 - 5
packages/Webkul/BagistoApi/src/Resolver/CategoryProductsResolver.php

@@ -52,6 +52,20 @@ class CategoryProductsResolver implements QueryItemResolverInterface
             $productArgs['reverse'] = false;
         }
 
+        // The product provider only understands Relay cursor args (after/before),
+        // so translate a direct `page` arg into an equivalent `after` cursor.
+        if (! empty($productArgs['page'])) {
+            $page = max(1, (int) $productArgs['page']);
+            $limit = max(1, (int) ($productArgs['first'] ?? $productArgs['last'] ?? 30));
+            $offset = ($page - 1) * $limit;
+
+            if ($offset > 0) {
+                $productArgs['after'] = base64_encode((string) ($offset - 1));
+            }
+
+            unset($productArgs['page']);
+        }
+
         $paginator = $this->productProvider->provide(
             $context['operation'],
             [],
@@ -85,24 +99,30 @@ class CategoryProductsResolver implements QueryItemResolverInterface
         $count = count($result->products);
         $result->page_info->has_next_page = ($offset + $count) < $result->total_count;
         $result->page_info->end_cursor = $count > 0 ? $result->products[$count - 1]->cursor : null;
+        $result->page_info->current_page = $result->total_count > 0 ? (int) floor($offset / $limit) + 1 : 1;
+        $result->page_info->total_pages = $result->total_count > 0 ? (int) ceil($result->total_count / $limit) : 1;
 
         return $result;
     }
 
     /**
-     * Resolve the current page offset from Relay cursor args.
+     * Resolve the current page offset.
+     *
+     * Supports both Relay cursor args (after/before) and a direct `page` arg.
+     * When `page` is provided it takes precedence over after/before.
      */
     private function resolveOffset(array $args, int $total): int
     {
         $limit = max(1, (int) ($args['first'] ?? $args['last'] ?? 30));
         $offset = 0;
 
-        if (! empty($args['after'])) {
+        if (! empty($args['page'])) {
+            $page = max(1, (int) $args['page']);
+            $offset = ($page - 1) * $limit;
+        } elseif (! empty($args['after'])) {
             $decoded = base64_decode($args['after'], true);
             $offset = ctype_digit((string) $decoded) ? ((int) $decoded + 1) : 0;
-        }
-
-        if (! empty($args['before'])) {
+        } elseif (! empty($args['before'])) {
             $decoded = base64_decode($args['before'], true);
             $cursor = ctype_digit((string) $decoded) ? (int) $decoded : 0;
             $offset = max(0, $cursor - $limit);