ArticleService.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. $keyword = isset($params['keyword']) ? trim($params['keyword']) : '';
  132. $cateId = isset($params['cate_id']) ? intval($params['cate_id']) : 0;
  133. if ($cateId > 0) {
  134. $query->where('a.cate_id', $cateId);
  135. } else if ($keyword=='') {
  136. $query->where('a.is_recommand', 1);
  137. }
  138. $isTop = isset($params['is_top']) ? intval($params['is_top']) : 0;
  139. if ($isTop > 0) {
  140. $query->where('a.is_top', $isTop);
  141. }
  142. $type = isset($params['type']) ? intval($params['type']) : 0;
  143. if ($type > 0) {
  144. $query->where('a.type', $type);
  145. }
  146. })
  147. ->where(function($query) use($params){
  148. $keyword = isset($params['keyword']) ? trim($params['keyword']) : '';
  149. if (!empty($keyword)) {
  150. $query->where('a.title', 'like', "%{$keyword}%")->orWhere('c.name','like',"%{$keyword}%");
  151. }
  152. })
  153. ->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'])
  154. ->orderBy('a.sort', 'desc')
  155. ->orderBy('a.update_time', 'desc')
  156. ->paginate($pageSize);
  157. $dataList = $dataList ? $dataList->toArray() : [];
  158. if ($dataList) {
  159. foreach ($dataList['data'] as &$item) {
  160. $item['thumb'] = $item['thumb'] ? get_image_url($item['thumb']) : '';
  161. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  162. $item['publish_at'] = $item['publish_at'] ? $item['publish_at'] : $item['create_at'];
  163. }
  164. unset($item);
  165. }
  166. return [
  167. 'code' => 0,
  168. 'success'=> true,
  169. 'msg' => '操作成功',
  170. 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
  171. 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
  172. ];
  173. }
  174. /**
  175. * 获取详情
  176. * @param $id
  177. */
  178. public function getDetail($id, $userId=0){
  179. $info = $this->model::from('article as a')
  180. ->leftJoin('article_cates as c', 'a.cate_id', '=', 'c.id')
  181. ->where(['a.mark'=> 1,'a.status'=> 1,'a.id'=> $id])
  182. ->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'])
  183. ->first();
  184. $info = $info? $info->toArray() : [];
  185. if($info){
  186. $info['thumb'] = $info['thumb']? get_image_url($info['thumb']) : '';
  187. $info['publish_at'] = $info['publish_at']? $info['publish_at'] : datetime( $info['create_time'],'Y-m-d H:i:s');
  188. $info['author'] = '报恩寺';
  189. if($info['user_id']){
  190. $author = MemberModel::where(['id'=> $info['user_id']])->value('nickname');
  191. $info['author'] = $author? $author : '报恩寺';
  192. }
  193. // 是否已收藏
  194. $info['is_collect'] = 0;
  195. if($userId){
  196. if(CollectModel::where(['user_id'=> $userId,'source_id'=> $id,'type'=> 1,'mark'=> 1,'status'=> 1])->value('id')){
  197. $info['is_collect'] = 1;
  198. }
  199. }
  200. }
  201. return $info;
  202. }
  203. /**
  204. * 添加或编辑
  205. * @return array
  206. * @since 2020/11/11
  207. * @author wesmiler
  208. */
  209. public function edit()
  210. {
  211. $data = request()->all();
  212. // 图片处理
  213. $type = isset($data['type'])? intval($data['type']) : 0;
  214. $image = $data['thumb']? trim($data['thumb']) : '';
  215. $id = isset($data['id']) ? $data['id'] : 0;
  216. if (!$id && !$image && $type == 1) {
  217. return message('请上传封面图片', false);
  218. }
  219. if (strpos($image, "temp")) {
  220. $data['thumb'] = save_image($image, 'item');
  221. } else {
  222. $data['thumb'] = is_array($data['thumb'])? '' : str_replace(IMG_URL, "", $data['thumb']);
  223. }
  224. $data['update_time'] = time();
  225. $data['publish_at'] = isset($data['publish_at']) && $data['publish_at']? $data['publish_at'] : date('Y-m-d H:i:s');
  226. var_dump($data);
  227. return false;
  228. return parent::edit($data); // TODO: Change the autogenerated stub
  229. }
  230. }