Jelajahi Sumber

路由匹配-获取它是否拥有子级栏目;获取栏目-传递type参数则返回其二级、三级的栏目

15313670163 5 hari lalu
induk
melakukan
7e46567a7b
1 mengubah file dengan 65 tambahan dan 63 penghapusan
  1. 65 63
      app/JsonRpc/WebsiteService.php

+ 65 - 63
app/JsonRpc/WebsiteService.php

@@ -605,57 +605,50 @@ class WebsiteService implements WebsiteServiceInterface
         }
     }
 
-    /**
+     /**
      * 获取栏目
      * @param array $data
      * @return array
      */
 
-    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)
-            ->whereRaw('JSON_CONTAINS(category_arr_id,?)', [$data['pid']])
-            ->orderBy('sort')
-            ->select('*');
-        $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'));
-        }
-        // $child = Result::buildMenuTree($child);
-        $placeid = $data['placeid']-1;
-        $result = $query->offset($placeid)->limit($data['num'])->get();
-        if (!empty($result)) {
-            $pidResults = $pidQuery->get();
-            if(!isset($data['type']) || empty($data['type'])){
-                $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;
-                    }
-                });
-            }else{
+     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) {
                     $children = $pidResults->where('pid', $item->category_id)->map(function ($child) {
                         if(!empty($child)){
@@ -666,12 +659,23 @@ class WebsiteService implements WebsiteServiceInterface
                     $item->children = $children->values();
                     return $item;
                 });
-            }
-            return Result::success($result);
-        } else {
-            return Result::error("本网站暂无栏目", 0);
-        }
-    }
+             }else{
+                $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);
+         }
+     }
+ 
 
     /**
      * 获取友情链接
@@ -2284,18 +2288,16 @@ class WebsiteService implements WebsiteServiceInterface
         if (empty($website)) {
             return Result::error("暂无该网站", 0);
         }
-        // return Result::success($data);
         if (isset($data['pinyin']) &&!empty($data['pinyin'])) {
-        $result = WebsiteCategory::where('website_id', $data['website_id'])
-                    ->where('aLIas_pinyin', $data['pinyin'])
-                    ->selectRaw('website_category.category_id, website_category.alias, 
-                        CASE 
-                            WHEN EXISTS (SELECT 1 FROM website_category as wc WHERE wc.pid = website_category.category_id) 
-                            THEN 1 
-                            ELSE 0 
-                        END as childrencount')
-                    ->first();
-                }
+            $result = WebsiteCategory::where('website_category.website_id', $data['website_id'])
+                ->where('website_category.aLIas_pinyin', $data['pinyin'])
+                ->leftJoin('website_category as pc', function ($join) use ($data) {
+                    $join->on('pc.pid', '=', 'website_category.category_id')
+                        ->where('pc.website_id', '=', $data['website_id']);
+                })
+                ->select('website_category.category_id', 'website_category.aLIas_pinyin', DB::raw('CASE WHEN pc.category_id IS NOT NULL THEN 1 ELSE 0 END as children_count'))
+                ->first();
+        }
         if(isset($data['foot_pinyin']) &&!empty($data['foot_pinyin'])){
             $result = FooterCategory::where('website_id',$data['website_id'])->where('name_pinyin',$data['foot_pinyin'])->first(['id']);
         }