| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Api;
- use App\Models\ArticleCateModel;
- use App\Models\ArticleModel;
- use App\Models\ExamAccessLogModel;
- use App\Services\BaseService;
- use App\Services\ConfigService;
- use App\Services\RedisService;
- /**
- * 文章-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * @package App\Services\Api
- */
- class ArticleService extends BaseService
- {
- // 静态对象
- protected static $instance = null;
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- */
- public function __construct()
- {
- $this->model = new ArticleModel();
- }
- /**
- * 静态入口
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = new static();
- }
- return self::$instance;
- }
- /**
- * 信息
- * @param int $num
- * @return array|mixed
- */
- public function getInfoByType($type)
- {
- $cacheKey = "caches:article:info_{$type}";
- $datas = RedisService::get($cacheKey);
- if($datas){
- return $datas;
- }
- $datas = $this->model->where(['type'=>$type,'status'=>1,'mark'=>1])
- ->select(['id','title','type','content','status'])
- ->orderBy('create_time','desc')
- ->first();
- $datas = $datas? $datas->toArray() : [];
- if($datas){
- $datas['content'] = preg_replace("/\n+/",'<br/>',$datas['content']);
- $datas['content'] = get_format_content($datas['content']);
- RedisService::set($cacheKey, $datas, 86400);
- }
- return $datas;
- }
- /**
- * @param $params
- * @param int $pageSize
- * @return array
- */
- public function getDataList($params, $pageSize = 15)
- {
- $page = isset($params['page'])? $params['page'] : 1;
- $cacheKey = "caches:articles:list_{$page}_{$pageSize}:".md5(json_encode($params));
- $datas = RedisService::get($cacheKey);
- if($datas){
- return $datas;
- }
- $query = $this->getQuery($params);
- $list = $query->select(['a.id','a.type','a.title','a.cover','a.view_num','a.create_time','a.description','a.status'])
- ->orderBy('a.create_time','desc')
- ->paginate($pageSize > 0 ? $pageSize : 9999999);
- $list = $list? $list->toArray() :[];
- if($list){
- foreach($list['data'] as &$item){
- $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d') : '';
- $item['cover'] = $item['cover']? get_image_url($item['cover']) : '';
- }
- }
- $rows = isset($list['data'])? $list['data'] : [];
- $datas = [
- 'pageSize'=> $pageSize,
- 'total'=> isset($list['total'])? $list['total'] : 0,
- 'list'=> $rows
- ];
- if($rows){
- RedisService::set($cacheKey, $datas, rand(3600, 7200));
- }
- return $datas;
- }
- /**
- * 查询
- * @param $params
- * @return mixed
- */
- public function getQuery($params)
- {
- $where = ['a.mark' => 1];
- $status = isset($params['status'])? $params['status'] : 0;
- $type = isset($params['type'])? $params['type'] : 0;
- $cateId = isset($params['cate_id'])? $params['cate_id'] : 0;
- if($status>0){
- $where['a.status'] = $status;
- }
- if($type>0){
- $where['a.type'] = $type;
- }
- if($cateId>0){
- $where['a.cate_id'] = $cateId;
- }
- return $this->model->from('article as a')
- ->where($where)
- ->where(function ($query) use($params){
- $keyword = isset($params['keyword'])? $params['keyword'] : '';
- if($keyword){
- $query->where('a.title','like',"%{$keyword}%");
- }
- });
- }
- /**
- * 热门问答(都在问列表)
- * @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
- */
- public function getInfo($id)
- {
- $cacheKey = "caches:articles:info_{$id}";
- $info = RedisService::get($cacheKey);
- if($info){
- return $info;
- }
- $info = $this->model->where(['id'=> $id,'status'=>1,'mark'=>1])
- ->select(['id','title','type','cover','show_type','file_url','view_num','author','description','create_time','type','content'])
- ->first();
- $info = $info? $info->toArray() : [];
- if($info){
- $info['create_time'] = $info['create_time']? datetime($info['create_time'],'Y-m-d') : '';
- $info['cover'] = get_image_url($info['cover']);
- $info['file_url'] = get_image_url($info['file_url']);
- $info['content'] = get_format_content($info['content']);
- $this->model->where(['id'=> $id])->increment('view_num',1);
- $info['view_num'] += intval($info['view_num']);
- RedisService::set($cacheKey, $info, rand(5,10));
- }
- return $info;
- }
- /**
- * 查找或查看
- * @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-客服回复
- * @return array|mixed
- */
- public function getCustomRecommend($cateId=0, $type=4)
- {
- $cacheKey = "caches:articles:list_{$cateId}";
- $datas = RedisService::get($cacheKey);
- if($datas){
- return $datas;
- }
- $limitNum = ConfigService::make()->getConfigByCode('custom_recommend_num', 6);
- $limitNum = $limitNum? $limitNum : 6;
- $datas = ArticleCateModel::where(function($query) use($cateId){
- if($cateId){
- $query->where('cate_id', $cateId);
- }
- })->where(['type'=>$type,'status'=>1,'mark'=>1])
- ->select(['id','cate_id','title','description','sort','type','status'])
- ->limit($limitNum)
- ->orderBy('sort','desc')
- ->orderBy('create_time','desc')
- ->get();
- $datas = $datas? $datas->toArray() : [];
- if($datas){
- RedisService::set($cacheKey, $datas, rand(300,600));
- }
- return $datas;
- }
- /**
- * 获取文章推荐分类
- * @param int $type 1-普通文章分类
- * @return array|mixed
- */
- public function getIndexList($type=1)
- {
- $cacheKey = "caches:articles:list_{$type}";
- $datas = RedisService::get($cacheKey);
- if($datas){
- return $datas;
- }
- $limitNum = ConfigService::make()->getConfigByCode('index_article_num', 6);
- $limitNum = $limitNum? $limitNum : 6;
- $datas = $this->model::where(['type'=> $type,'status'=>1,'mark'=>1])
- ->orderBy('sort','desc')
- ->orderBy('id','desc')
- ->limit($limitNum)
- ->get();
- $datas = $datas? $datas->toArray() : [];
- if($datas){
- RedisService::set($cacheKey, $datas, rand(300,600));
- }
- return $datas;
- }
- /**
- * 获取文章推荐分类
- * @param int $type 2-对口资料分类,3-专升本资料分类
- * @return array|mixed
- */
- public function getCateList($type=2, $sc=0)
- {
- $cacheKey = "caches:articles:cateList_{$type}";
- $datas = RedisService::get($cacheKey);
- // 复习资料访问统计
- if (empty($sc)) {
- ExamAccessLogModel::saveLog(date('Y-m-d'), $type, 5);
- }
- if($datas){
- return $datas;
- }
- $datas = ArticleCateModel::where(['type'=> $type,'status'=>1,'mark'=>1])
- ->select(['id','icon','name','pid','description','type','sort','status'])
- ->orderBy('sort','desc')
- ->orderBy('id','asc')
- ->get();
- $datas = $datas? $datas->toArray() : [];
- $list = [];
- if($datas){
- foreach ($datas as $item){
- $pid = isset($item['pid'])? $item['pid'] : 0;
- $id = isset($item['id'])? $item['id'] : 0;
- if($pid>0){
- $list[$pid] = isset($list[$pid])? $list[$pid] : [];
- $list[$pid]['children'][] = $item;
- }else{
- $item['children'] = [];
- $list[$id] = $item;
- }
- }
- RedisService::set($cacheKey, $list, rand(300,600));
- }
- return $list;
- }
- }
|