Ver código fonte

根据路由修改获取父级/子级栏目

15313670163 3 semanas atrás
pai
commit
42f8cd7fc0
1 arquivos alterados com 40 adições e 22 exclusões
  1. 40 22
      app/JsonRpc/WebsiteService.php

+ 40 - 22
app/JsonRpc/WebsiteService.php

@@ -2038,40 +2038,58 @@ class WebsiteService implements WebsiteServiceInterface
         return Result::success($result);
     }
 
-     /*
+    /*
      * 获取父级/子级栏目
      * @param array $data
      * @return array
      * */
     public function getWebsiteParentCategory(array $data): array
     {
-        if(isset($data['website_id']) &&!empty($data['website_id'])){
-            $website = Website::where('id',$data['website_id'])->where('status',1)->first(); 
-        } 
+        if (isset($data['website_id']) && !empty($data['website_id'])) {
+            $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
+        }
         if (empty($website)) {
-            return Result::error("暂无该网站",0); 
+            return Result::error("暂无该网站", 0);
         }
-        if($data['id']==0){
+        if ($data['id'] == 0) {
+            // 在 Hyperf 中,查询构建器(Builder)没有 `map` 方法,`map` 方法是集合(Collection)的方法。
+            // 因此,需要先调用 `get()` 方法获取查询结果集合,再使用 `map` 方法进行处理。
             $category = WebsiteCategory::where('website_id', $data['website_id'])
-            ->where('pid', 0)
-            ->pluck('aLIas_pinyin')
-            ->map(function ($alias) {
-                return "/{$alias}/:id";
-            })
-            ->values()
-            ->all();
-        }else{
+                ->where('pid', 0)
+                ->select('alias_pinyin', 'category_id', 'alias')
+                ->get() // 添加 get() 方法获取结果集合
+                ->map(function ($item) {
+                    return [
+                        'path' => "/{$item->alias_pinyin}/",
+                        'name' => $item->alias,
+                        'cid' => $item->category_id
+                    ];
+                })
+                ->values()
+                ->all();
+        } else {
+            // 不使用 with 方法,直接查询父级栏目需要的字段
             $category = WebsiteCategory::where('website_id', $data['website_id'])
-            ->where('pid','!=', 0)
-            ->pluck('aLIas_pinyin')
-            ->map(function ($alias) {
-                return "/{$alias}/:id";
-            })
-            ->values()
-            ->all();
+                ->where('pid', '!=', 0)
+                ->get();
+            $category = $category->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 : '';
+                return [
+                    'path' => "/{$parentAlias}/{$alias_pinyin}/",
+                    'name' => $alias ? "{$alias}" : null,
+                    'cid' => $category_id ? "{$category_id}" : null
+                ];
+            })->values()->all();
         }
         if (empty($category)) {
-            return Result::error("暂无此导航",0);
+            return Result::error("暂无此导航", 0);
         }
         // $result = [];
         return Result::success($category);