ArticleService.php 9.0 KB

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