ArticleService.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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.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']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
  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. if($status>0){
  106. $where['a.status'] = $status;
  107. }
  108. if($type>0){
  109. $where['a.type'] = $type;
  110. }
  111. return $this->model->from('article as a')
  112. ->where($where)
  113. ->where(function ($query) use($params){
  114. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  115. if($keyword){
  116. $query->where('a.title','like',"%{$keyword}%");
  117. }
  118. });
  119. }
  120. /**
  121. * 获取文章详情
  122. * @param $id
  123. * @return array|mixed
  124. */
  125. public function getInfo($id)
  126. {
  127. $cacheKey = "caches:articles:info_{$id}";
  128. $info = RedisService::get($cacheKey);
  129. if($info){
  130. return $info;
  131. }
  132. $info = $this->model->where(['id'=> $id,'status'=>1,'mark'=>1])
  133. ->select(['id','title','cover','views','create_time','type','content'])
  134. ->first();
  135. $info = $info? $info->toArray() : [];
  136. if($info){
  137. $info['create_time'] = $info['create_time']? datetime($info['create_time'],'Y-m-d H.i.s') : '';
  138. $info['cover'] = get_image_url($info['cover']);
  139. $info['content'] = get_format_content($info['content']);
  140. $this->model->where(['id'=> $id])->increment('views',1);
  141. $info['views'] += intval($info['views']);
  142. RedisService::set($cacheKey, $info, rand(3600,7200));
  143. }
  144. return $info;
  145. }
  146. /**
  147. * 获取分类文章推荐
  148. * @param int $cateId 推荐分类ID
  149. * @param int $type 类别:3-普通文章,4-客服回复
  150. * @return array|mixed
  151. */
  152. public function getCustomRecommend($cateId=0, $type=4)
  153. {
  154. $cacheKey = "caches:articles:list_{$cateId}";
  155. $datas = RedisService::get($cacheKey);
  156. if($datas){
  157. return $datas;
  158. }
  159. $limitNum = ConfigService::make()->getConfigByCode('custom_recommend_num', 6);
  160. $limitNum = $limitNum? $limitNum : 6;
  161. $datas = ArticleCateModel::where(function($query) use($cateId){
  162. if($cateId){
  163. $query->where('cate_id', $cateId);
  164. }
  165. })->where(['type'=>$type,'status'=>1,'mark'=>1])
  166. ->select(['id','cate_id','title','description','sort','type','status'])
  167. ->limit($limitNum)
  168. ->orderBy('sort','desc')
  169. ->orderBy('create_time','desc')
  170. ->get();
  171. $datas = $datas? $datas->toArray() : [];
  172. if($datas){
  173. RedisService::set($cacheKey, $datas, rand(300,600));
  174. }
  175. return $datas;
  176. }
  177. /**
  178. * 获取文章推荐分类
  179. * @param int $type 1-普通文章分类,2-客服回复文章分类
  180. * @return array|mixed
  181. */
  182. public function getCateList($type=2)
  183. {
  184. $cacheKey = "caches:articles:cateList_{$type}";
  185. $datas = RedisService::get($cacheKey);
  186. if($datas){
  187. return $datas;
  188. }
  189. $limitNum = ConfigService::make()->getConfigByCode('custom_cate_num', 6);
  190. $limitNum = $limitNum? $limitNum : 6;
  191. $datas = ArticleCateModel::where(['type'=> $type,'status'=>1,'mark'=>1])
  192. ->select(['cate_id','name','sort','type'])
  193. ->limit($limitNum)
  194. ->get();
  195. $datas = $datas? $datas->toArray() : [];
  196. if($datas){
  197. RedisService::set($cacheKey, $datas, rand(300,600));
  198. }
  199. return $datas;
  200. }
  201. }