ArticleService.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. * Class ArticleService
  22. * @package App\Services\Common
  23. */
  24. class ArticleService extends BaseService
  25. {
  26. /**
  27. * 构造函数
  28. * @author laravel开发员
  29. * @since 2020/11/11
  30. * ArticleService constructor.
  31. */
  32. public function __construct()
  33. {
  34. $this->model = new ArticleModel();
  35. }
  36. /**
  37. * 静态入口
  38. * @return static|null
  39. */
  40. public static function make()
  41. {
  42. if (!self::$instance) {
  43. self::$instance = (new static());
  44. }
  45. return self::$instance;
  46. }
  47. /**
  48. * @param $params
  49. * @param int $pageSize
  50. * @return array
  51. */
  52. public function getDataList($params, $pageSize = 15)
  53. {
  54. $where = ['a.mark' => 1];
  55. $status = isset($params['status'])? $params['status'] : 0;
  56. $type = isset($params['type'])? $params['type'] : 0;
  57. $publishType = isset($params['publish_type'])? $params['publish_type'] : 0;
  58. $cateId = isset($params['cate_id'])? $params['cate_id'] : 0;
  59. if($status>0){
  60. $where['a.status'] = $status;
  61. }
  62. if($type>0){
  63. $where['a.type'] = $type;
  64. }
  65. if($publishType>0){
  66. $where['a.publish_type'] = $publishType;
  67. }
  68. if($cateId>0){
  69. $where['a.cate_id'] = $cateId;
  70. }
  71. $list = $this->model->from('article as a')
  72. ->where($where)
  73. ->where('publish_at','<=', date('Y-m-d H:i:s'))
  74. ->where(function ($query) use($params){
  75. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  76. if($keyword){
  77. $query->where('a.title','like',"%{$keyword}%");
  78. }
  79. })
  80. ->select(['a.*'])
  81. ->orderBy('a.publish_at','desc')
  82. ->orderBy('a.create_time','desc')
  83. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  84. $list = $list? $list->toArray() :[];
  85. if($list){
  86. foreach($list['data'] as &$item){
  87. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
  88. $item['time_text'] = $item['publish_at']? dateFormat(strtotime($item['publish_at'])) : '';
  89. $item['cover'] = $item['cover']? get_image_url($item['cover']) : '';
  90. $item['file_url'] = $item['file_url']? get_image_url($item['file_url']) : '';
  91. $item['content'] = $item['content']? htmlspecialchars_decode($item['content']) : '';
  92. }
  93. }
  94. return [
  95. 'pageSize'=> $pageSize,
  96. 'total'=>isset($list['total'])? $list['total'] : 0,
  97. 'list'=> isset($list['data'])? $list['data'] : []
  98. ];
  99. }
  100. /**
  101. * 类目
  102. * @return array[]
  103. */
  104. public function getCateList($type=0)
  105. {
  106. $cacheKey = "caches:articles:cates_{$type}";
  107. $datas = RedisService::get($cacheKey);
  108. if($datas){
  109. return $datas;
  110. }
  111. $where = ['status'=>1,'mark'=>1];
  112. if($type){
  113. $where['type'] = $type;
  114. }
  115. $datas = ArticleCateModel::where($where)
  116. ->select(['id','icon','name','type','status'])
  117. ->orderBy('sort','desc')
  118. ->orderBy('id','desc')
  119. ->get();
  120. if($datas){
  121. foreach ($datas as &$item){
  122. $item['icon'] = $item['icon']? get_image_url($item['icon']) : '';
  123. }
  124. unset($item);
  125. }
  126. return $datas;
  127. }
  128. /**
  129. * 获取文章详情
  130. * @param $id
  131. * @return array|mixed
  132. */
  133. public function getInfo($id, $userId=0, $refresh = false)
  134. {
  135. $cacheKey = "caches:articles:info_{$id}_{$userId}";
  136. $info = RedisService::get($cacheKey);
  137. if($info && !$refresh){
  138. return $info;
  139. }
  140. $locale = RedisService::get("caches:locale:lang_{$userId}");
  141. $locale = $locale? $locale : session('locale_lang');
  142. $locale = $locale? $locale :'zh-cn';
  143. if($locale == 'zh-cn'){
  144. $field = ['id','title','cover','views','author','publish_at','file_url','type','content'];
  145. }else{
  146. $field = ['id',"title_{$locale} as title",'cover','views','author','publish_at','file_url','type',"content_{$locale} as content"];
  147. }
  148. $info = $this->model->where(['id'=> $id,'status'=>1,'mark'=>1])
  149. ->where('publish_at','<=', date('Y-m-d H:i:s'))
  150. ->select($field)
  151. ->first();
  152. $info = $info? $info->toArray() : [];
  153. if($info){
  154. $info['time_text'] = $info['publish_at']? dateFormat(strtotime($info['publish_at'])) : '';
  155. $info['cover'] = get_image_url($info['cover']);
  156. $info['file_url'] = get_image_url($info['file_url']);
  157. $info['content'] = htmlspecialchars_decode($info['content']);
  158. $this->model->where(['id'=> $id])->increment('views',1);
  159. $info['views'] += 1;
  160. RedisService::set($cacheKey, $info, rand(300,600));
  161. }
  162. return $info;
  163. }
  164. /**
  165. * 文章详情
  166. * @param $type
  167. * @return array|mixed
  168. */
  169. public function getInfoByType($type)
  170. {
  171. $cacheKey = "caches:articles:type_{$type}";
  172. $info = RedisService::get($cacheKey);
  173. if($info){
  174. return $info;
  175. }
  176. $id = ConfigService::make()->getConfigByCode('page_'.$type);
  177. if($id>0){
  178. $info = $this->model->where(['id'=> $id,'status'=>1,'mark'=>1])
  179. ->select(['id','title','cover','type','file_url','content'])
  180. ->first();
  181. $info = $info? $info->toArray() : [];
  182. if($info){
  183. $info['cover'] = get_image_url($info['cover']);
  184. $info['file_url'] = get_image_url($info['file_url']);
  185. $info['content'] = $info['content']? str_replace("\n","</br>", $info['content']):'';
  186. $info['content'] = htmlspecialchars_decode($info['content']);
  187. RedisService::set($cacheKey, $info, rand(30,60));
  188. }
  189. }
  190. return $info? $info : [];
  191. }
  192. }