| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- <?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\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)
- {
- $query = $this->getQuery($params);
- $list = $query->select(['a.id','a.type','a.title','a.cover','a.views','a.cate_id','a.create_time','a.content','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']? dateFormat($item['create_time']) : '';
- $item['cover'] = $item['cover']? get_image_url($item['cover']) : '';
- $item['content'] = $item['content']? get_format_content($item['content']) : '';
- }
- }
- return [
- 'pageSize'=> $pageSize,
- 'total'=>isset($list['total'])? $list['total'] : 0,
- 'list'=> isset($list['data'])? $list['data'] : []
- ];
- }
- /**
- * 查询
- * @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->with(['category'])->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 $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','cover','views','cate_id','create_time','type','content'])
- ->first();
- $info = $info? $info->toArray() : [];
- if($info){
- $info['create_time'] = $info['create_time']? datetime($info['create_time'],'Y-m-d H.i.s') : '';
- $info['cover'] = get_image_url($info['cover']);
- $info['content'] = get_format_content($info['content']);
- $this->model->where(['id'=> $id])->increment('views',1);
- $info['views'] += intval($info['views']);
- RedisService::set($cacheKey, $info, rand(3600,7200));
- }
- return $info;
- }
- /**
- * 按类型获取文章
- * @param int $type
- * @return array|mixed
- */
- public function getListByType($type=1)
- {
- $cacheKey = "caches:articles:indexList_{$type}";
- $datas = RedisService::get($cacheKey);
- if($datas){
- return $datas;
- }
- $limit = ConfigService::make()->getConfigByCode('index_article_num',3);
- $datas = ArticleCateModel::with(['articles'])->where(['type'=>$type,'status'=>1,'mark'=>1])
- ->select(['id','name','sort','type'])
- ->get();
- $datas = $datas? $datas->toArray() : [];
- if($datas){
- foreach ($datas as &$item){
- $item['articles'] = $item['articles']? array_slice($item['articles'],0,$limit) :[];
- }
- unset($item);
- RedisService::set($cacheKey, $datas, rand(300,600));
- }
- return $datas;
- }
- /**
- * 获取文章推荐分类
- * @param int $type
- * @return array|mixed
- */
- public function getCateList($type=1)
- {
- $cacheKey = "caches:articles:cateList_{$type}";
- $datas = RedisService::get($cacheKey);
- if($datas){
- return $datas;
- }
- $datas = ArticleCateModel::where(['type'=> $type,'status'=>1,'mark'=>1])
- ->select(['cate_id','name','sort','type'])
- ->get();
- $datas = $datas? $datas->toArray() : [];
- if($datas){
- RedisService::set($cacheKey, $datas, rand(300,600));
- }
- return $datas;
- }
- }
|