ArticleService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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, 3]);
  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.create_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['content'] = $item['content'] ? str_replace("\n",'<br>',$item['content']) : '';
  93. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  94. }
  95. unset($item);
  96. }
  97. return [
  98. 'code' => 0,
  99. 'success'=> true,
  100. 'msg' => '操作成功',
  101. 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
  102. 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
  103. ];
  104. }
  105. /**
  106. * 访问量
  107. * @return mixed
  108. */
  109. public function updateVisit($userId=0){
  110. $id = request()->get('id');
  111. $cacheKey = "caches:article:visit:{$userId}_{$id}";
  112. $check = RedisService::get($cacheKey);
  113. if($id && !$check){
  114. RedisService::set($cacheKey, $id, 3600);
  115. return $this->model::where(['id'=> $id])->increment('view_num', 1);
  116. }
  117. }
  118. /**
  119. * 获取列表
  120. * @return array
  121. * @since 2020/11/11
  122. * @author wesmiler
  123. */
  124. public function getDataList($params)
  125. {
  126. $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
  127. $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
  128. $dataList = $this->model::from('article as a')
  129. ->leftJoin('article_cates as c', 'a.cate_id', '=', 'c.id')
  130. ->where(function ($query) use ($params) {
  131. $query->where(['a.mark'=>1]);
  132. $status = isset($params['status'])? $params['status'] : 0;
  133. if($status){
  134. $status = is_array($status)? $status : [$status];
  135. $query->whereIn('a.status', $status);
  136. }else{
  137. $query->where('a.status', 1);
  138. }
  139. $keyword = isset($params['keyword']) ? trim($params['keyword']) : '';
  140. $cateId = isset($params['cate_id']) ? intval($params['cate_id']) : 0;
  141. if ($cateId > 0) {
  142. $query->where('a.cate_id', $cateId);
  143. } else if ($cateId==0) {
  144. $query->where('a.is_recommand', 1);
  145. }
  146. $isTop = isset($params['is_top']) ? intval($params['is_top']) : 0;
  147. if ($isTop > 0) {
  148. $query->where('a.is_top', $isTop);
  149. }
  150. $userId = isset($params['user_id']) ? intval($params['user_id']) : 0;
  151. if ($userId > 0) {
  152. $query->where('a.user_id', $userId);
  153. }
  154. $type = isset($params['type']) ? intval($params['type']) : 0;
  155. if ($type > 0) {
  156. $query->where('a.type', $type);
  157. }
  158. })
  159. ->where(function($query) use($params){
  160. $keyword = isset($params['keyword']) ? trim($params['keyword']) : '';
  161. if (!empty($keyword)) {
  162. $query->where('a.title', 'like', "%{$keyword}%")->orWhere('c.name','like',"%{$keyword}%");
  163. }
  164. })
  165. ->select(['a.id', 'a.cate_id','a.user_id', 'c.name as cate_name','a.author', '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'])
  166. ->orderBy('a.sort', 'desc')
  167. ->orderBy('a.create_time', 'desc')
  168. ->paginate($pageSize);
  169. $dataList = $dataList ? $dataList->toArray() : [];
  170. if ($dataList) {
  171. foreach ($dataList['data'] as &$item) {
  172. $item['thumb'] = $item['thumb'] ? get_image_url($item['thumb']) : '';
  173. $item['author'] = $item['author'] ? $item['author'] : '报恩寺';
  174. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  175. $item['publish_at'] = $item['publish_at'] ? $item['publish_at'] : $item['create_at'];
  176. }
  177. unset($item);
  178. }
  179. return [
  180. 'code' => 0,
  181. 'success'=> true,
  182. 'msg' => '操作成功',
  183. 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
  184. 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
  185. ];
  186. }
  187. /**
  188. * 相关推荐列表
  189. * @return array
  190. * @since 2020/11/11
  191. * @author wesmiler
  192. */
  193. public function getRelationList($params)
  194. {
  195. $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
  196. $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
  197. $id = isset($params['id']) ? intval($params['id']) : PERPAGE;
  198. $info = $this->model::from('article as a')
  199. ->leftJoin('article_cates as c', 'a.cate_id', '=', 'c.id')
  200. ->where(['a.id'=> $id,'a.mark'=> 1,'a.status'=> 1])
  201. ->select(['a.id','a.cate_id','a.type'])
  202. ->first();
  203. $dataList = $this->model::from('article as a')
  204. ->leftJoin('article_cates as c', 'a.cate_id', '=', 'c.id')
  205. ->where(function ($query) use ($params, $info) {
  206. $query->where(['a.mark'=>1,'a.status'=> 1]);
  207. $cateId = isset($info['cate_id']) ? intval($info['cate_id']) : 0;
  208. if ($cateId > 0) {
  209. $query->where('a.cate_id', $cateId);
  210. }
  211. $type = isset($info['type']) ? intval($info['type']) : 0;
  212. if ($type > 0) {
  213. $query->where('a.type', $type);
  214. }
  215. })
  216. ->where(function($query) use($params){
  217. $keyword = isset($params['keyword']) ? trim($params['keyword']) : '';
  218. if (!empty($keyword)) {
  219. $query->where('a.title', 'like', "%{$keyword}%")->orWhere('c.name','like',"%{$keyword}%");
  220. }
  221. })
  222. ->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'])
  223. ->orderBy('a.sort', 'desc')
  224. ->orderBy('a.update_time', 'desc')
  225. ->paginate($pageSize);
  226. $dataList = $dataList ? $dataList->toArray() : [];
  227. if(empty($dataList)){
  228. $dataList = $this->model::from('article as a')
  229. ->leftJoin('article_cates as c', 'a.cate_id', '=', 'c.id')
  230. ->where(['a.mark'=>1,'a.status'=> 1])
  231. ->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'])
  232. ->orderBy(\DB::raw('rand()'))
  233. ->orderBy('a.update_time', 'desc')
  234. ->paginate($pageSize);
  235. $dataList = $dataList ? $dataList->toArray() : [];
  236. }
  237. if ($dataList) {
  238. foreach ($dataList['data'] as &$item) {
  239. $item['thumb'] = $item['thumb'] ? get_image_url($item['thumb']) : '';
  240. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  241. $item['publish_at'] = $item['publish_at'] ? $item['publish_at'] : $item['create_at'];
  242. }
  243. unset($item);
  244. }
  245. return [
  246. 'code' => 0,
  247. 'success'=> true,
  248. 'msg' => '操作成功',
  249. 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
  250. 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
  251. ];
  252. }
  253. /**
  254. * 获取详情
  255. * @param $id
  256. */
  257. public function getDetail($id, $userId=0){
  258. $info = $this->model::from('article as a')
  259. ->leftJoin('article_cates as c', 'a.cate_id', '=', 'c.id')
  260. ->where(['a.mark'=> 1,'a.status'=> 1,'a.id'=> $id])
  261. ->select(['a.id','a.title','a.thumb','a.author','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'])
  262. ->first();
  263. $info = $info? $info->toArray() : [];
  264. if($info){
  265. $info['thumb'] = $info['thumb']? get_image_url($info['thumb']) : '';
  266. $info['publish_at'] = $info['publish_at']? $info['publish_at'] : datetime( $info['create_time'],'Y-m-d H:i:s');
  267. $info['content'] = $info['content'] ? str_replace("\n",'<br>',$info['content']) : '';
  268. if(empty($info['author']) && $info['user_id']){
  269. $author = MemberModel::where(['id'=> $info['user_id']])->value('nickname');
  270. $info['author'] = $author? $author : '报恩寺';
  271. }
  272. // 是否已收藏
  273. $info['is_collect'] = 0;
  274. if($userId){
  275. if(CollectModel::where(['user_id'=> $userId,'source_id'=> $id,'type'=> 1,'mark'=> 1,'status'=> 1])->value('id')){
  276. $info['is_collect'] = 1;
  277. }
  278. }
  279. }
  280. return $info;
  281. }
  282. /**
  283. * 添加或编辑
  284. * @return array
  285. * @since 2020/11/11
  286. * @author wesmiler
  287. */
  288. public function edit()
  289. {
  290. $data = request()->all();
  291. // 图片处理
  292. $type = isset($data['type'])? intval($data['type']) : 0;
  293. $image = $data['thumb']? trim($data['thumb']) : '';
  294. $id = isset($data['id']) ? $data['id'] : 0;
  295. if (!$id && !$image && $type == 1) {
  296. return message('请上传封面图片', false);
  297. }
  298. if (strpos($image, "temp")) {
  299. $data['thumb'] = save_image($image, 'item');
  300. } else {
  301. $data['thumb'] = is_array($data['thumb'])? '' : str_replace(IMG_URL, "", $data['thumb']);
  302. }
  303. $data['update_time'] = time();
  304. $data['publish_at'] = isset($data['publish_at']) && $data['publish_at']? $data['publish_at'] : date('Y-m-d H:i:s');
  305. return parent::edit($data); // TODO: Change the autogenerated stub
  306. }
  307. /**
  308. * 发布
  309. * @param $params
  310. * @return array
  311. */
  312. public function send($params){
  313. $data = [
  314. 'type'=> 3,
  315. 'id'=> isset($params['id'])? $params['id'] : 0,
  316. 'user_id'=> isset($params['user_id'])? $params['user_id'] : 0,
  317. 'title'=> isset($params['title'])? $params['title'] : '',
  318. 'author'=> isset($params['author'])? $params['author'] : '',
  319. 'thumb'=> isset($params['thumb'])? $params['thumb'] : '',
  320. 'content'=> isset($params['content'])? $params['content'] : '',
  321. 'create_time'=> time(),
  322. 'mark'=> 1,
  323. 'status'=> 2,
  324. ];
  325. if($data['thumb']){
  326. $data['thumb'] = is_array($data['thumb'])? '' : str_replace(IMG_URL, "", $data['thumb']);
  327. }
  328. $data['update_time'] = time();
  329. $data['publish_at'] = isset($data['publish_at']) && $data['publish_at']? $data['publish_at'] : date('Y-m-d H:i:s');
  330. return parent::edit($data); // TODO: Change the autogenerated stub
  331. }
  332. }