ArticleService.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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.views','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','views','cate_id','create_time','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. $info['content'] = get_format_content($info['content']);
  144. $this->model->where(['id'=> $id])->increment('views',1);
  145. $info['views'] += intval($info['views']);
  146. RedisService::set($cacheKey, $info, rand(3600,7200));
  147. }
  148. return $info;
  149. }
  150. /**
  151. * 按类型获取文章
  152. * @param int $type
  153. * @return array|mixed
  154. */
  155. public function getListByType($type=1)
  156. {
  157. $cacheKey = "caches:articles:indexList_{$type}";
  158. $datas = RedisService::get($cacheKey);
  159. if($datas){
  160. return $datas;
  161. }
  162. $limit = ConfigService::make()->getConfigByCode('index_article_num',3);
  163. $datas = ArticleCateModel::with(['articles'])->where(['type'=>$type,'status'=>1,'mark'=>1])
  164. ->select(['id','name','sort','type'])
  165. ->get();
  166. $datas = $datas? $datas->toArray() : [];
  167. if($datas){
  168. foreach ($datas as &$item){
  169. $item['articles'] = $item['articles']? array_slice($item['articles'],0,$limit) :[];
  170. }
  171. unset($item);
  172. RedisService::set($cacheKey, $datas, rand(300,600));
  173. }
  174. return $datas;
  175. }
  176. /**
  177. * 获取文章推荐分类
  178. * @param int $type
  179. * @return array|mixed
  180. */
  181. public function getCateList($type=1)
  182. {
  183. $cacheKey = "caches:articles:cateList_{$type}";
  184. $datas = RedisService::get($cacheKey);
  185. if($datas){
  186. return $datas;
  187. }
  188. $datas = ArticleCateModel::where(['type'=> $type,'status'=>1,'mark'=>1])
  189. ->select(['cate_id','name','sort','type'])
  190. ->get();
  191. $datas = $datas? $datas->toArray() : [];
  192. if($datas){
  193. RedisService::set($cacheKey, $datas, rand(300,600));
  194. }
  195. return $datas;
  196. }
  197. }