Переглянути джерело

修改获取栏目及其一个自己栏目

15313670163 3 тижнів тому
батько
коміт
1ee2d05ec0
1 змінених файлів з 19 додано та 7 видалено
  1. 19 7
      app/JsonRpc/WebsiteService.php

+ 19 - 7
app/JsonRpc/WebsiteService.php

@@ -594,9 +594,11 @@ class WebsiteService implements WebsiteServiceInterface
         // 初始化 $pid 数组
         // $pid = [];
         // 以下注释掉的代码是之前的逻辑,用于获取非顶级分类的 pid
-        $pid = WebsiteCategory::where($website_id)
+        $pidQuery = WebsiteCategory::where($website_id)
             ->where('pid', '!=', 0)
-            ->pluck('pid');
+            ->orderBy('sort')
+            ->select('pid','category_id','alias');
+        $pid = $pidQuery->pluck('pid');
         $pid = array_values(array_unique($pid->toArray()));
 
         // 构建查询语句
@@ -617,11 +619,21 @@ class WebsiteService implements WebsiteServiceInterface
         // 执行查询
         $placeid = $data['placeid']-1;
         $result = $query->offset($placeid)->limit($data['num'])->get();
-        if(!empty($result)){ 
-            return Result::success($result);  
-        }else{ 
-            return Result::error("本网站暂无栏目",0);  
-        } 
+
+        if (!empty($result)) {
+            $pidResults = $pidQuery->get();
+            $pidMap = $pidResults->keyBy('pid');
+            $result->each(function ($record) use ($pidMap) {
+                if ($pidMap->has($record->category_id)) {
+                    $pidResult = $pidMap->get($record->category_id);
+                    $record->chilid_id = $pidResult->category_id;
+                    $record->chilid_alias = $pidResult->alias;
+                }
+            });
+            return Result::success($result);
+        } else {
+            return Result::error("本网站暂无栏目", 0);
+        }
     } 
 
     /**