ArticleService.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. $page = isset($params['page'])? $params['page'] : 1;
  78. $cacheKey = "caches:articles:list_{$page}_{$pageSize}:".md5(json_encode($params));
  79. $datas = RedisService::get($cacheKey);
  80. if($datas){
  81. return $datas;
  82. }
  83. $query = $this->getQuery($params);
  84. $list = $query->select(['a.id','a.type','a.title','a.cover','a.view_num','a.create_time','a.description','a.status'])
  85. ->orderBy('a.create_time','desc')
  86. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  87. $list = $list? $list->toArray() :[];
  88. if($list){
  89. foreach($list['data'] as &$item){
  90. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d') : '';
  91. $item['cover'] = $item['cover']? get_image_url($item['cover']) : '';
  92. }
  93. }
  94. $rows = isset($list['data'])? $list['data'] : [];
  95. $datas = [
  96. 'pageSize'=> $pageSize,
  97. 'total'=> isset($list['total'])? $list['total'] : 0,
  98. 'list'=> $rows
  99. ];
  100. if($rows){
  101. RedisService::set($cacheKey, $datas, rand(3600, 7200));
  102. }
  103. return $datas;
  104. }
  105. /**
  106. * 查询
  107. * @param $params
  108. * @return mixed
  109. */
  110. public function getQuery($params)
  111. {
  112. $where = ['a.mark' => 1];
  113. $status = isset($params['status'])? $params['status'] : 0;
  114. $type = isset($params['type'])? $params['type'] : 0;
  115. if($status>0){
  116. $where['a.status'] = $status;
  117. }
  118. if($type>0){
  119. $where['a.type'] = $type;
  120. }
  121. return $this->model->from('article as a')
  122. ->where($where)
  123. ->where(function ($query) use($params){
  124. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  125. if($keyword){
  126. $query->where('a.title','like',"%{$keyword}%");
  127. }
  128. });
  129. }
  130. /**
  131. * 获取文章详情
  132. * @param $id
  133. * @return array|mixed
  134. */
  135. public function getInfo($id)
  136. {
  137. $cacheKey = "caches:articles:info_{$id}";
  138. $info = RedisService::get($cacheKey);
  139. if($info){
  140. return $info;
  141. }
  142. $info = $this->model->where(['id'=> $id,'status'=>1,'mark'=>1])
  143. ->select(['id','title','type','cover','view_num','author','description','create_time','type','content'])
  144. ->first();
  145. $info = $info? $info->toArray() : [];
  146. if($info){
  147. $info['create_time'] = $info['create_time']? datetime($info['create_time'],'Y-m-d') : '';
  148. $info['cover'] = get_image_url($info['cover']);
  149. $info['content'] = get_format_content($info['content']);
  150. $this->model->where(['id'=> $id])->increment('view_num',1);
  151. $info['view_num'] += intval($info['view_num']);
  152. RedisService::set($cacheKey, $info, rand(5,10));
  153. }
  154. return $info;
  155. }
  156. /**
  157. * 获取分类文章推荐
  158. * @param int $cateId 推荐分类ID
  159. * @param int $type 类别:3-普通文章,4-客服回复
  160. * @return array|mixed
  161. */
  162. public function getCustomRecommend($cateId=0, $type=4)
  163. {
  164. $cacheKey = "caches:articles:list_{$cateId}";
  165. $datas = RedisService::get($cacheKey);
  166. if($datas){
  167. return $datas;
  168. }
  169. $limitNum = ConfigService::make()->getConfigByCode('custom_recommend_num', 6);
  170. $limitNum = $limitNum? $limitNum : 6;
  171. $datas = ArticleCateModel::where(function($query) use($cateId){
  172. if($cateId){
  173. $query->where('cate_id', $cateId);
  174. }
  175. })->where(['type'=>$type,'status'=>1,'mark'=>1])
  176. ->select(['id','cate_id','title','description','sort','type','status'])
  177. ->limit($limitNum)
  178. ->orderBy('sort','desc')
  179. ->orderBy('create_time','desc')
  180. ->get();
  181. $datas = $datas? $datas->toArray() : [];
  182. if($datas){
  183. RedisService::set($cacheKey, $datas, rand(300,600));
  184. }
  185. return $datas;
  186. }
  187. /**
  188. * 获取文章推荐分类
  189. * @param int $type 1-普通文章分类
  190. * @return array|mixed
  191. */
  192. public function getIndexList($type=1)
  193. {
  194. $cacheKey = "caches:articles:list_{$type}";
  195. $datas = RedisService::get($cacheKey);
  196. if($datas){
  197. return $datas;
  198. }
  199. $limitNum = ConfigService::make()->getConfigByCode('index_article_num', 6);
  200. $limitNum = $limitNum? $limitNum : 6;
  201. $datas = $this->model::where(['type'=> $type,'status'=>1,'mark'=>1])
  202. ->orderBy('sort','desc')
  203. ->orderBy('id','desc')
  204. ->limit($limitNum)
  205. ->get();
  206. $datas = $datas? $datas->toArray() : [];
  207. if($datas){
  208. RedisService::set($cacheKey, $datas, rand(300,600));
  209. }
  210. return $datas;
  211. }
  212. /**
  213. * 获取文章推荐分类
  214. * @param int $type 1-普通文章分类
  215. * @return array|mixed
  216. */
  217. public function getCateList($type=2)
  218. {
  219. $cacheKey = "caches:articles:cateList_{$type}";
  220. $datas = RedisService::get($cacheKey);
  221. if($datas){
  222. return $datas;
  223. }
  224. $limitNum = ConfigService::make()->getConfigByCode('custom_cate_num', 6);
  225. $limitNum = $limitNum? $limitNum : 6;
  226. $datas = ArticleCateModel::where(['type'=> $type,'status'=>1,'mark'=>1])
  227. ->select(['cate_id','name','sort','type'])
  228. ->limit($limitNum)
  229. ->get();
  230. $datas = $datas? $datas->toArray() : [];
  231. if($datas){
  232. RedisService::set($cacheKey, $datas, rand(300,600));
  233. }
  234. return $datas;
  235. }
  236. }