ArticleService.php 14 KB

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