|
|
@@ -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-客服回复
|