wesmiler 5 месяцев назад
Родитель
Сommit
a112f058a1

+ 47 - 9
app/Http/Controllers/Api/v1/ArticleController.php

@@ -19,30 +19,68 @@ class ArticleController extends webApp
      */
     public function index()
     {
-        $params =request()->post();
+        $params = request()->post();
         $pageSize = request()->post('pageSize', 15);
         $datas = ArticleService::make()->getDataList($params, $pageSize);
         return message(1010, true, $datas);
     }
 
     /**
+     * 咨询问答主页数据
+     * @return array
+     */
+    public function datas()
+    {
+        $datas = [
+            'hotList' => ArticleService::make()->getHotList($this->userId),
+            'recList' => ArticleService::make()->getRecommendList(),
+        ];
+        return message(1010, true, $datas);
+    }
+
+    /**
+     * 热门咨询
+     * @return array
+     */
+    public function hots()
+    {
+        $params = request()->all();
+        $refresh = isset($params['refresh'])? $params['refresh'] : false;
+        $datas = ArticleService::make()->getHotList($this->userId, $refresh);
+        return message(1010, true, $datas);
+    }
+
+    /**
      * 详情
      */
     public function info()
     {
         $params = request()->all();
-        $id = isset($params['id'])? intval($params['id']) : 0;
-        if(empty($id)){
+        $id = isset($params['id']) ? intval($params['id']) : 0;
+        if (empty($id)) {
             return message(1036, false);
         }
 
-        if($info = ArticleService::make()->getInfo($id)){
+        if ($info = ArticleService::make()->getInfo($id)) {
             return message(1010, true, $info);
-        }else{
+        } else {
             return message(1009, false);
         }
     }
 
+    /**
+     * 问题咨询
+     */
+    public function search()
+    {
+        $params = request()->all();
+        if ($info = ArticleService::make()->search($params)) {
+            return message(1010, true, $info);
+        } else {
+            return message('抱歉,没有找到您要的答案', false);
+        }
+    }
+
 
     /**
      * 单页数据
@@ -50,14 +88,14 @@ class ArticleController extends webApp
     public function page()
     {
         $params = request()->all();
-        $type = isset($params['type'])? intval($params['type']) : 0;
-        if(empty($type)){
+        $type = isset($params['type']) ? intval($params['type']) : 0;
+        if (empty($type)) {
             return message(1031, false);
         }
 
-        if($info = ArticleService::make()->getInfoByType($type)){
+        if ($info = ArticleService::make()->getInfoByType($type)) {
             return message(1010, true, $info);
-        }else{
+        } else {
             return message(1009, false);
         }
     }

+ 15 - 0
app/Http/Controllers/Api/v1/IndexController.php

@@ -6,6 +6,7 @@ use App\Http\Controllers\Api\webApp;
 use App\Services\Api\ArticleService;
 use App\Services\Api\CourseService;
 use App\Services\Common\AdService;
+use App\Services\Common\InstitutionService;
 use App\Services\Common\NoticeService;
 use App\Services\ConfigService;
 use App\Services\RedisService;
@@ -35,6 +36,8 @@ class IndexController extends webApp
                     'app_version' => ConfigService::make()->getConfigByCode('app_version'),
                     'wxpay_open' => ConfigService::make()->getConfigByCode('wxpay_open', 1),
                     'vip_buy_desc' => format_content(ConfigService::make()->getConfigByCode('vip_buy_desc', '')),
+                    'custom_name' => ConfigService::make()->getConfigByCode('custom_name', ''),
+                    'custom_desc' => format_content(ConfigService::make()->getConfigByCode('custom_desc', '')),
                     'video_vip_tips' => format_content(ConfigService::make()->getConfigByCode('video_vip_tips', '')),
                     'errors' => config('platform.errors'),
                 ];
@@ -73,6 +76,18 @@ class IndexController extends webApp
     }
 
     /**
+     * 院校列表
+     * @return array
+     */
+    public function institutions()
+    {
+        $params = request()->all();
+        $pageSize = isset($params['pageSize']) && $params['pageSize']? intval($params['pageSize']) : 20;
+        $data =  InstitutionService::make()->getDataList($params, $pageSize);
+        return showJson(1010, true, $data);
+    }
+
+    /**
      * 验证更新
      * @return array
      */

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

@@ -143,6 +143,69 @@ class ArticleService extends BaseService
     }
 
     /**
+     * 热门问答(都在问列表)
+     * @param $userId
+     * @param false $refresh
+     * @return array|mixed
+     */
+    public function getHotList($userId,$refresh=false)
+    {
+        $cacheKey = "caches:articles:hotList_{$userId}";
+        $datas = RedisService::get($cacheKey);
+        if($datas && !$refresh){
+            return $datas;
+        }
+
+        $model = $this->model->where(['type'=>9,'status'=>1,'mark'=>1]);
+        $model1 = clone $model;
+        if($model1->where('view_num','>', 0)->count('id')>5){
+            $model = $model1;
+        }
+
+        $num = ConfigService::make()->getConfigByCode('custom_hot_num', 3);
+        $num = $num>=2 && $num<= 20? $num : 3;
+        $datas = $model->select(['id','cover','type','title','description','status'])
+            ->orderByRaw('RAND()')
+            ->take($num)
+            ->get();
+        $datas = $datas? $datas->toArray() : [];
+        if($datas){
+            RedisService::set($cacheKey, $datas, rand(300, 600));
+        }
+
+        return $datas;
+    }
+
+    /**
+     * 推荐问答
+     * @param $userId
+     * @param false $refresh
+     * @return array|mixed
+     */
+    public function getRecommendList($refresh=false)
+    {
+        $cacheKey = "caches:articles:recList";
+        $datas = RedisService::get($cacheKey);
+        if($datas && !$refresh){
+            return $datas;
+        }
+
+        $num = ConfigService::make()->getConfigByCode('custom_rec_num', 3);
+        $num = $num>=2 && $num<= 20? $num : 3;
+        $datas = $this->model->where(['type'=>9,'status'=>1,'mark'=>1])
+            ->select(['id','cover','title','type','description','status'])
+            ->orderBy('view_num','desc')
+            ->take($num)
+            ->get();
+        $datas = $datas? $datas->toArray() : [];
+        if($datas){
+            RedisService::set($cacheKey, $datas, rand(300, 600));
+        }
+
+        return $datas;
+    }
+
+    /**
      * 获取文章详情
      * @param $id
      * @return array|mixed
@@ -172,6 +235,49 @@ class ArticleService extends BaseService
     }
 
     /**
+     * 查找或查看
+     * @param $params
+     * @return array|false|mixed
+     */
+    public function search($params)
+    {
+        $cacheKey = "caches:articles:search_".md5(json_encode($params));
+        $data = RedisService::get($cacheKey);
+        if($data){
+            return $data;
+        }
+        $id = isset($params['id'])? $params['id'] : 0;
+        $keyword = isset($params['keyword'])? $params['keyword'] : '';
+        if(empty($keyword) && $id<=0){
+            $this->error = '请选择或输入问题';
+            return false;
+        }
+
+        $where = ['id'=>0,'type'=>9,'status'=>1,'mark'=>1];
+        if($id){
+            $where['id'] = $id;
+        }else{
+            unset($where['id']);
+        }
+        $data = $this->model->where($where)
+            ->where(function($query) use($keyword){
+                if($keyword){
+                    $query->where('title', 'like',"%{$keyword}%")->orWhere('content', 'like',"%{$keyword}%");
+                }
+            })
+            ->select(['id','title','description','content','status'])
+            ->orderByRaw('RAND()')
+            ->orderBy('sort','desc')
+            ->first();
+        $data = $data? $data->toArray() :[];
+        if($data){
+            RedisService::set($cacheKey, $data, rand(3,5));
+        }
+
+        return $data;
+    }
+
+    /**
      * 获取分类文章推荐
      * @param int $cateId 推荐分类ID
      * @param int $type 类别:3-普通文章,4-客服回复

+ 1 - 0
app/Services/Api/CourseService.php

@@ -77,6 +77,7 @@ class CourseService extends BaseService
         if($datas){
             foreach ($datas as &$item){
                 $item['poster'] = $item['poster'] ? get_image_url($item['poster']) : '';
+                $item['poster_error'] = 0;
             }
 
             RedisService::set($cacheKey, $datas, rand(3600, 7200));

+ 1 - 1
config/platform.php

@@ -47,7 +47,7 @@ return [
             "id" => 99,
             "name" => '院校直达',
             "code" => 'institution-entry',
-            'page' => '/pages/institution/index'
+            'page' => '/pages/index/institution'
         ],
     ],
     // 纠错类型

+ 6 - 0
routes/api.php

@@ -23,6 +23,7 @@ Route::prefix('v1')->group(function() {
     Route::post('/login', [\App\Http\Controllers\Api\v1\LoginController::class, 'login']);
 
     Route::get('/index/data', [\App\Http\Controllers\Api\v1\IndexController::class, 'data']);
+    Route::post('/index/institutions', [\App\Http\Controllers\Api\v1\IndexController::class, 'institutions']);
 
     // 注册
     Route::post('/register', [\App\Http\Controllers\Api\v1\LoginController::class, 'register']);
@@ -38,9 +39,14 @@ Route::prefix('v1')->group(function() {
 
     // 文章
     Route::post('/article/index', [\App\Http\Controllers\Api\v1\ArticleController::class, 'index']);
+    Route::get('/article/datas', [\App\Http\Controllers\Api\v1\ArticleController::class, 'datas']);
+    Route::get('/article/hots', [\App\Http\Controllers\Api\v1\ArticleController::class, 'hots']);
+    Route::get('/article/search', [\App\Http\Controllers\Api\v1\ArticleController::class, 'search']);
     Route::get('/article/info', [\App\Http\Controllers\Api\v1\ArticleController::class, 'info']);
     Route::get('/article/page', [\App\Http\Controllers\Api\v1\ArticleController::class, 'page']);
 
+
+    Route::get('/notice/info', [\App\Http\Controllers\Api\v1\NoticeController::class, 'info']);
     Route::get('/notice/info', [\App\Http\Controllers\Api\v1\NoticeController::class, 'info']);
 
 });