浏览代码

Merge branch '20250210_lj_zhanqun'

AI 1 月之前
父节点
当前提交
b47d14db61
共有 1 个文件被更改,包括 91 次插入74 次删除
  1. 91 74
      app/JsonRpc/NewsService.php

+ 91 - 74
app/JsonRpc/NewsService.php

@@ -19,6 +19,8 @@ use Hyperf\RpcServer\Annotation\RpcService;
 use App\Tools\Result;
 use App\Tools\Result;
 use Ramsey\Uuid\Uuid;
 use Ramsey\Uuid\Uuid;
 use Hyperf\Utils\Random;
 use Hyperf\Utils\Random;
+use Fukuball\Jieba\Jieba;
+use Fukuball\Jieba\Finalseg;
 
 
 #[RpcService(name: "NewsService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
 #[RpcService(name: "NewsService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
 class NewsService implements NewsServiceInterface
 class NewsService implements NewsServiceInterface
@@ -31,7 +33,7 @@ class NewsService implements NewsServiceInterface
      */
      */
     public function getCategoryList(array $data): array
     public function getCategoryList(array $data): array
     {
     {
-        $rep = Category::select("category.*")->orderBy('category.updated_at',"desc")->get();
+        $rep = Category::select("category.*")->orderBy('category.updated_at', "desc")->get();
         if (empty($rep)) {
         if (empty($rep)) {
             return Result::error("没有导航池数据");
             return Result::error("没有导航池数据");
         }
         }
@@ -275,7 +277,14 @@ class NewsService implements NewsServiceInterface
             var_dump($articleData['level'], '----------$articleData[level]----------1');
             var_dump($articleData['level'], '----------$articleData[level]----------1');
             if ($articleData['keyword'] == '') {
             if ($articleData['keyword'] == '') {
                 //提取标题+内容中的关键词
                 //提取标题+内容中的关键词
-                $articleData['keyword'] = $data['title'] . substr(str_replace(' ', '', strip_tags($data['content'])), 0, 20);
+                $articleData['keyword'] = $data['title'];
+                //  . substr(str_replace(' ', '', strip_tags($data['content'])), 0, 20);
+                Jieba::init(); // 初始化 jieba-php
+                Finalseg::init();
+                $segList = Jieba::cut($articleData['keyword']);
+                $segList1 = array_slice($segList, 0, 8);
+                $articleData['keyword'] = implode(',', $segList1);
+
             }
             }
             if ($articleData['introduce'] == '') {
             if ($articleData['introduce'] == '') {
                 //提取内容中的描述
                 //提取内容中的描述
@@ -398,8 +407,8 @@ class NewsService implements NewsServiceInterface
                 $data['status'] = 0;
                 $data['status'] = 0;
             }
             }
 
 
-            $data['cat_arr_id'] = isset($data['cat_arr_id']) ? json_encode($data['cat_arr_id']) : '';
-            $data['tag'] = isset($data['tag']) ? json_encode($data['tag']) : '';
+            $data['cat_arr_id'] = is_array($data['cat_arr_id']) ? json_encode($data['cat_arr_id']) : '';
+            $data['tag'] = is_array($data['tag']) ? json_encode($data['tag']) : '';
             $articleData = $data;
             $articleData = $data;
             unset($articleData['content']);
             unset($articleData['content']);
             unset($articleData['status_name']);
             unset($articleData['status_name']);
@@ -430,7 +439,13 @@ class NewsService implements NewsServiceInterface
             var_dump($articleData['level'], '----------$articleData[level]----------1');
             var_dump($articleData['level'], '----------$articleData[level]----------1');
             if ($articleData['keyword'] == '') {
             if ($articleData['keyword'] == '') {
                 //提取标题+内容中的关键词
                 //提取标题+内容中的关键词
-                $articleData['keyword'] = $data['title'] . substr(str_replace(' ', '', strip_tags($data['content'])), 0, 20);
+                $articleData['keyword'] = $data['title'];
+                //  . substr(str_replace(' ', '', strip_tags($data['content'])), 0, 20);
+                Jieba::init(); // 初始化 jieba-php
+                Finalseg::init();
+                $segList = Jieba::cut($articleData['keyword']);
+                $segList1 = array_slice($segList, 0, 8);
+                $articleData['keyword'] = implode(',', $segList1);
             }
             }
             if ($articleData['introduce'] == '') {
             if ($articleData['introduce'] == '') {
                 //提取内容中的描述
                 //提取内容中的描述
@@ -455,6 +470,7 @@ class NewsService implements NewsServiceInterface
                 var_dump($suveys_array, '---------------------1');
                 var_dump($suveys_array, '---------------------1');
                 var_dump($suvey_array, '---------------------2');
                 var_dump($suvey_array, '---------------------2');
                 $suvey_data = [];
                 $suvey_data = [];
+
                 if (is_array($suveys_array)) {
                 if (is_array($suveys_array)) {
                     foreach ($suveys_array as $key => $value) {
                     foreach ($suveys_array as $key => $value) {
                         if ($value == '') {
                         if ($value == '') {
@@ -482,7 +498,6 @@ class NewsService implements NewsServiceInterface
                                 'is_other' => 0,
                                 'is_other' => 0,
                                 'other_id' => 0,
                                 'other_id' => 0,
                                 'results' => 0,
                                 'results' => 0,
-
                             ];
                             ];
                         }
                         }
                         if (empty($suvey_data)) {
                         if (empty($suvey_data)) {
@@ -553,8 +568,8 @@ class NewsService implements NewsServiceInterface
      */
      */
     public function getWebsiteArticlett(array $data): array
     public function getWebsiteArticlett(array $data): array
     {
     {
-    
-       $category = WebsiteCategory::where('website_id', $data['website_id'])->pluck('category_id');
+
+        $category = WebsiteCategory::where('website_id', $data['website_id'])->pluck('category_id');
         $result = [];
         $result = [];
         if ($category) {
         if ($category) {
             $placeid = isset($data['placeid']) && !empty($data['placeid']) ? $data['placeid'] - 1 : 0;
             $placeid = isset($data['placeid']) && !empty($data['placeid']) ? $data['placeid'] - 1 : 0;
@@ -585,7 +600,7 @@ class NewsService implements NewsServiceInterface
                 ->when(!empty($data['level']), function ($query) use ($data) {
                 ->when(!empty($data['level']), function ($query) use ($data) {
                     if ($data['level'] != 4 && $data['level'] != 5) {
                     if ($data['level'] != 4 && $data['level'] != 5) {
                         $query->whereRaw("JSON_CONTAINS(level, '" . intval($data['level']) . "') = 1")
                         $query->whereRaw("JSON_CONTAINS(level, '" . intval($data['level']) . "') = 1")
-                                ->orderBy("updated_at", "desc");
+                            ->orderBy("updated_at", "desc");
                     }
                     }
                 })
                 })
                 ->offset($placeid)
                 ->offset($placeid)
@@ -617,25 +632,25 @@ class NewsService implements NewsServiceInterface
             ];
             ];
             $placeid = isset($data['placeid']) && !empty($data['placeid']) ? $data['placeid'] - 1 : 0;
             $placeid = isset($data['placeid']) && !empty($data['placeid']) ? $data['placeid'] - 1 : 0;
             // 1:文字新闻;2:轮播图;3:图文;
             // 1:文字新闻;2:轮播图;3:图文;
-            // 级别:0:未分类 
+            // 级别:0:未分类
             // 3:推荐图片
             // 3:推荐图片
             if ($data['level'] == 1) {
             if ($data['level'] == 1) {
                 $data['level'] = 0;
                 $data['level'] = 0;
-             } 
+            }
             $result = Article::where($where)
             $result = Article::where($where)
-                    ->where(function ($query) use ($data) {
-                        $query->whereRaw("JSON_CONTAINS(level, '" . intval($data['level']) . "') = 1")
-                            ->orWhereNull("level")
-                            ->orWhereRaw("level = '[]'");
-                    })
-                    ->where(function ($query) use ($data) {
-                        $query->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0")
-                            ->orWhereNull("ignore_ids");
-                    })
-                    ->orderBy("updated_at", "desc")
-                    ->offset($placeid)
-                    ->limit($data['pagesize'])
-                    ->get();
+                ->where(function ($query) use ($data) {
+                    $query->whereRaw("JSON_CONTAINS(level, '" . intval($data['level']) . "') = 1")
+                        ->orWhereNull("level")
+                        ->orWhereRaw("level = '[]'");
+                })
+                ->where(function ($query) use ($data) {
+                    $query->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0")
+                        ->orWhereNull("ignore_ids");
+                })
+                ->orderBy("updated_at", "desc")
+                ->offset($placeid)
+                ->limit($data['pagesize'])
+                ->get();
             if (empty($result)) {
             if (empty($result)) {
                 return Result::error("此栏目暂无相关新闻", 0);
                 return Result::error("此栏目暂无相关新闻", 0);
             }
             }
@@ -764,38 +779,38 @@ class NewsService implements NewsServiceInterface
             }
             }
             // return Result::error($data,0);
             // return Result::error($data,0);
             $where['art_id'] = $data['art_id'];
             $where['art_id'] = $data['art_id'];
-            // $query = ArticleSurvey::where('art_id',$data['art_id']); 
-            
-        }else{
+            // $query = ArticleSurvey::where('art_id',$data['art_id']);
+
+        } else {
             $survey = Article::where(function ($query) {
             $survey = Article::where(function ($query) {
                 $query->whereRaw("JSON_CONTAINS(cat_arr_id, '28')")
                 $query->whereRaw("JSON_CONTAINS(cat_arr_id, '28')")
-                      ->orWhereRaw("JSON_CONTAINS(cat_arr_id, '\"28\"')");
+                    ->orWhereRaw("JSON_CONTAINS(cat_arr_id, '\"28\"')");
             })
             })
-            ->where('status',1)
-            ->where('is_survey',1)
-            ->select('survey_id')
-            ->orderBy('updated_at','desc')
-            ->first();
-            if(empty($survey)){
-                return Result::error("暂无调查问卷",0); 
+                ->where('status', 1)
+                ->where('is_survey', 1)
+                ->select('survey_id')
+                ->orderBy('updated_at', 'desc')
+                ->first();
+            if (empty($survey)) {
+                return Result::error("暂无调查问卷", 0);
             }
             }
             $where['sur_id'] = $survey['survey_id'];
             $where['sur_id'] = $survey['survey_id'];
             // $query = ArticleSurvey::where('sur_id',$survey['sur_id']);
             // $query = ArticleSurvey::where('sur_id',$survey['sur_id']);
         }
         }
         // return Result::success($where);
         // return Result::success($where);
-        $result =  ArticleSurvey::where($where)
+        $result = ArticleSurvey::where($where)
             ->where(function ($query) {
             ->where(function ($query) {
                 $query->where('is_other', 0)
                 $query->where('is_other', 0)
-                      ->orWhere(function ($subQuery) {
-                          $subQuery->where('is_other', 1)
-                                   ->where('other_id', 0);
-                      });
+                    ->orWhere(function ($subQuery) {
+                        $subQuery->where('is_other', 1)
+                            ->where('other_id', 0);
+                    });
             })
             })
             ->leftJoin('article', 'article_survey.art_id', 'article.id')
             ->leftJoin('article', 'article_survey.art_id', 'article.id')
             ->select('article_survey.*', 'article.survey_type')
             ->select('article_survey.*', 'article.survey_type')
             ->get()->all();
             ->get()->all();
-        if(empty($result)){
-            return Result::error("此文章暂无调查问卷",0); 
+        if (empty($result)) {
+            return Result::error("此文章暂无调查问卷", 0);
         }
         }
         return Result::success($result);
         return Result::success($result);
     }
     }
@@ -847,20 +862,21 @@ class NewsService implements NewsServiceInterface
     public function addWebsiteSurveyVote(array $data): array
     public function addWebsiteSurveyVote(array $data): array
     {
     {
         // return Result::success($data);
         // return Result::success($data);
-        if(isset($data['website_id']) && !empty($data['website_id'])){
-            $website = Website::where('id',$data['website_id'])->first(); 
-            if(empty($website)){
-                return Result::error("暂无此网站",0); 
+        if (isset($data['website_id']) && !empty($data['website_id'])) {
+            $website = Website::where('id', $data['website_id'])->first();
+            if (empty($website)) {
+                return Result::error("暂无此网站", 0);
+
             }
             }
-            if(isset($data['sur_id']) && !empty($data['sur_id'])){
-                $is_survey = ArticleSurvey::where('sur_id',$data['sur_id'])->first();
+            if (isset($data['sur_id']) && !empty($data['sur_id'])) {
+                $is_survey = ArticleSurvey::where('sur_id', $data['sur_id'])->first();
                 // return Result::success($survey);
                 // return Result::success($survey);
-                if(empty($is_survey)){
-                    return Result::error("此调查问卷不存在",0);
+                if (empty($is_survey)) {
+                    return Result::error("此调查问卷不存在", 0);
                 }
                 }
                 // return Result::success($survey);
                 // return Result::success($survey);
-                // 调查问卷类型   
-                if(isset($data['choice_id']) &&!empty($data['choice_id'])){
+                // 调查问卷类型
+                if (isset($data['choice_id']) && !empty($data['choice_id'])) {
                     //多选 若是json型则转化成数组类型
                     //多选 若是json型则转化成数组类型
                     if (strpos($data['choice_id'], '[') === 0) {
                     if (strpos($data['choice_id'], '[') === 0) {
                         $data['choice_id'] = json_decode($data['choice_id'], true);
                         $data['choice_id'] = json_decode($data['choice_id'], true);
@@ -869,31 +885,31 @@ class NewsService implements NewsServiceInterface
                         $data['choice_id'] = [$data['choice_id']];
                         $data['choice_id'] = [$data['choice_id']];
                     }
                     }
                     $data['choice_id'] = array_map('intval', $data['choice_id']);
                     $data['choice_id'] = array_map('intval', $data['choice_id']);
-                    $other = ArticleSurvey::whereIn('id',$data['choice_id'])
-                    ->where('website_id',$data['website_id'])
-                    ->where('is_other',1)
-                    ->where('other_id',0)
-                    ->first();
-                    if(!empty($other)){
-                        return Result::error("请选择已有的选项!",0);
+                    $other = ArticleSurvey::whereIn('id', $data['choice_id'])
+                        ->where('website_id', $data['website_id'])
+                        ->where('is_other', 1)
+                        ->where('other_id', 0)
+                        ->first();
+                    if (!empty($other)) {
+                        return Result::error("请选择已有的选项!", 0);
                     }
                     }
-                    $choice['other'] = ArticleSurvey::whereIn('id',$data['choice_id'])
-                    ->where('website_id',$data['website_id'])
-                    ->where('is_other',1)
-                    ->where('other_id','!=',0)
-                    ->first();
+                    $choice['other'] = ArticleSurvey::whereIn('id', $data['choice_id'])
+                        ->where('website_id', $data['website_id'])
+                        ->where('is_other', 1)
+                        ->where('other_id', '!=', 0)
+                        ->first();
                     // return Result::success($choice['other']);
                     // return Result::success($choice['other']);
                     $choice_id = $data['choice_id'];
                     $choice_id = $data['choice_id'];
-                    if(!empty($choice['other'])){
+                    if (!empty($choice['other'])) {
                         // array_push($data['choice_id'],$choice['other']['other_id']);
                         // array_push($data['choice_id'],$choice['other']['other_id']);
-                        if(!empty($choice_id)){
+                        if (!empty($choice_id)) {
                             $key = array_search($choice['other']['id'], $choice_id);
                             $key = array_search($choice['other']['id'], $choice_id);
-                            if ($key!== false) {
+                            if ($key !== false) {
                                 unset($choice_id[$key]);
                                 unset($choice_id[$key]);
                                 $choice_id = array_values($choice_id);
                                 $choice_id = array_values($choice_id);
                             }
                             }
-                            array_push($choice_id,$choice['other']['other_id']);
-                        }else{
+                            array_push($choice_id, $choice['other']['other_id']);
+                        } else {
                             $choice_id[0] = $choice['other']['other_id'];
                             $choice_id[0] = $choice['other']['other_id'];
                         }
                         }
                         // return Result::success($data['choice_id']);
                         // return Result::success($data['choice_id']);
@@ -905,14 +921,15 @@ class NewsService implements NewsServiceInterface
                     if (empty($choice)) {
                     if (empty($choice)) {
                         return Result::error("请选择已有的选项!", 0);
                         return Result::error("请选择已有的选项!", 0);
                     }
                     }
-                    $survey['data'] = ArticleSurvey::where('sur_id',$data['sur_id'])
-                    ->where('website_id',$data['website_id'])
-                    ->where('other_id', 0)
-                    ->get();
+                    $survey['data'] = ArticleSurvey::where('sur_id', $data['sur_id'])
+                        ->where('website_id', $data['website_id'])
+                        ->where('other_id', 0)
+                        ->get();
                     $survey['choice'] = $choice_id;
                     $survey['choice'] = $choice_id;
                     return Result::success($survey);
                     return Result::success($survey);
+
                 }
                 }
-                return Result::error("参数必填!"); 
+                return Result::error("参数必填!");
             }
             }
             return Result::error("此调查问卷不存在", 0);
             return Result::error("此调查问卷不存在", 0);
         }
         }