Преглед изворни кода

Merge branch 'web_sannong_fr'

15313670163 пре 3 недеља
родитељ
комит
7b40f5d37a
1 измењених фајлова са 32 додато и 13 уклоњено
  1. 32 13
      app/JsonRpc/WebsiteService.php

+ 32 - 13
app/JsonRpc/WebsiteService.php

@@ -1546,10 +1546,11 @@ class WebsiteService implements WebsiteServiceInterface
             if (empty($category)) {
                 return Result::error("暂无此导航", 0);
             }
-            $category['children_count'] = WebsiteCategory::where('website_id', $data['website_id'])->where('pid', $data['category_id'])->count();
-            $parent = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $category['pid'])->select('category_id as parent_id', 'alias as parent_name')->first();
-            $category['parent_id'] = $parent['parent_id'] ?? '';
-            $category['parent_name'] = $parent['parent_name'] ?? '';
+            $category['children_count'] = WebsiteCategory::where('website_id',$data['website_id'])->where('pid',$data['category_id'])->count();
+            $parent = WebsiteCategory::where('website_id',$data['website_id'])->where('category_id',$category['pid'])->select('category_id as parent_id','alias as parent_name','aLIas_pinyin')->first();
+            $category['parent_id'] = $parent['parent_id']?? '';
+            $category['parent_pinyin'] = $parent['aLIas_pinyin']?? '';
+            $category['parent_name'] = $parent['parent_name']?? '';
             return Result::success($category);
         } else {
             return Result::error("参数错误", 0);
@@ -2100,23 +2101,41 @@ class WebsiteService implements WebsiteServiceInterface
             return Result::error("暂无该网站", 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";
+                ->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();
+                ->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);