ArticleService.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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, $userId=0)
  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. $locale = RedisService::get("caches:locale:lang_{$userId}");
  87. $locale = $locale? $locale : session('locale_lang');
  88. $locale = $locale? $locale :'zh-cn';
  89. foreach($list['data'] as &$item){
  90. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
  91. $item['time_text'] = $item['publish_at']? dateFormat(strtotime($item['publish_at'])) : '';
  92. $item['cover'] = $item['cover']? get_image_url($item['cover']) : '';
  93. $item['file_url'] = $item['file_url']? get_image_url($item['file_url']) : '';
  94. $title = $item['title']? $item['title'] : '';
  95. $content = $item['content']? htmlspecialchars_decode($item['content']) : '';
  96. if($locale != 'zh-cn'){
  97. $item['title'] = isset($item['title_'.$locale]) && $item['title_'.$locale]? $item['title_'.$locale] : '';
  98. $item['content'] = isset($item['content_'.$locale]) && $item['content_'.$locale]? htmlspecialchars_decode($item['content_'.$locale]) : '';
  99. }
  100. $item['title'] = $item['title']? $item['title'] : $title;
  101. $item['content'] = $item['content']? $item['content'] : $content;
  102. }
  103. }
  104. return [
  105. 'pageSize'=> $pageSize,
  106. 'total'=>isset($list['total'])? $list['total'] : 0,
  107. 'list'=> isset($list['data'])? $list['data'] : []
  108. ];
  109. }
  110. /**
  111. * 类目
  112. * @return array[]
  113. */
  114. public function getCateList($type=0)
  115. {
  116. $cacheKey = "caches:articles:cates_{$type}";
  117. $datas = RedisService::get($cacheKey);
  118. if($datas){
  119. return $datas;
  120. }
  121. $where = ['status'=>1,'mark'=>1];
  122. if($type){
  123. $where['type'] = $type;
  124. }
  125. $datas = ArticleCateModel::where($where)
  126. ->select(['id','icon','name','type','status'])
  127. ->orderBy('sort','desc')
  128. ->orderBy('id','desc')
  129. ->get();
  130. if($datas){
  131. foreach ($datas as &$item){
  132. $item['icon'] = $item['icon']? get_image_url($item['icon']) : '';
  133. }
  134. unset($item);
  135. }
  136. return $datas;
  137. }
  138. /**
  139. * 获取文章详情
  140. * @param $id
  141. * @return array|mixed
  142. */
  143. public function getInfo($id, $userId=0, $refresh = false)
  144. {
  145. $cacheKey = "caches:articles:info_{$id}_{$userId}";
  146. $info = RedisService::get($cacheKey);
  147. if($info && !$refresh){
  148. return $info;
  149. }
  150. $locale = RedisService::get("caches:locale:lang_{$userId}");
  151. $locale = $locale? $locale : session('locale_lang');
  152. $locale = $locale? $locale :'zh-cn';
  153. if($locale == 'zh-cn'){
  154. $field = ['id','title','cover','views','author','publish_at','file_url','show_type','type','content'];
  155. }else{
  156. $field = ['id','title as title_all',"title_{$locale} as title",'cover','views','author','publish_at','file_url','show_type','type',"content_{$locale} as content"];
  157. }
  158. $info = $this->model->where(['id'=> $id,'status'=>1,'mark'=>1])
  159. ->where('publish_at','<=', date('Y-m-d H:i:s'))
  160. ->select($field)
  161. ->first();
  162. $info = $info? $info->toArray() : [];
  163. if($info){
  164. $info['time_text'] = $info['publish_at']? datetime(strtotime($info['publish_at'])) : '';
  165. $info['author']= $info['author']? $info['author'] : ConfigService::make()->getConfigByCode('app_name','星链社交');
  166. $info['title'] = $info['title']? $info['title'] : $info['title_all'];
  167. $info['cover'] = get_image_url($info['cover']);
  168. $info['file_url'] = get_image_url($info['file_url']);
  169. $info['content'] = htmlspecialchars_decode($info['content']);
  170. $this->model->where(['id'=> $id])->increment('views',1);
  171. $info['views'] += 1;
  172. RedisService::set($cacheKey, $info, rand(300,600));
  173. }
  174. return $info;
  175. }
  176. /**
  177. * 文章详情
  178. * @param $type
  179. * @return array|mixed
  180. */
  181. public function getInfoByType($type)
  182. {
  183. $cacheKey = "caches:articles:type_{$type}";
  184. $info = RedisService::get($cacheKey);
  185. if($info){
  186. return $info;
  187. }
  188. $id = ConfigService::make()->getConfigByCode('page_'.$type);
  189. if($id>0){
  190. $info = $this->model->where(['id'=> $id,'status'=>1,'mark'=>1])
  191. ->select(['id','title','cover','type','file_url','content'])
  192. ->first();
  193. $info = $info? $info->toArray() : [];
  194. if($info){
  195. $info['cover'] = get_image_url($info['cover']);
  196. $info['file_url'] = get_image_url($info['file_url']);
  197. $info['content'] = $info['content']? str_replace("\n","</br>", $info['content']):'';
  198. $info['content'] = htmlspecialchars_decode($info['content']);
  199. RedisService::set($cacheKey, $info, rand(30,60));
  200. }
  201. }
  202. return $info? $info : [];
  203. }
  204. }