黄宗昌 2 lat temu
rodzic
commit
1f7a9e60db

+ 10 - 11
app/Helper/function.php

xqd
@@ -335,20 +335,19 @@ if (!function_exists('valid_url')) {
  */
 if (!function_exists('pages')) {
 
-    function pages($list,$num){
-        $arr1 = array_slice(array_chunk($list->items(),$num),0,count(array_chunk($list->items(),$num))-2);
-        $arr2 = array_slice(array_chunk($list->items(),$num),-2);
-        $arr3 = [];
-        if(!empty($arr2)){
-            foreach ($arr2 as $v){
-                foreach ($v as $v1){
-                    $arr3[] = $v1;
-                }
+    function pages($list,$go){
+        $num_elements = ceil(count($list->items()) / $go); //每个二维数组包含几个元素
+        $items = array_chunk($list->items(), $num_elements);
+        $arr = [];
+        if($go > count($items)){
+            $num = $go-count($items);
+            for ($i=0;$i< $num;$i++){
+                $arr[] = [];
             }
         }
-        $arr = array_merge($arr1,[$arr3]);
+        $items = array_merge($items,$arr);
         return [
-            'items' => $arr,
+            'items' => $items,
             'total' => $list->total(),   // 总数
             'pageSize' => $list->perPage(), // 每页数据
             'totalPage' => $list->lastPage(),// 总页数

+ 2 - 2
app/Http/Controllers/V1/ProductController.php

xqd
@@ -183,8 +183,8 @@ class ProductController extends Controller
         }
         $list = $query->where('status',1)
             ->paginate($limit);
-        $num = intval($limit / $go);
-        return $this->success(pages($list,$num));
+
+        return $this->success(pages($list,$go));
     }
 
     /**