Просмотр исходного кода

c端:获取导航栏的接口

15313670163 4 месяцев назад
Родитель
Сommit
becd734b63
1 измененных файлов с 51 добавлено и 43 удалено
  1. 51 43
      app/JsonRpc/WebsiteService.php

+ 51 - 43
app/JsonRpc/WebsiteService.php

@@ -622,43 +622,52 @@ class WebsiteService implements WebsiteServiceInterface
 
      public function getWebsiteModelCategory(array $data): array
      {
-         // return Result::success($data);
-         $website_id = [
-             'website_id' => $data['website_id'],
-         ];
-         // 初始化 $pid 数组
-         // $pid = [];
-         // 以下注释掉的代码是之前的逻辑,用于获取非顶级分类的 pid
-         $pidQuery = WebsiteCategory::where($website_id)
-             ->where('pid', '!=', 0)
-             ->orderBy('sort')
-             ->select('pid', 'category_id', 'alias');
-         $pid = $pidQuery->pluck('pid');
-         $pid = array_values(array_unique($pid->toArray()));
- 
-         // 构建查询语句
-         $query = WebsiteCategory::where($website_id)
-             ->where('pid', $data['pid'])
-             ->offset($data['placeid'])
-             ->limit($data['num'])
-             ->orderBy('sort');
-         // 如果 $pid 数组不为空,添加 CASE WHEN 条件
-         if (!empty($pid)) {
-             $placeholders = implode(',', array_fill(0, count($pid), '?'));
-             $query->selectRaw("website_category.*, CASE WHEN website_category.category_id IN ($placeholders) THEN 1 ELSE 0 END AS children_count", $pid);
-         } else {
-             // 如果 $pid 数组为空,不添加 CASE WHEN 条件,添加字段 children_count 并赋值为 0
-             $query->select('website_category.*', DB::raw('0 as children_count'));
-         }
- 
-         // 执行查询
-         $placeid = $data['placeid'] - 1;
-         $result = $query->offset($placeid)->limit($data['num'])->get();
- 
-         if (!empty($result)) {
-             $pidResults = $pidQuery->get();
-             if(isset($data['type']) && $data['type'] == 1){
-                $result = $result->map(function ($item) use ($pidResults) {
+        // return Result::success($data);
+        $website_id = [
+            'website_id' => $data['website_id'],
+        ];
+        // 初始化 $pid 数组
+        $pid = [];
+        // 以下注释掉的代码是之前的逻辑,用于获取非顶级分类的 pid
+        $pidQuery = WebsiteCategory::where($website_id)
+            ->where('pid', '!=', 0)
+            ->orderBy('sort')
+            ->select('pid', 'category_id', 'alias');
+        $pid = $pidQuery->pluck('pid');
+        $pid = array_values(array_unique($pid->toArray()));
+
+        // 构建查询语句
+        $query = WebsiteCategory::where($website_id)
+            ->when(isset($data['is_show']) && $data['is_show'] == 1, function ($query) use ($data) {
+                $query->where('is_show', $data['is_show']);
+            })
+            ->when(!isset($data['is_show']) || $data['is_show'] != 1, function ($query) use ($data) {
+                $query->where('pid', $data['pid']);
+            })
+            ->offset($data['placeid'])
+            ->limit($data['num'])
+            ->orderBy('sort');
+        // 如果 $pid 数组不为空,添加 CASE WHEN 条件
+        
+        if (!empty($pid)) {
+            $placeholders = implode(',', array_fill(0, count($pid), '?'));
+            $query->selectRaw("website_category.*, CASE WHEN website_category.category_id IN ($placeholders) THEN 1 ELSE 0 END AS children_count", $pid);
+        } else {
+            // 如果 $pid 数组为空,不添加 CASE WHEN 条件,添加字段 children_count 并赋值为 0
+            $query->select('website_category.*', DB::raw('0 as children_count'));
+        }
+
+        // 执行查询
+        $placeid = $data['placeid'] - 1;
+        $result = $query->offset($placeid)->limit($data['num'])->get();
+        
+        if (!empty($result)) {
+            $pidResults = $pidQuery->get();
+            if(isset($data['type']) && $data['type'] == 1){
+                $result = $result->map(function ($item) use ($pidResults,$data) {
+                    if(isset($data['is_show']) && $data['is_show'] == 1){
+                        $item->aLIas_pinyin = $item->path;
+                    }
                     $children = $pidResults->where('pid', $item->category_id)->map(function ($child) {
                         if(!empty($child)){
                             return $child;
@@ -668,7 +677,7 @@ class WebsiteService implements WebsiteServiceInterface
                     $item->children = $children->values();
                     return $item;
                 });
-             }else{
+            }else{
                 $pidMap = $pidResults->keyBy('pid');
                 $result->each(function ($record) use ($pidMap) {
                     if ($pidMap->has($record->category_id)) {
@@ -677,12 +686,11 @@ class WebsiteService implements WebsiteServiceInterface
                         $record->chilid_alias = $pidResult->alias;
                     }
                 });
-             }
-             
-             return Result::success($result);
-         } else {
+            }
+            return Result::success($result);
+        } else {
              return Result::error("本网站暂无栏目", 0);
-         }
+        }
      }