Browse Source

合并c端-获取新闻列表及获取新闻详情

15313670163 1 month ago
parent
commit
174b718d42
3 changed files with 208 additions and 25 deletions
  1. 201 23
      app/JsonRpc/NewsService.php
  2. 6 1
      app/JsonRpc/NewsServiceInterface.php
  3. 1 1
      runtime/hyperf.pid

+ 201 - 23
app/JsonRpc/NewsService.php

@@ -7,7 +7,7 @@ use App\Model\Category;
 use App\Model\WebsiteCategory;
 use App\Model\ArticleSurvey;
 use App\Model\Good;
-
+use App\Model\Website;
 use Hyperf\DbConnection\Db;
 use Hyperf\RpcServer\Annotation\RpcService;
 use App\Tools\Result;
@@ -453,7 +453,8 @@ class NewsService implements NewsServiceInterface
     public function getArticleInfo(array $data): array
     {
         $where = [
-            'article.id' => $data['id'],
+            'article.id'=>$data['id'],
+            'article.status'=>1
         ];
         $result = Article::where($where)->leftJoin("article_data", "article.id", "article_data.article_id")->first();
         $articleSurvey = ArticleSurvey::where(['art_id' => $data['id']])->get();
@@ -554,6 +555,90 @@ class NewsService implements NewsServiceInterface
         return Result::success($result);
 
     }
+/**
+     *获取新闻列表 
+     * @param array $data
+     * @return array
+     */
+    public function getWebsiteArticleList(array $data): array
+    {
+        $where[] = ['status', '=', 1];
+       if(isset($data['keyword'])  && !empty($data['keyword'])){
+            array_push($where,['article.title','like','%'.$data['keyword'].'%']);
+        }
+        if(isset($data['catid'])  && !empty($data['catid'])){
+            if(is_array($data['catid'])){
+                $category = WebsiteCategory::where('website_id',$data['website_id'])->whereIn('category_id',$data['catid'])->pluck('category_id');
+                array_push($where,['catid', 'in', $data['catid']]);
+            }else{
+                $category = WebsiteCategory::where('website_id',$data['website_id'])->where('category_id',$data['catid'])->pluck('category_id');
+                array_push($where,['catid', '=', $data['catid']]);
+            }
+            if(empty($category)){
+               return Result::error("此网站暂无此栏目",0); 
+            }
+        }
+        // return Result::success($where);
+
+        $rep = Article::where(function ($query) use ($where) {
+            foreach ($where as $condition) {
+                if ($condition[1] === 'in') {
+                    $query->whereIn($condition[0], $condition[2]);
+                } else {
+                    $query->where($condition[0], $condition[1], $condition[2]);
+                }
+            }
+        })
+        ->orderBy("created_at", "desc")
+        ->limit($data['pageSize'])
+        ->offset(($data['page'] - 1) * $data['pageSize'])
+        ->get();
+
+        $count =  Article::where(function ($query) use ($where) {
+            foreach ($where as $condition) {
+                if ($condition[1] === 'in') {
+                    $query->whereIn($condition[0], $condition[2]);
+                } else {
+                    $query->where($condition[0], $condition[1], $condition[2]);
+                }
+            }
+        })->count();
+
+        $data = [
+            'rows'=>$rep->toArray(),
+            'count'=>$count
+        ];
+        
+        if(empty($rep)){
+            return Result::error("没有信息数据");
+        }
+        return Result::success($data);
+        
+    }
+    /**
+     * 前端-获取新闻详情
+     * @param array $data
+     * @return array
+     */
+    public function selectWebsiteArticleInfo(array $data): array
+    {
+        $where = [
+            'article.id'=>$data['id'],
+            'article.status'=>1
+        ];
+        $result = Article::where($where)->leftJoin("article_data","article.id","article_data.article_id")->first();
+        if(empty($result)){
+            return Result::error("查询失败",0);
+        }
+        $category = WebsiteCategory::where('website_id',$data['website_id'])->where(['category_id'=>$result['catid']])->first();
+        if(empty($category)){
+            return Result::error("查询失败",0);
+        }
+        $result['category_id'] = $category['category_id'];
+        $result['cat_name'] = $category['name'];
+        return Result::success($result);
+    }
+
 
     /**
      * 前端-获取网站调查问卷
@@ -562,16 +647,16 @@ class NewsService implements NewsServiceInterface
      */
     public function getWebsiteSurvey(array $data): array
     {
-        if (isset($data['survey_id']) && !empty($data['survey_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['art_id']) && !empty($data['art_id'])) {
-            $article = Article::where('id', $data['art_id'])->first();
-            if (empty($article)) {
-                return Result::error("暂无此文章", 0);
+        if(isset($data['art_id'])  && !empty($data['art_id'])){
+            $article = Article::where('id',$data['art_id'])->where('status',1)->first();
+            if(empty($article)){
+                return Result::error("暂无此文章",0);
             }
             // return Result::error($data,0);
             $where['art_id'] = $data['art_id'];
@@ -585,14 +670,14 @@ class NewsService implements NewsServiceInterface
             $where['sur_id'] = $survey['sur_id'];
             // $query = ArticleSurvey::where('sur_id',$survey['sur_id']);
         }
-        $result['survey'] = ArticleSurvey::where($where)->where('is_other', 0)
-            ->leftJoin('article', 'article_survey.art_id', 'article.id')
-            ->select('article_survey.*', 'article.survey_type')
-            ->get()->all();
-        $result['others'] = ArticleSurvey::where($where)->where('is_other', 1)->where('other_id', 0)->first();
-        $result['other'] = ArticleSurvey::where($where)->where('is_other', 1)->where('other_id', '!=', 0)->orderByDesc('created_at')->first();
-        if (empty($result)) {
-            return Result::error("此文章暂无调查问卷", 0);
+        $result['survey'] =  ArticleSurvey::where($where)->where('is_other',0)
+        ->leftJoin('article','article_survey.art_id','article.id')
+        ->select('article_survey.*','article.survey_type')
+        ->get()->all();
+        $result['other'] = ArticleSurvey::where($where)->where('is_other',1)->where('other_id',0)->first();
+        $result['others'] = ArticleSurvey::where($where)->where('is_other',1)->where('other_id','!=',0)->orderByDesc('created_at')->first();
+        if(empty($result)){
+            return Result::error("此文章暂无调查问卷",0); 
         }
         return Result::success($result);
     }
@@ -643,11 +728,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');
@@ -674,6 +758,14 @@ class NewsService implements NewsServiceInterface
                         }
                     }
                     // return Result::success($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);
+                    }
                     $choice['other'] = ArticleSurvey::whereIn('id', $data['choice_id'])
                         ->where('website_id', $data['website_id'])
                         ->where('is_other', 1)
@@ -704,6 +796,92 @@ class NewsService implements NewsServiceInterface
         }
         return Result::error("参数必填!");
     }
+    /**
+     * 后端-获取网站调查问卷列表
+     * @param array $data
+     * @return array
+     */
+    public function getSurveyList(array $data): array
+    {
+        $where = [];
+        if(isset($data['survey_name']) &&!empty($data['survey_name'])){
+            array_push($where,['survey_name','like','%'.$data['survey_name'].'%']);
+        }
+        if(isset($data['survey_type']) && $data['survey_type'] != null){
+            array_push($where,['survey_type','=',$data['survey_type']]);
+        }
+        if(isset($data['is_survey']) && $data['is_survey'] != null){
+            array_push($where,['is_survey','=',$data['is_survey']]);
+        }
+        // return Result::success($where);
+        if(!empty($where)){
+            $query = Article::where($where)->whereNotNull('survey_name');
+        }else{
+            $query = Article::whereNotNull('survey_name');
+        }
+        $count = $query->count();
+        $survey = $query->orderByDesc('id')
+        ->limit($data['pageSize'])
+        ->offset(($data['page']-1)*$data['pageSize'])
+        ->get();
+        if(empty($survey->toArray())){
+            return Result::error("暂无调查问卷!",0);
+        }
+        $result = [
+            'rows'=>$survey,
+            'count'=>$count
+        ];
+        return Result::success($result);
+    }
+    /**
+     * 后端-获取网站调查问卷详情
+     * @param array $data
+     * @return array 
+     */
+    public function getSurveyInfo(array $data): array
+    {
+        if(isset($data['sur_id']) &&!empty($data['sur_id'])){
+            $where = [ 'sur_id'=>$data['sur_id']];
+            $choose =  ArticleSurvey::where($where)->where('is_other',0)
+            ->leftJoin('article','article_survey.art_id','article.id')
+            ->select('article_survey.*','article.survey_type')
+            ->get()->all();
+            if(empty($choose)){
+                return Result::error("此调查问卷不存在",0);
+            }
+            $resultsArray  = array_column($choose, 'results');
+            $total = array_sum($resultsArray);
+            $other = ArticleSurvey::where($where)->where('is_other',1)->where('other_id',0)->first();
+            $others = ArticleSurvey::where($where)->where('is_other',1)->where('other_id','!=',0)->orderByDesc('created_at')->get()->all();
+            // $total = 0;
+            if(!empty($other)){
+                $total = $total + $other['results'];
+                $other['choice_name'] = $other['choice_name'].'(其他)';
+                if(!empty($others)){
+                    $other['hasChildren'] = true;
+                    // array_push($other,['hasChildren','=',true]);
+                    $other['children'] = $others;
+                    $other_choices = [$other->toArray()];
+                    $mer_choice = array_merge($choose,$other_choices);
+                    $value_choice = array_values($mer_choice);
+                }
+                else{
+                    // return Result::error('1111');
+                    $other_choices = [$other->toArray()];
+                    $other_choices = array_merge($choose,$other_choices);
+                    $value_choice = array_values($other_choices);
+                    // return Result::success($result);
+                }
+            }else{
+                $value_choice = $choose;
+            }
+            $result = [
+                'choose'=>$value_choice,
+                'total'=>$total 
+            ];
+        }
+        return Result::success($result);
+    }
 
     /**
      * 验证导航名称是否重复

+ 6 - 1
app/JsonRpc/NewsServiceInterface.php

@@ -128,11 +128,16 @@ interface NewsServiceInterface
      * @return array
      */
     public function addWebsiteSurveyVote(array $data):array;
-
     /**
      * @param array $data
      * @return array
      */
     public function checkCategoryName(array $data):array;
 
+    public function getSurveyList(array $data):array;
+    /**
+     * @param array $data
+     * @return array
+     */
+    public function getSurveyInfo(array $data):array;
 }

+ 1 - 1
runtime/hyperf.pid

@@ -1 +1 @@
-64458
+39344