ArticleService.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. * @param $id
  105. * @return array
  106. */
  107. public function getDetail(){
  108. $id = request()->get('id', 0);
  109. if($id<=0){
  110. return message(1006, false);
  111. }
  112. $info = $this->model::from('article as a')
  113. ->leftJoin('article_cates as c','c.id','=','a.cate_id')
  114. ->where(['id'=> $id,'mark'=> 1,'status'=> 1])
  115. ->select(['a.*','c.name as cate_name'])
  116. ->first();
  117. $info = $info? $info->toArray() :[];
  118. if($info){
  119. $info['thumb'] = $info['thumb']? get_image_url($info['thumb']) : '';
  120. $info['create_time'] = $info['create_time']? datetime($info['create_time'], 'Y-m-d H:i:s') : '';
  121. }
  122. return message(1005, true, $info);
  123. }
  124. /**
  125. * 访问量
  126. * @return mixed
  127. */
  128. public function updateVisit($userId=0){
  129. $id = request()->get('id');
  130. $cacheKey = "caches:article:visit:{$userId}_{$id}";
  131. $check = RedisService::get($cacheKey);
  132. if($id && !$check){
  133. RedisService::set($cacheKey, $id, 3600);
  134. return $this->model::where(['id'=> $id])->increment('view_num', 1);
  135. }
  136. }
  137. /**
  138. * 获取列表
  139. * @return array
  140. * @since 2020/11/11
  141. * @author wesmiler
  142. */
  143. public function getDataList($params)
  144. {
  145. $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
  146. $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
  147. $dataList = $this->model::from('article as a')
  148. ->leftJoin('article_cates as c', 'a.cate_id', '=', 'c.id')
  149. ->where(function ($query) use ($params) {
  150. $query->where(['a.mark'=>1,'a.status'=> 1]);
  151. $title = isset($params['title']) ? trim($params['title']) : '';
  152. if (!empty($title)) {
  153. $query->where('a.title', 'like', "%{$title}%");
  154. }
  155. $cateId = isset($params['cate_id']) ? intval($params['cate_id']) : 0;
  156. if ($cateId > 0) {
  157. $query->where('a.cate_id', $cateId);
  158. } else if ($cateId == -1) {
  159. $query->where('a.is_recommand', 1);
  160. }
  161. $isTop = isset($params['is_top']) ? intval($params['is_top']) : 0;
  162. if ($isTop > 0) {
  163. $query->where('a.is_top', $isTop);
  164. }
  165. $type = isset($params['type']) ? intval($params['type']) : 0;
  166. if ($type > 0) {
  167. $query->where('a.type', $type);
  168. }
  169. })
  170. ->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'])
  171. ->orderBy('a.update_time', 'desc')
  172. ->paginate($pageSize);
  173. $dataList = $dataList ? $dataList->toArray() : [];
  174. if ($dataList) {
  175. foreach ($dataList['data'] as &$item) {
  176. $item['thumb'] = $item['thumb'] ? get_image_url($item['thumb']) : '';
  177. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  178. $item['publish_at'] = $item['publish_at'] ? $item['publish_at'] : $item['create_at'];
  179. }
  180. unset($item);
  181. }
  182. return [
  183. 'code' => 0,
  184. 'success'=> true,
  185. 'msg' => '操作成功',
  186. 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
  187. 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
  188. ];
  189. }
  190. /**
  191. * 获取详情
  192. * @param $id
  193. */
  194. public function getDetail($id){
  195. $info = $this->model::from('article as a')
  196. ->leftJoin('article_cates as c', 'a.cate_id', '=', 'c.id')
  197. ->where(['a.mark'=> 1,'a.status'=> 1,'a.id'=> $id])
  198. ->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'])
  199. ->first();
  200. $info = $info? $info->toArray() : [];
  201. if($info){
  202. $info['thumb'] = $info['thumb']? get_image_url($info['thumb']) : '';
  203. $info['publish_at'] = $info['publish_at']? $info['publish_at'] : datetime( $info['create_time'],'Y-m-d H:i:s');
  204. }
  205. return $info;
  206. }
  207. /**
  208. * 添加或编辑
  209. * @return array
  210. * @since 2020/11/11
  211. * @author wesmiler
  212. */
  213. public function edit()
  214. {
  215. $data = request()->all();
  216. // 图片处理
  217. $type = isset($data['type'])? intval($data['type']) : 0;
  218. $image = $data['thumb']? trim($data['thumb']) : '';
  219. $id = isset($data['id']) ? $data['id'] : 0;
  220. if (!$id && !$image && $type == 1) {
  221. return message('请上传封面图片', false);
  222. }
  223. if (strpos($image, "temp")) {
  224. $data['thumb'] = save_image($image, 'item');
  225. } else {
  226. $data['thumb'] = str_replace(IMG_URL, "", $data['thumb']);
  227. }
  228. $data['update_time'] = time();
  229. $data['publish_at'] = isset($data['publish_at']) && $data['publish_at']? $data['publish_at'] : date('Y-m-d H:i:s');
  230. return parent::edit($data); // TODO: Change the autogenerated stub
  231. }
  232. }