ArticleService.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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']? get_format_content($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. * @return array[]
  131. */
  132. public function getCategory()
  133. {
  134. $datas = [
  135. ["id"=>2,'name'=>'账号安全','icon'=>'/images/icons/icon-safe.png'],
  136. ["id"=>3,'name'=>'物流查询','icon'=>'/images/icons/icon-delivery.png'],
  137. ["id"=>4,'name'=>'投诉卖家','icon'=>'/images/icons/icon-complaint.png'],
  138. ["id"=>5,'name'=>'服务进度','icon'=>'/images/icons/icon-service.png'],
  139. ["id"=>6,'name'=>'催促发货','icon'=>'/images/icons/icon-send.png'],
  140. ["id"=>7,'name'=>'常见问题','icon'=>'/images/icons/icon-question.png'],
  141. ["id"=>10,'name'=>'帮助教程','icon'=>'/images/icons/icon-question.png'],
  142. ];
  143. return $datas;
  144. }
  145. /**
  146. * 获取文章详情
  147. * @param $id
  148. * @return array|mixed
  149. */
  150. public function getInfo($id, $userId=0)
  151. {
  152. $cacheKey = "caches:articles:info_{$id}_{$userId}";
  153. $info = RedisService::get($cacheKey);
  154. if($info){
  155. return $info;
  156. }
  157. $info = $this->model->where(['id'=> $id,'status'=>1,'mark'=>1])
  158. ->where('publish_at','<=', date('Y-m-d H:i:s'))
  159. ->select(['id','title','cover','views','author','publish_at','file_url','show_type','type','content'])
  160. ->first();
  161. $info = $info? $info->toArray() : [];
  162. if($info){
  163. $info['time_text'] = $info['publish_at']? dateFormat(strtotime($info['publish_at'])) : '';
  164. $info['cover'] = get_image_url($info['cover']);
  165. $info['file_url'] = get_image_url($info['file_url']);
  166. $info['content'] = get_format_content($info['content']);
  167. $this->model->where(['id'=> $id])->increment('views',1);
  168. $info['views'] += 1;
  169. RedisService::set($cacheKey, $info, rand(300,600));
  170. }
  171. return $info;
  172. }
  173. /**
  174. * 文章详情
  175. * @param $type
  176. * @return array|mixed
  177. */
  178. public function getInfoByType($type)
  179. {
  180. $cacheKey = "caches:articles:type_{$type}";
  181. $info = RedisService::get($cacheKey);
  182. if($info){
  183. return $info;
  184. }
  185. $id = ConfigService::make()->getConfigByCode('page_'.$type);
  186. if($id>0){
  187. $info = $this->model->where(['id'=> $id,'status'=>1,'mark'=>1])
  188. ->select(['id','title','cover','type','show_type','file_url','content'])
  189. ->first();
  190. $info = $info? $info->toArray() : [];
  191. if($info){
  192. $info['cover'] = get_image_url($info['cover']);
  193. $info['file_url'] = get_image_url($info['file_url']);
  194. $info['content'] = $info['content']? str_replace("\n","</br>", $info['content']):'';
  195. $info['content'] = get_format_content($info['content']);
  196. RedisService::set($cacheKey, $info, rand(30,60));
  197. }
  198. }
  199. return $info? $info : [];
  200. }
  201. }