123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <?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
- * Class ArticleService
- * @package App\Services\Common
- */
- class ArticleService extends BaseService
- {
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- * ArticleService constructor.
- */
- public function __construct()
- {
- $this->model = new ArticleModel();
- }
- /**
- * 静态入口
- * @return static|null
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = (new static());
- }
- return self::$instance;
- }
- /**
- * @param $params
- * @param int $pageSize
- * @return array
- */
- public function getDataList($params, $pageSize = 15, $userId=0)
- {
- $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;
- }
- if($type>0){
- $where['a.type'] = $type;
- }
- if($publishType>0){
- $where['a.publish_type'] = $publishType;
- }
- if($cateId>0){
- $where['a.cate_id'] = $cateId;
- }
- $list = $this->model->from('article as a')
- ->where($where)
- ->where('publish_at','<=', date('Y-m-d H:i:s'))
- ->where(function ($query) use($params){
- $keyword = isset($params['keyword'])? $params['keyword'] : '';
- if($keyword){
- $query->where('a.title','like',"%{$keyword}%");
- }
- })
- ->select(['a.*'])
- ->orderBy('a.publish_at','desc')
- ->orderBy('a.create_time','desc')
- ->paginate($pageSize > 0 ? $pageSize : 9999999);
- $list = $list? $list->toArray() :[];
- if($list){
- $locale = RedisService::get("caches:locale:lang_{$userId}");
- $locale = $locale? $locale : session('locale_lang');
- $locale = $locale? $locale :'zh-cn';
- foreach($list['data'] as &$item){
- $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
- $item['time_text'] = $item['publish_at']? dateFormat(strtotime($item['publish_at'])) : '';
- $item['cover'] = $item['cover']? get_image_url($item['cover']) : '';
- $item['file_url'] = $item['file_url']? get_image_url($item['file_url']) : '';
- $title = $item['title']? $item['title'] : '';
- $content = $item['content']? htmlspecialchars_decode($item['content']) : '';
- if($locale != 'zh-cn'){
- $item['title'] = isset($item['title_'.$locale]) && $item['title_'.$locale]? $item['title_'.$locale] : '';
- $item['content'] = isset($item['content_'.$locale]) && $item['content_'.$locale]? htmlspecialchars_decode($item['content_'.$locale]) : '';
- }
- $item['title'] = $item['title']? $item['title'] : $title;
- $item['content'] = $item['content']? $item['content'] : $content;
- }
- }
- return [
- 'pageSize'=> $pageSize,
- 'total'=>isset($list['total'])? $list['total'] : 0,
- 'list'=> isset($list['data'])? $list['data'] : []
- ];
- }
- /**
- * 类目
- * @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;
- }
- /**
- * 获取文章详情
- * @param $id
- * @return array|mixed
- */
- public function getInfo($id, $userId=0, $refresh = false)
- {
- $cacheKey = "caches:articles:info_{$id}_{$userId}";
- $info = RedisService::get($cacheKey);
- if($info && !$refresh){
- return $info;
- }
- $locale = RedisService::get("caches:locale:lang_{$userId}");
- $locale = $locale? $locale : session('locale_lang');
- $locale = $locale? $locale :'zh-cn';
- if($locale == 'zh-cn'){
- $field = ['id','title','cover','views','author','publish_at','file_url','show_type','type','content'];
- }else{
- $field = ['id','title as title_all',"title_{$locale} as title",'cover','views','author','publish_at','file_url','show_type','type',"content_{$locale} as content"];
- }
- $info = $this->model->where(['id'=> $id,'status'=>1,'mark'=>1])
- ->where('publish_at','<=', date('Y-m-d H:i:s'))
- ->select($field)
- ->first();
- $info = $info? $info->toArray() : [];
- if($info){
- $info['time_text'] = $info['publish_at']? datetime(strtotime($info['publish_at'])) : '';
- $info['author']= $info['author']? $info['author'] : ConfigService::make()->getConfigByCode('app_name','星链社交');
- $info['title'] = $info['title']? $info['title'] : $info['title_all'];
- $info['cover'] = get_image_url($info['cover']);
- $info['file_url'] = get_image_url($info['file_url']);
- $info['content'] = htmlspecialchars_decode($info['content']);
- $this->model->where(['id'=> $id])->increment('views',1);
- $info['views'] += 1;
- RedisService::set($cacheKey, $info, rand(300,600));
- }
- return $info;
- }
- /**
- * 文章详情
- * @param $type
- * @return array|mixed
- */
- public function getInfoByType($type)
- {
- $cacheKey = "caches:articles:type_{$type}";
- $info = RedisService::get($cacheKey);
- if($info){
- return $info;
- }
- $id = ConfigService::make()->getConfigByCode('page_'.$type);
- if($id>0){
- $info = $this->model->where(['id'=> $id,'status'=>1,'mark'=>1])
- ->select(['id','title','cover','type','file_url','content'])
- ->first();
- $info = $info? $info->toArray() : [];
- if($info){
- $info['cover'] = get_image_url($info['cover']);
- $info['file_url'] = get_image_url($info['file_url']);
- $info['content'] = $info['content']? str_replace("\n","</br>", $info['content']):'';
- $info['content'] = htmlspecialchars_decode($info['content']);
- RedisService::set($cacheKey, $info, rand(30,60));
- }
- }
- return $info? $info : [];
- }
- }
|