AI 1 개월 전
부모
커밋
ea189c056f
1개의 변경된 파일62개의 추가작업 그리고 47개의 파일을 삭제
  1. 62 47
      app/JsonRpc/NewsService.php

+ 62 - 47
app/JsonRpc/NewsService.php

@@ -19,6 +19,8 @@ use Hyperf\RpcServer\Annotation\RpcService;
 use App\Tools\Result;
 use Ramsey\Uuid\Uuid;
 use Hyperf\Utils\Random;
+use Fukuball\Jieba\Jieba;
+use Fukuball\Jieba\Finalseg;
 
 #[RpcService(name: "NewsService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
 class NewsService implements NewsServiceInterface
@@ -31,7 +33,7 @@ class NewsService implements NewsServiceInterface
      */
     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)) {
             return Result::error("没有导航池数据");
         }
@@ -275,7 +277,14 @@ class NewsService implements NewsServiceInterface
             var_dump($articleData['level'], '----------$articleData[level]----------1');
             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'] == '') {
                 //提取内容中的描述
@@ -398,8 +407,8 @@ class NewsService implements NewsServiceInterface
                 $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;
             unset($articleData['content']);
             unset($articleData['status_name']);
@@ -430,7 +439,13 @@ class NewsService implements NewsServiceInterface
             var_dump($articleData['level'], '----------$articleData[level]----------1');
             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'] == '') {
                 //提取内容中的描述
@@ -455,6 +470,7 @@ class NewsService implements NewsServiceInterface
                 var_dump($suveys_array, '---------------------1');
                 var_dump($suvey_array, '---------------------2');
                 $suvey_data = [];
+
                 if (is_array($suveys_array)) {
                     foreach ($suveys_array as $key => $value) {
                         if ($value == '') {
@@ -482,7 +498,6 @@ class NewsService implements NewsServiceInterface
                                 'is_other' => 0,
                                 'other_id' => 0,
                                 'results' => 0,
-
                             ];
                         }
                         if (empty($suvey_data)) {
@@ -553,8 +568,8 @@ class NewsService implements NewsServiceInterface
      */
     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 = [];
         if ($category) {
             $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) {
                     if ($data['level'] != 4 && $data['level'] != 5) {
                         $query->whereRaw("JSON_CONTAINS(level, '" . intval($data['level']) . "') = 1")
-                                ->orderBy("updated_at", "desc");
+                            ->orderBy("updated_at", "desc");
                     }
                 })
                 ->offset($placeid)
@@ -617,25 +632,25 @@ class NewsService implements NewsServiceInterface
             ];
             $placeid = isset($data['placeid']) && !empty($data['placeid']) ? $data['placeid'] - 1 : 0;
             // 1:文字新闻;2:轮播图;3:图文;
-            // 级别:0:未分类 
+            // 级别:0:未分类
             // 3:推荐图片
             if ($data['level'] == 1) {
                 $data['level'] = 0;
-             } 
+            }
             $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)) {
                 return Result::error("此栏目暂无相关新闻", 0);
             }
@@ -773,19 +788,19 @@ class NewsService implements NewsServiceInterface
             $where['sur_id'] = $survey['sur_id'];
             // $query = ArticleSurvey::where('sur_id',$survey['sur_id']);
         }
-        $result =  ArticleSurvey::where($where)
+        $result = ArticleSurvey::where($where)
             ->where(function ($query) {
                 $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')
             ->select('article_survey.*', 'article.survey_type')
             ->get()->all();
-        if(empty($result)){
-            return Result::error("此文章暂无调查问卷",0); 
+        if (empty($result)) {
+            return Result::error("此文章暂无调查问卷", 0);
         }
         return Result::success($result);
     }
@@ -837,10 +852,10 @@ class NewsService implements NewsServiceInterface
     public function addWebsiteSurveyVote(array $data): array
     {
 
-        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'])) {
                 $survey = ArticleSurvey::where('sur_id', $data['sur_id'])->where('website_id', $data['website_id'])->pluck('sur_id');
@@ -848,8 +863,8 @@ class NewsService implements NewsServiceInterface
                     return Result::error("此调查问卷不存在", 0);
                 }
                 // return Result::success($survey);
-                // 调查问卷类型   
-                if(isset($data['choice_id']) &&!empty($data['choice_id'])){
+                // 调查问卷类型
+                if (isset($data['choice_id']) && !empty($data['choice_id'])) {
                     //多选 若是json型则转化成数组类型
                     if (strpos($data['choice_id'], '[') === 0) {
                         $data['choice_id'] = json_decode($data['choice_id'], true);
@@ -858,13 +873,13 @@ class NewsService implements NewsServiceInterface
                         $data['choice_id'] = [$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'])
@@ -884,12 +899,12 @@ class NewsService implements NewsServiceInterface
                         return Result::error("请选择已有的选项!", 0);
                     }
                     $retult_survey['data'] = ArticleSurvey::where('website_id', $data['website_id'])
-                    ->where('sur_id', $data['sur_id'])
-                    ->get();
+                        ->where('sur_id', $data['sur_id'])
+                        ->get();
                     $retult_survey['choice'] = $data['choice_id'];
                     return Result::success($retult_survey);
                 }
-                return Result::error("参数必填!"); 
+                return Result::error("参数必填!");
             }
             return Result::error("此调查问卷不存在", 0);
         }