黄宗昌 2 年之前
父节点
当前提交
0fc35d5d76
共有 2 个文件被更改,包括 29 次插入8 次删除
  1. 29 7
      app/Helper/function.php
  2. 0 1
      app/Http/Controllers/V1/ProductController.php

+ 29 - 7
app/Helper/function.php

@@ -336,18 +336,40 @@ if (!function_exists('valid_url')) {
 if (!function_exists('pages')) {
 if (!function_exists('pages')) {
 
 
     function pages($list,$go){
     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);
+        $num_elements = intval(count($list->items()) / $go); //每个二维数组包含几个元素
+        $items = array_chunk($list->items(), $num_elements>0?$num_elements:1);
+        $arr1 = [];
+        $arrList = [];
+        foreach ($items as $k=>$v){
+            if($k+1 >= $go){
+                $arr1[] = $v;
+            }else{
+                $arrList[] = $v;
+            }
+        }
+        $arr3 = [];
+        if(!empty($arr1)){
+            foreach ($arr1 as $v){
+                if(!empty($v)){
+                    foreach ($v as $v1){
+                        $arr3[] = $v1;
+                    }
+                }
+            }
+        }
+        if(!empty($arr3)){
+            $arrList = array_merge($arrList,[$arr3]);
+        }
+        if($go < count($arrList)){
+            $arr = [];
+            $num = $go - count($items);
             for ($i=0;$i< $num;$i++){
             for ($i=0;$i< $num;$i++){
                 $arr[] = [];
                 $arr[] = [];
             }
             }
+            $arrList = array_merge($arrList,$arr);
         }
         }
-        $items = array_merge($items,$arr);
         return [
         return [
-            'items' => $items,
+            'items' => $arrList,
             'total' => $list->total(),   // 总数
             'total' => $list->total(),   // 总数
             'pageSize' => $list->perPage(), // 每页数据
             'pageSize' => $list->perPage(), // 每页数据
             'totalPage' => $list->lastPage(),// 总页数
             'totalPage' => $list->lastPage(),// 总页数

+ 0 - 1
app/Http/Controllers/V1/ProductController.php

@@ -183,7 +183,6 @@ class ProductController extends Controller
         }
         }
         $list = $query->where('status',1)
         $list = $query->where('status',1)
             ->paginate($limit);
             ->paginate($limit);
-
         return $this->success(pages($list,$go));
         return $this->success(pages($list,$go));
     }
     }