rkljw 1 tydzień temu
rodzic
commit
a1c9dc30b0
1 zmienionych plików z 49 dodań i 53 usunięć
  1. 49 53
      app/JsonRpc/NewsService.php

+ 49 - 53
app/JsonRpc/NewsService.php

@@ -1660,7 +1660,7 @@ class NewsService implements NewsServiceInterface
       //     $pinyin = '';
       $category = WebsiteCategory::where('category_id', $catid)->where('website_id', $data['website_id'])->first();
       if (!empty($category->pid) && $category->pid != 0) {
-        $level = json_decode($category->category_arr_id);
+        $level = json_decode($category->category_arr_id, true);
         $pinyin = WebsiteCategory::where('website_id', $data['website_id'])
           ->whereIn('category_id', $level)
           ->orderByRaw('FIELD(category_id, ' . implode(',', $level) . ')')
@@ -3093,7 +3093,7 @@ class NewsService implements NewsServiceInterface
             break;
           case 4:
             // 招聘
-            //   `status` int DEFAULT '0' COMMENT '状态   0:待审核;1:已审核;(只有企业会员需要审核)',
+            //   `status` int DEFAULT '0' COMMENT '状态   0:待审核;1:已审核通过;(只有企业会员需要审核)',
             $article = JobRecruiting::where('website_id', $data['website_id'])
               ->where('status', 1)
               ->where('id', $data['id'])
@@ -5364,63 +5364,59 @@ class NewsService implements NewsServiceInterface
       ->limit(6)
       ->get();
     // 获取分类文章数据
-    $categoryIds =  WebsiteCategory::where(['website_id' => $data['website_id'], 'pid' => 0])
-      ->pluck('category_id')
-      ->toArray();
 
-    //    $categoryIds = [627, 628, 629, 630, 631, 632, 633, 634, 635, 636];
+    $categoryIds = WebsiteCategory::where(['website_id' => $data['website_id'], 'pid' => 0])
+    ->pluck('category_id')
+    ->toArray();
 
-    // 使用子查询优化
-    $articlesList = DB::table('article')
-      ->select([
-        'article.id',
-        'article.title',
-        'article.imgurl',
-        'article.created_at',
-        'category.name as category_name',
-        'category.id as category_id',
-        'website_category.aLIas_pinyin as pinyin',
-        DB::raw('100 as type')
-      ])
-      ->join('category', 'article.catid', '=', 'category.id')
-      ->leftJoin('website_category', 'website_category.category_id', '=', 'category.id')
-      ->where('website_category.website_id', $data['website_id'])
-      ->where('article.status', 1)
-      ->whereIn('category.id', $categoryIds)
-      // ->whereRaw('article.created_at >= DATE_SUB(NOW(), INTERVAL 30 DAY)')
-      ->orderBy('article.created_at', 'desc')
-      ->get()
-      ->groupBy('category_id')
-      ->map(function ($group) {
-        // 确保第一条数据有图片
-        $firstWithImage = $group->first(function ($item) {
-          return !empty($item->imgurl);
-        });
+    $articlesList = [];
+    foreach ($categoryIds as $catid) {
+        // 先查有图片的
+        $withImg = DB::table('article')
+            ->where('status', 1)
+            ->where('catid', $catid)
+            ->whereNotNull('imgurl')
+            ->where('imgurl', '!=', '')
+            ->orderBy('updated_at', 'desc')
+            ->limit(6)
+            ->get();
 
-        if ($firstWithImage) {
-          // 如果有带图片的文章,将其放在第一位
-          $group = $group->filter(function ($item) use ($firstWithImage) {
-            return $item->id !== $firstWithImage->id;
-          })->prepend($firstWithImage);
+        $count = $withImg->count();
+        if ($count < 6) {
+            // 不足6条再查无图片的补足
+            $withoutImg = DB::table('article')
+                ->where('status', 1)
+                ->where('catid', $catid)
+                ->where(function($q){
+                    $q->whereNull('imgurl')->orWhere('imgurl', '');
+                })
+                ->orderBy('updated_at', 'desc')
+                ->limit(6 - $count)
+                ->get();
+            $articles = $withImg->concat($withoutImg);
+        } else {
+            $articles = $withImg;
         }
 
-        return [
-          'category_name' => $group->first()->category_name,
-          'catid' => $group->first()->category_id,
-          'pinyin' => $group->first()->pinyin,
-          'type' => 100,
-          'list' => $group->take(6)->map(function ($item) {
-            return [
-              'id' => $item->id,
-              'title' => $item->title,
-              'imgurl' => $item->imgurl,
-              'created_at' => $item->created_at
-            ];
-          })->values()
+        // 组装
+        $category = \App\Model\Category::find($catid);
+        $websiteCategory = WebsiteCategory::where('category_id', $catid)->first();
+        $articlesList[] = [
+            'category_name' => $category ? $category->name : '',
+            'catid' => $catid,
+            'pinyin' => $websiteCategory ? $websiteCategory->aLIas_pinyin : '',
+            'type' => 100,
+            'list' => $articles->map(function ($item) {
+                return [
+                    'id' => $item->id,
+                    'title' => $item->title,
+                    'imgurl' => $item->imgurl,
+                    'created_at' => $item->created_at
+                ];
+            })->values()
         ];
-      })
-      ->values()
-      ->toArray();
+    }
+
 
     $result = [
       'almanac' => $almanacInfo,