ArticleService.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Models\ArticleCateModel;
  13. use App\Models\ArticleModel;
  14. use App\Services\BaseService;
  15. use App\Services\ConfigService;
  16. use App\Services\RedisService;
  17. /**
  18. * 文章-服务类
  19. * @author laravel开发员
  20. * @since 2020/11/11
  21. * @package App\Services\Api
  22. */
  23. class ArticleService extends BaseService
  24. {
  25. // 静态对象
  26. protected static $instance = null;
  27. /**
  28. * 构造函数
  29. * @author laravel开发员
  30. * @since 2020/11/11
  31. */
  32. public function __construct()
  33. {
  34. $this->model = new ArticleModel();
  35. }
  36. /**
  37. * 静态入口
  38. */
  39. public static function make()
  40. {
  41. if (!self::$instance) {
  42. self::$instance = new static();
  43. }
  44. return self::$instance;
  45. }
  46. /**
  47. * 信息
  48. * @param int $num
  49. * @return array|mixed
  50. */
  51. public function getInfoByType($type)
  52. {
  53. $cacheKey = "caches:article:info_{$type}";
  54. $datas = RedisService::get($cacheKey);
  55. if($datas){
  56. return $datas;
  57. }
  58. $datas = $this->model->where(['type'=>$type,'status'=>1,'mark'=>1])
  59. ->select(['id','title','type','content','status'])
  60. ->orderBy('create_time','desc')
  61. ->first();
  62. $datas = $datas? $datas->toArray() : [];
  63. if($datas){
  64. $datas['content'] = preg_replace("/\n+/",'<br/>',$datas['content']);
  65. $datas['content'] = get_format_content($datas['content']);
  66. RedisService::set($cacheKey, $datas, 86400);
  67. }
  68. return $datas;
  69. }
  70. /**
  71. * @param $params
  72. * @param int $pageSize
  73. * @return array
  74. */
  75. public function getDataList($params, $pageSize = 15)
  76. {
  77. $query = $this->getQuery($params);
  78. $list = $query->select(['a.id','a.type','a.title','a.cover','a.author','a.view_num','a.cate_id','a.create_time','a.content','a.status'])
  79. ->orderBy('a.create_time','desc')
  80. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  81. $list = $list? $list->toArray() :[];
  82. if($list){
  83. foreach($list['data'] as &$item){
  84. $item['create_time'] = $item['create_time']? dateFormat($item['create_time']) : '';
  85. $item['cover'] = $item['cover']? get_image_url($item['cover']) : '';
  86. $item['content'] = $item['content']? get_format_content($item['content']) : '';
  87. }
  88. }
  89. return [
  90. 'pageSize'=> $pageSize,
  91. 'total'=>isset($list['total'])? $list['total'] : 0,
  92. 'list'=> isset($list['data'])? $list['data'] : []
  93. ];
  94. }
  95. /**
  96. * 查询
  97. * @param $params
  98. * @return mixed
  99. */
  100. public function getQuery($params)
  101. {
  102. $where = ['a.mark' => 1];
  103. $status = isset($params['status'])? $params['status'] : 0;
  104. $type = isset($params['type'])? $params['type'] : 0;
  105. $cateId = isset($params['cate_id'])? $params['cate_id'] : 0;
  106. if($status>0){
  107. $where['a.status'] = $status;
  108. }
  109. if($type>0){
  110. $where['a.type'] = $type;
  111. }
  112. if($cateId>0){
  113. $where['a.cate_id'] = $cateId;
  114. }
  115. return $this->model->with(['category'])->from('article as a')
  116. ->where($where)
  117. ->where(function ($query) use($params){
  118. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  119. if($keyword){
  120. $query->where('a.title','like',"%{$keyword}%");
  121. }
  122. });
  123. }
  124. /**
  125. * 获取文章详情
  126. * @param $id
  127. * @return array|mixed
  128. */
  129. public function getInfo($id)
  130. {
  131. $cacheKey = "caches:articles:info_{$id}";
  132. $info = RedisService::get($cacheKey);
  133. if($info){
  134. return $info;
  135. }
  136. $info = $this->model->where(['id'=> $id,'status'=>1,'mark'=>1])
  137. ->select(['id','title','cover','type','content_type','author','view_num','cate_id','create_time','description','type','content'])
  138. ->first();
  139. $info = $info? $info->toArray() : [];
  140. if($info){
  141. $info['create_time'] = $info['create_time']? datetime($info['create_time'],'Y-m-d H.i.s') : '';
  142. $info['cover'] = get_image_url($info['cover']);
  143. if($info['content_type'] == 2){
  144. $info['content'] = json_decode(format_content($info['content']),true);
  145. }else{
  146. $info['content'] = get_format_content($info['content']);
  147. }
  148. $this->model->where(['id'=> $id])->increment('view_num',1);
  149. $info['view_num'] += intval($info['view_num']);
  150. RedisService::set($cacheKey, $info, rand(3600,7200));
  151. }
  152. return $info;
  153. }
  154. /**
  155. * 按类型获取文章
  156. * @param int $type
  157. * @return array|mixed
  158. */
  159. public function getListByType($type=1)
  160. {
  161. $cacheKey = "caches:articles:indexList_{$type}";
  162. $datas = RedisService::get($cacheKey);
  163. if($datas){
  164. return $datas;
  165. }
  166. $limit = ConfigService::make()->getConfigByCode('index_article_num',3);
  167. $datas = ArticleCateModel::with(['articles'])->where(['type'=>$type,'status'=>1,'mark'=>1])
  168. ->select(['id','name','sort','type'])
  169. ->get();
  170. $datas = $datas? $datas->toArray() : [];
  171. if($datas){
  172. foreach ($datas as &$item){
  173. $item['articles'] = $item['articles']? array_slice($item['articles'],0,$limit) :[];
  174. }
  175. unset($item);
  176. RedisService::set($cacheKey, $datas, rand(300,600));
  177. }
  178. return $datas;
  179. }
  180. /**
  181. * 获取文章推荐分类
  182. * @param int $type
  183. * @return array|mixed
  184. */
  185. public function getCateList($type=1)
  186. {
  187. $cacheKey = "caches:articles:cateList_{$type}";
  188. $datas = RedisService::get($cacheKey);
  189. if($datas){
  190. return $datas;
  191. }
  192. $datas = ArticleCateModel::where(['type'=> $type,'status'=>1,'mark'=>1])
  193. ->select(['id','name','sort','type'])
  194. ->get();
  195. $datas = $datas? $datas->toArray() : [];
  196. if($datas){
  197. RedisService::set($cacheKey, $datas, rand(300,600));
  198. }
  199. return $datas;
  200. }
  201. }