rkljw 3 veckor sedan
förälder
incheckning
f591c3db28
1 ändrade filer med 72 tillägg och 4 borttagningar
  1. 72 4
      app/JsonRpc/NewsService.php

+ 72 - 4
app/JsonRpc/NewsService.php

@@ -19,7 +19,11 @@ use Hyperf\RpcServer\Annotation\RpcService;
 use App\Tools\Result;
 use Ramsey\Uuid\Uuid;
 use Hyperf\Utils\Random;
+<<<<<<< Updated upstream
 
+=======
+use Hyperf\Utils\Collection;
+>>>>>>> Stashed changes
 #[RpcService(name: "NewsService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
 class NewsService implements NewsServiceInterface
 {
@@ -31,12 +35,76 @@ class NewsService implements NewsServiceInterface
      */
     public function getCategoryList(array $data): array
     {
-        $rep = Category::select("category.*")->orderBy('category.updated_at',"desc")->get();
-        if (empty($rep)) {
-            return Result::error("没有导航池数据");
+
+        $page = (int)$data['page']??1;
+        $perPage = (int)$data['pageSize']??10;
+        $search =$data['name']??'';
+        // 查找所有匹配的分类
+        $matchedCategories = $this->findMatchedCategories($search);
+        // 查找所有匹配分类的父级分类
+        $allRequiredCategories = $this->findAllParents($matchedCategories);
+        // 提取一级分类
+        $topLevelCategories = [];
+        foreach ($allRequiredCategories as $category) {
+            if ($category->pid === 0) {
+                $topLevelCategories[] = $category;
+            }
+        }
+        // 分页处理
+        $offset = ($page - 1) * $perPage;
+        $paginatedTopLevelCategories = array_slice($topLevelCategories, $offset, $perPage);
+        $categoryTree = [];
+        foreach ($paginatedTopLevelCategories as $category) {
+            $categoryTree[] = $this->buildCategoryTree($category, $allRequiredCategories);
+        }
+        $return = [
+            'rows' => $categoryTree,
+            'total' => count($topLevelCategories),
+        ];
+        return Result::success($return);
+    }
+    private function findMatchedCategories($search)
+    {
+        if ($search) {
+            return Category::where('name', 'like', "%{$search}%")->get();
+        }
+        return Category::all();
+    }
+
+    private function findAllParents($categories)
+    {
+        $allCategories = [];
+        foreach ($categories as $category) {
+            $parentId = $category->pid;
+            while ($parentId > 0) {
+                $parent = Category::find($parentId);
+                if ($parent) {
+                    $allCategories[$parent->id] = $parent;
+                    $parentId = $parent->pid;
+                } else {
+                    break;
+                }
+            }
+            $allCategories[$category->id] = $category;
         }
-        return Result::success($rep);
+        return array_values($allCategories);
     }
+
+    private function buildCategoryTree($category, $allRequiredCategories)
+    {
+        $categoryData = $category->toArray();
+        $children = [];
+        foreach ($allRequiredCategories as $c) {
+            if ($c->pid === $category->id) {
+                $children[] = $this->buildCategoryTree($c, $allRequiredCategories);
+            }
+        }
+        if (!empty($children)) {
+            $categoryData['children'] = $children;
+        }
+        return $categoryData;
+    }
+
     public function myCategoryList(array $data): array
     {
         $sszq = $data['sszq'] ?? '';