Sfoglia il codice sorgente

调整c端获取栏目路由的接口;修改获取栏目的接口;

15313670163 5 giorni fa
parent
commit
8a6441cfd4
2 ha cambiato i file con 86 aggiunte e 45 eliminazioni
  1. 39 20
      app/JsonRpc/WebsiteService.php
  2. 47 25
      app/Tools/Result.php

+ 39 - 20
app/JsonRpc/WebsiteService.php

@@ -43,7 +43,7 @@ use App\Model\Sector;
 use PhpParser\Node\Stmt\Return_;
 use function PHPUnit\Framework\isNull;
 use Overtrue\Pinyin\Pinyin;
-
+use App\Tools\buildTree;
 #[RpcService(name: "WebsiteService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
 class WebsiteService implements WebsiteServiceInterface
 {
@@ -622,11 +622,11 @@ class WebsiteService implements WebsiteServiceInterface
         // 以下注释掉的代码是之前的逻辑,用于获取非顶级分类的 pid
         $pidQuery = WebsiteCategory::where($website_id)
             ->where('pid', '!=', 0)
+            ->whereRaw('JSON_CONTAINS(category_arr_id,?)', [$data['pid']])
             ->orderBy('sort')
-            ->select('pid', 'category_id', 'alias');
+            ->select('*');
         $pid = $pidQuery->pluck('pid');
         $pid = array_values(array_unique($pid->toArray()));
-
         // 构建查询语句
         $query = WebsiteCategory::where($website_id)
             ->where('pid', $data['pid'])
@@ -641,21 +641,32 @@ class WebsiteService implements WebsiteServiceInterface
             // 如果 $pid 数组为空,不添加 CASE WHEN 条件,添加字段 children_count 并赋值为 0
             $query->select('website_category.*', DB::raw('0 as children_count'));
         }
-
-        // 执行查询
-        $placeid = $data['placeid'] - 1;
+        // $child = Result::buildMenuTree($child);
+        $placeid = $data['placeid']-1;
         $result = $query->offset($placeid)->limit($data['num'])->get();
-
         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;
-                }
-            });
+            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{
+                $result = $result->map(function ($item) use ($pidResults) {
+                    $children = $pidResults->where('pid', $item->category_id)->map(function ($child) {
+                        if(!empty($child)){
+                            return $child;
+                        }
+                    });
+                    // 重置索引,使 key 值从 0 开始
+                    $item->children = $children->values();
+                    return $item;
+                });
+            }
             return Result::success($result);
         } else {
             return Result::error("本网站暂无栏目", 0);
@@ -2274,11 +2285,19 @@ class WebsiteService implements WebsiteServiceInterface
             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'])->first(['category_id', 'alias']);
-        }
-        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']);
+        if (isset($data['pinyin']) &&!empty($data['pinyin'])) {
+        $result = WebsiteCategory::where('website_id', $data['website_id'])
+                    ->where('aLIas_pinyin', $data['pinyin'])
+                    ->selectRaw('website_category.*, 
+                        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(['category_id', 'alias', 'children_count']);
+                }
+        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']);
         }
         if (!isset($result) || empty($result)) {
             return Result::error("暂无该导航", 0);

+ 47 - 25
app/Tools/Result.php

@@ -1,26 +1,48 @@
-<?php
-namespace App\Tools;
-use App\Constants\ErrorCode;
-class Result
-{
-    public static function success($data = [])
-    {
-        return static::result(ErrorCode::SUCCESS, ErrorCode::getMessage(ErrorCode::SUCCESS), $data);
-    }
-    public static function error($message = '', $code = ErrorCode::ERROR, $data = [])
-    {
-        if (empty($message)) {
-            return static::result($code, ErrorCode::getMessage($code), $data);
-        } else {
-            return static::result($code, $message, $data);
-        }
-    }
-    protected static function result($code, $message, $data)
-    {
-        return [
-            'code' => $code,
-            'message' => $message,
-            'data' => $data,
-        ];
-    }
+<?php
+namespace App\Tools;
+use App\Constants\ErrorCode;
+class Result
+{
+    public static function success($data = [])
+    {
+        return static::result(ErrorCode::SUCCESS, ErrorCode::getMessage(ErrorCode::SUCCESS), $data);
+    }
+    public static function error($message = '', $code = ErrorCode::ERROR, $data = [])
+    {
+        if (empty($message)) {
+            return static::result($code, ErrorCode::getMessage($code), $data);
+        } else {
+            return static::result($code, $message, $data);
+        }
+    }
+    protected static function result($code, $message, $data)
+    {
+        return [
+            'code' => $code,
+            'message' => $message,
+            'data' => $data,
+        ];
+    }
+    /**
+     * 递归查询
+     * @param $menuItems
+     * @param $parentId
+     * @return array
+     */
+    public static  function buildMenuTree($menuItems, $parentId = 0) {
+        $tree = [];
+        foreach ($menuItems as $item) {
+            if ($item['pid'] == $parentId) {
+                // 找到子菜单
+                $children = self::buildMenuTree($menuItems, $item['category_id']);
+                // 如果子菜单存在,则添加到当前菜单的children中
+                if ($children) {
+                    $item['children'] = $children;
+                }
+                // 将当前菜单添加到树中
+                $tree[] = $item;
+            }
+        }
+        return $tree;
+    }
 }