15313670163 2 недель назад
Родитель
Сommit
05d6be630a
1 измененных файлов с 50 добавлено и 28 удалено
  1. 50 28
      app/JsonRpc/NewsService.php

+ 50 - 28
app/JsonRpc/NewsService.php

@@ -446,19 +446,16 @@ class NewsService implements NewsServiceInterface
     {
         // return Result::success($data);
         $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 (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); 
+            if (empty($category)) {
+                return Result::error("此网站暂无此栏目", 0);
             }
         }
         // return Result::success($where);
@@ -489,8 +486,7 @@ class NewsService implements NewsServiceInterface
         ->orderBy("updated_at", "desc")
         ->limit($data['pageSize'])
         ->offset(($data['page'] - 1) * $data['pageSize'])
-        ->get()
-        // ;
+        ->get()->all()
         ->map(function ($article ) use ($data) {
             $catid = $article->catid ?? 0;
             $pinyin = '';
@@ -504,7 +500,7 @@ class NewsService implements NewsServiceInterface
             return $article;
             });
 
-        $count =  Article::where(function ($query) use ($where) {
+        $count = Article::where(function ($query) use ($where) {
             foreach ($where as $condition) {
                 if ($condition[1] === 'in') {
                     $query->whereIn($condition[0], $condition[2]);
@@ -515,11 +511,11 @@ class NewsService implements NewsServiceInterface
         })->count();
 
         $data = [
-            'rows'=>$rep->toArray(),
-            'count'=>$count
+            'rows' => $rep->toArray(),
+            'count' => $count,
         ];
-        
-        if(empty($rep)){
+
+        if (empty($rep)) {
             return Result::error("没有信息数据");
         }
         return Result::success($data);
@@ -829,40 +825,66 @@ class NewsService implements NewsServiceInterface
     {
         $where = [];
         // 初始化查询构造器
-        $category = WebsiteCategory::where('website_id',$data['website_id'])->pluck('category_id');
+        $category = WebsiteCategory::where('website_id', $data['website_id'])->pluck('category_id');
         $query = Article::where('status', 1)
             ->whereIn('catid', $category)
             ->where(function ($query) use ($data) {
                 $query->where(function ($subQuery) use ($data) {
-                    $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($data['website_id'])."') = 0");
+                    $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0");
                 })->orWhereNull("ignore_ids");
             });
         // return Result::success($all_articles);
         // 检查是否存在 cityid 参数
         if (isset($data['cityid']) && !empty($data['cityid'])) {
-            $query->whereRaw("JSON_CONTAINS(city_arr_id, '".intval($data['cityid'])."')");
+            $query->whereRaw("JSON_CONTAINS(city_arr_id, '" . intval($data['cityid']) . "')");
         }
         // 检查是否存在 department_id 参数
         if (isset($data['department_id']) && !empty($data['department_id'])) {
-            $query->whereRaw("JSON_CONTAINS(department_arr_id, '".intval($data['department_id'])."')");
+            $query->whereRaw("JSON_CONTAINS(department_arr_id, '" . intval($data['department_id']) . "')");
         }
         // 检查是否存在 keyword 参数
         if (isset($data['keyword']) && !empty($data['keyword'])) {
-            $query->where('title', 'like', '%'.$data['keyword'].'%');
+            $query->where('title', 'like', '%' . $data['keyword'] . '%');
         }
         // 计算总数
         $count = $query->count();
         // 分页查询
-        $articles = $query->orderBy("updated_at", "desc")
-            ->limit($data['pageSize'])
+        $articles = $query
+            ->select(
+                'article.id',
+                'article.title',
+                'article.imgurl',
+                'article.author',
+                'article.updated_at',
+                'article.introduce',
+                'article.islink',
+                'article.linkurl',
+                'article.copyfrom',
+                'article.catid',
+                'article.department_arr_id',
+                'article.city_arr_id',)
+            ->orderBy("updated_at", "desc")
             ->offset(($data['page'] - 1) * $data['pageSize'])
-            ->get()->all();
+            ->limit($data['pageSize'])
+            ->get()->all()
+            ->map(function ($article ) use ($data) {
+                $catid = $article->catid ?? 0;
+                $pinyin = '';
+                $category = WebsiteCategory::where('category_id', $catid)->where('website_category.website_id', $data['website_id'])->first();
+                $pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
+                if ($category->pid != 0 && !empty($category->aLIas_pinyin)) {
+                    $childCategory = WebsiteCategory::where('category_id', $category->pid)->where('website_category.website_id', $data['website_id'])->first();
+                    $pinyin = $childCategory->aLIas_pinyin ?  $childCategory->aLIas_pinyin.'/'. $category->aLIas_pinyin : null;
+                }
+                $article->pinyin = $pinyin;
+                return $article;
+                });
         if (empty($articles)) {
             return Result::error("没有符合条件的资讯数据");
         }
         $data = [
             'rows' => $articles,
-            'count' => $count
+            'count' => $count,
         ];
         return Result::success($data);
     }