15313670163 пре 2 недеља
родитељ
комит
ad2a304d39
1 измењених фајлова са 38 додато и 37 уклоњено
  1. 38 37
      app/JsonRpc/WebsiteService.php

+ 38 - 37
app/JsonRpc/WebsiteService.php

@@ -2045,7 +2045,7 @@ class WebsiteService implements WebsiteServiceInterface
         return Result::success($result);
     }
 
-    /*
+        /*
      * 获取父级/子级栏目------路由
      * @param array $data
      * @return array
@@ -2059,45 +2059,46 @@ class WebsiteService implements WebsiteServiceInterface
         if (empty($website)) {
             return Result::error("暂无该网站", 0);
         }
-        // if ($data['id'] == 0) {
-            // 在 Hyperf 中,查询构建器(Builder)没有 `map` 方法,`map` 方法是集合(Collection)的方法。
-            // 因此,需要先调用 `get()` 方法获取查询结果集合,再使用 `map` 方法进行处理。
-            $category['parent'] = WebsiteCategory::where('website_id', $data['website_id'])
-                ->where('pid', 0)
-                ->selectRaw("category_id as cid, alias as name, CONCAT('/', '', aLIas_pinyin, '/') as path")
-                ->get() // 添加 get() 方法获取结果集合
-                ->all();
-        // } else if($data['id'] == 1){
-            // 不使用 with 方法,直接查询父级栏目需要的字段
-            $child = WebsiteCategory::where('website_id', $data['website_id'])
-                ->where('pid', '!=', 0)
-                ->get();
-            $category['child'] = $child->map(function ($item) {
-                $alias = $item->alias;
-                $category_id = $item->category_id;
-                $alias_pinyin = $item->aLIas_pinyin;
-                // 根据 pid 查找父级栏目
-                $parent = WebsiteCategory::where('website_id', $item->website_id)
-                    ->where('category_id', $item->pid)
-                    ->first();
-                $parentAlias = $parent ? $parent->aLIas_pinyin : '';
-                $result = [
-                    'name' => $alias,
-                    'cid' => $category_id
-                ];
-                if (!empty($alias_pinyin) && !empty($parentAlias)) {
-                    $result['path'] = "/{$parentAlias}/{$alias_pinyin}/";
-                }
-                return $result;
-            })->values()->all();
-        // }else if($data['id'] == 2){
-        // }else{
-        //     return Result::error("请输入正确的id", 0);
-        // }
+        // 在 Hyperf 中,查询构建器(Builder)没有 `map` 方法,`map` 方法是集合(Collection)的方法。
+        // 因此,需要先调用 `get()` 方法获取查询结果集合,再使用 `map` 方法进行处理。
+        $category['parent'] = WebsiteCategory::where('website_id', $data['website_id'])
+            ->where('pid', 0)
+            ->selectRaw("category_id as cid, alias as name, CONCAT('/', '', aLIas_pinyin, '/') as path")
+            ->get() // 添加 get() 方法获取结果集合
+            ->all();
+        // 提前查询所有父级栏目,避免在 map 中多次查询
+        // 提前查询所有父级栏目信息并以 category_id 作为键存储
+        $parentCategories = WebsiteCategory::where('website_id', $data['website_id'])
+            ->where('pid', 0)
+            ->select('category_id', 'aLIas_pinyin')
+            ->get()
+            ->keyBy('category_id');
+
+        // 直接查询子栏目信息
+        $child = WebsiteCategory::where('website_id', $data['website_id'])
+            ->where('pid', '!=', 0)
+            ->select('alias', 'category_id', 'aLIas_pinyin', 'pid')
+            ->get();
+
+        // 使用集合的 map 方法处理子栏目信息
+        $category['child'] = $child->map(function ($item) use ($parentCategories) {
+            $alias = $item->alias;
+            $category_id = $item->category_id;
+            $alias_pinyin = $item->aLIas_pinyin;
+            $parent = $parentCategories->get($item->pid);
+            $parentAlias = $parent ? $parent->aLIas_pinyin : '';
+            $result = [
+                'name' => $alias,
+                'cid' => $category_id
+            ];
+            if (!empty($alias_pinyin) && !empty($parentAlias)) {
+                $result['path'] = "/{$parentAlias}/{$alias_pinyin}/";
+            }
+            return $result;
+        })->values()->all();
         if (empty($category)) {
             return Result::error("暂无此导航", 0);
         }
-        // $result = [];
         return Result::success($category);
     }
     /**