Browse Source

Merge branch 'master' of http://git.bjzxtw.org.cn:3000/zxt/public_producer

LiuJ 2 days ago
parent
commit
9a66acae0a
2 changed files with 125 additions and 82 deletions
  1. 78 57
      app/JsonRpc/WebsiteService.php
  2. 47 25
      app/Tools/Result.php

+ 78 - 57
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
 {
@@ -605,62 +605,77 @@ 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)
-            ->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();
-            $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);
-        }
-    }
+     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)){
+                            return $child;
+                        }
+                    });
+                    // 重置索引,使 key 值从 0 开始
+                    $item->children = $children->values();
+                    return $item;
+                });
+             }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);
+         }
+     }
+ 
 
     /**
      * 获取友情链接
@@ -2277,12 +2292,18 @@ 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'])->first(['category_id', 'alias']);
+        if (isset($data['pinyin']) &&!empty($data['pinyin'])) {
+            $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', 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']);
+        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;
+    }
 }