ArticleService.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Laravel框架 [ Laravel ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 Laravel研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: wesmiler <12345678@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services;
  12. use App\Models\ArticleModel;
  13. /**
  14. * 文章管理-服务类
  15. * @author wesmiler
  16. * @since 2020/11/11
  17. * Class ArticleService
  18. * @package App\Services
  19. */
  20. class ArticleService extends BaseService
  21. {
  22. protected static $instance = null;
  23. /**
  24. * 构造函数
  25. * @author wesmiler
  26. * @since 2020/11/11
  27. * ArticleService constructor.
  28. */
  29. public function __construct()
  30. {
  31. $this->model = new ArticleModel();
  32. }
  33. /**
  34. * 静态入口
  35. * @return ArticleService|null
  36. */
  37. public static function make(){
  38. if(!self::$instance){
  39. self::$instance = new ArticleService();
  40. }
  41. return self::$instance;
  42. }
  43. /**
  44. * 获取列表
  45. * @return array
  46. * @since 2020/11/11
  47. * @author wesmiler
  48. */
  49. public function getList()
  50. {
  51. $params = request()->all();
  52. $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
  53. $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
  54. $dataList = $this->model::from('article as a')
  55. ->leftJoin('article_cates as c', 'a.cate_id', '=', 'c.id')
  56. ->where(function ($query) use ($params) {
  57. $query->where('a.mark', 1);
  58. $title = isset($params['title']) ? trim($params['title']) : '';
  59. if (!empty($title)) {
  60. $query->where('a.title', 'like', "%{$title}%");
  61. }
  62. $cateId = isset($params['cate_id']) ? intval($params['cate_id']) : 0;
  63. if ($cateId > 0) {
  64. $query->where('a.cate_id', $cateId);
  65. } else if ($cateId == -1) {
  66. $query->where('a.is_recommand', 1);
  67. }
  68. $isTop = isset($params['is_top']) ? intval($params['is_top']) : 0;
  69. if ($isTop > 0) {
  70. $query->where('a.is_top', $isTop);
  71. }
  72. $type = isset($params['type']) ? intval($params['type']) : 0;
  73. if ($type > 0) {
  74. $query->where('a.type', $type);
  75. }
  76. $status = isset($params['status']) ? $params['status'] : 0;
  77. if ($status > 0) {
  78. $query->where('a.status', $status);
  79. } else {
  80. $query->whereIn('a.status', [1, 2]);
  81. }
  82. })
  83. ->select(['a.id', 'a.cate_id', 'c.name as cate_name', 'a.title', 'a.is_form', 'a.is_recommand', 'a.view_num', 'a.thumb', 'a.status', 'a.create_time', 'a.update_time', 'a.description', 'a.sort', 'a.content','a.publish_at'])
  84. ->orderBy('a.update_time', 'desc')
  85. ->paginate($pageSize);
  86. $dataList = $dataList ? $dataList->toArray() : [];
  87. if ($dataList) {
  88. foreach ($dataList['data'] as &$item) {
  89. $item['thumb'] = $item['thumb'] ? get_image_url($item['thumb']) : '';
  90. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  91. }
  92. unset($item);
  93. }
  94. return [
  95. 'code' => 0,
  96. 'success'=> true,
  97. 'msg' => '操作成功',
  98. 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
  99. 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
  100. ];
  101. }
  102. /**
  103. * 访问量
  104. * @return mixed
  105. */
  106. public function updateVisit($userId=0){
  107. $id = request()->get('id');
  108. $cacheKey = "caches:article:visit:{$userId}_{$id}";
  109. $check = RedisService::get($cacheKey);
  110. if($id && !$check){
  111. RedisService::set($cacheKey, $id, 3600);
  112. return $this->model::where(['id'=> $id])->increment('view_num', 1);
  113. }
  114. }
  115. /**
  116. * 获取列表
  117. * @return array
  118. * @since 2020/11/11
  119. * @author wesmiler
  120. */
  121. public function getDataList($params)
  122. {
  123. $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
  124. $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
  125. $dataList = $this->model::from('article as a')
  126. ->leftJoin('article_cates as c', 'a.cate_id', '=', 'c.id')
  127. ->where(function ($query) use ($params) {
  128. $query->where(['a.mark'=>1,'a.status'=> 1]);
  129. $title = isset($params['title']) ? trim($params['title']) : '';
  130. if (!empty($title)) {
  131. $query->where('a.title', 'like', "%{$title}%");
  132. }
  133. $cateId = isset($params['cate_id']) ? intval($params['cate_id']) : 0;
  134. if ($cateId > 0) {
  135. $query->where('a.cate_id', $cateId);
  136. } else if ($cateId == -1) {
  137. $query->where('a.is_recommand', 1);
  138. }
  139. $isTop = isset($params['is_top']) ? intval($params['is_top']) : 0;
  140. if ($isTop > 0) {
  141. $query->where('a.is_top', $isTop);
  142. }
  143. $type = isset($params['type']) ? intval($params['type']) : 0;
  144. if ($type > 0) {
  145. $query->where('a.type', $type);
  146. }
  147. })
  148. ->select(['a.id', 'a.cate_id', 'c.name as cate_name', 'a.title', 'a.is_form', 'a.is_recommand', 'a.view_num', 'a.thumb', 'a.status', 'a.create_time', 'a.update_time', 'a.description', 'a.sort','a.publish_at'])
  149. ->orderBy('a.update_time', 'desc')
  150. ->paginate($pageSize);
  151. $dataList = $dataList ? $dataList->toArray() : [];
  152. if ($dataList) {
  153. foreach ($dataList['data'] as &$item) {
  154. $item['thumb'] = $item['thumb'] ? get_image_url($item['thumb']) : '';
  155. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  156. $item['publish_at'] = $item['publish_at'] ? $item['publish_at'] : $item['create_at'];
  157. }
  158. unset($item);
  159. }
  160. return [
  161. 'code' => 0,
  162. 'success'=> true,
  163. 'msg' => '操作成功',
  164. 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
  165. 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
  166. ];
  167. }
  168. /**
  169. * 获取详情
  170. * @param $id
  171. */
  172. public function getDetail($id){
  173. $info = $this->model::from('article as a')
  174. ->leftJoin('article_cates as c', 'a.cate_id', '=', 'c.id')
  175. ->where(['a.mark'=> 1,'a.status'=> 1,'a.id'=> $id])
  176. ->select(['a.id','a.title','a.thumb','a.cate_id','c.name as cate_name','a.view_num','a.description','a.content','a.publish_at','a.create_time'])
  177. ->first();
  178. $info = $info? $info->toArray() : [];
  179. if($info){
  180. $info['thumb'] = $info['thumb']? get_image_url($info['thumb']) : '';
  181. $info['publish_at'] = $info['publish_at']? $info['publish_at'] : datetime( $info['create_time'],'Y-m-d H:i:s');
  182. }
  183. return $info;
  184. }
  185. /**
  186. * 添加或编辑
  187. * @return array
  188. * @since 2020/11/11
  189. * @author wesmiler
  190. */
  191. public function edit()
  192. {
  193. $data = request()->all();
  194. // 图片处理
  195. $type = isset($data['type'])? intval($data['type']) : 0;
  196. $image = $data['thumb']? trim($data['thumb']) : '';
  197. $id = isset($data['id']) ? $data['id'] : 0;
  198. if (!$id && !$image && $type == 1) {
  199. return message('请上传封面图片', false);
  200. }
  201. if (strpos($image, "temp")) {
  202. $data['thumb'] = save_image($image, 'item');
  203. } else {
  204. $data['thumb'] = str_replace(IMG_URL, "", $data['thumb']);
  205. }
  206. $data['update_time'] = time();
  207. $data['publish_at'] = isset($data['publish_at']) && $data['publish_at']? $data['publish_at'] : date('Y-m-d H:i:s');
  208. return parent::edit($data); // TODO: Change the autogenerated stub
  209. }
  210. }