wesmiler 1 year ago
parent
commit
82d365ed84

+ 12 - 3
app/Http/Controllers/Api/v1/ArticleController.php

@@ -32,9 +32,18 @@ class ArticleController extends webApp
      */
     public function category()
     {
-        $params =request()->post();
-        $pageSize = request()->post('pageSize', 15);
-        $datas = ArticleService::make()->getCategory($params, $pageSize);
+        $datas = ArticleService::make()->getCategory();
+        return message(1010, true, $datas);
+    }
+
+    /**
+     * 分类
+     * @return array
+     */
+    public function cates()
+    {
+        $type = request()->post('type', 0);
+        $datas = ArticleService::make()->getCateList($type);
         return message(1010, true, $datas);
     }
 

+ 41 - 0
app/Services/Api/ArticleService.php

@@ -11,6 +11,7 @@
 
 namespace App\Services\Api;
 
+use App\Models\ArticleCateModel;
 use App\Models\ArticleModel;
 use App\Services\BaseService;
 use App\Services\ConfigService;
@@ -58,6 +59,7 @@ class ArticleService extends BaseService
         $where = ['a.mark' => 1];
         $status = isset($params['status'])? $params['status'] : 0;
         $type = isset($params['type'])? $params['type'] : 0;
+        $publishType = isset($params['publish_type'])? $params['publish_type'] : 0;
         $cateId = isset($params['cate_id'])? $params['cate_id'] : 0;
         if($status>0){
             $where['a.status'] = $status;
@@ -65,6 +67,11 @@ class ArticleService extends BaseService
         if($type>0){
             $where['a.type'] = $type;
         }
+
+        if($publishType>0){
+            $where['a.publish_type'] = $publishType;
+        }
+
         if($cateId>0){
             $where['a.cate_id'] = $cateId;
         }
@@ -96,6 +103,40 @@ class ArticleService extends BaseService
         ];
     }
 
+    /**
+     * 类目
+     * @return array[]
+     */
+    public function getCateList($type=0)
+    {
+        $cacheKey = "caches:articles:cates_{$type}";
+        $datas = RedisService::get($cacheKey);
+        if($datas){
+            return $datas;
+        }
+
+        $where = ['status'=>1,'mark'=>1];
+        if($type){
+            $where['type'] = $type;
+        }
+        $datas = ArticleCateModel::where($where)
+            ->select(['id','icon','name','type','status'])
+            ->orderBy('sort','desc')
+            ->orderBy('id','desc')
+            ->get();
+        if($datas){
+            foreach ($datas as &$item){
+                $item['icon'] = $item['icon']? get_image_url($item['icon']) : '';
+            }
+            unset($item);
+        }
+        return $datas;
+    }
+
+    /**
+     * 分类
+     * @return array[]
+     */
     public function getCategory()
     {
         $datas = [

+ 4 - 0
app/Services/Common/ArticleService.php

@@ -57,6 +57,10 @@ class ArticleService extends BaseService
             $map[] = ['type', '=', $param['type']];
         }
 
+        if (isset($param['publish_type'])) {
+            $map[] = ['publish_type', '=', $param['publish_type']];
+        }
+
         //获取数据总数
         $count = $this->model->where($map)->count();
         // 分页条件

+ 1 - 0
routes/api.php

@@ -229,6 +229,7 @@ Route::prefix('v1')->group(function(){
 
     // 文章
     Route::post('/article/index', [\App\Http\Controllers\Api\v1\ArticleController::class, 'index']);
+    Route::post('/article/cates', [\App\Http\Controllers\Api\v1\ArticleController::class, 'cates']);
     Route::post('/article/category', [\App\Http\Controllers\Api\v1\ArticleController::class, 'category']);
     Route::post('/article/info', [\App\Http\Controllers\Api\v1\ArticleController::class, 'info']);
     Route::post('/article/page', [\App\Http\Controllers\Api\v1\ArticleController::class, 'page']);