ArticleService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Models\ArticleCateModel;
  13. use App\Models\ArticleModel;
  14. use App\Models\ExamAccessLogModel;
  15. use App\Services\BaseService;
  16. use App\Services\ConfigService;
  17. use App\Services\RedisService;
  18. /**
  19. * 文章-服务类
  20. * @author laravel开发员
  21. * @since 2020/11/11
  22. * @package App\Services\Api
  23. */
  24. class ArticleService extends BaseService
  25. {
  26. // 静态对象
  27. protected static $instance = null;
  28. /**
  29. * 构造函数
  30. * @author laravel开发员
  31. * @since 2020/11/11
  32. */
  33. public function __construct()
  34. {
  35. $this->model = new ArticleModel();
  36. }
  37. /**
  38. * 静态入口
  39. */
  40. public static function make()
  41. {
  42. if (!self::$instance) {
  43. self::$instance = new static();
  44. }
  45. return self::$instance;
  46. }
  47. /**
  48. * 信息
  49. * @param int $num
  50. * @return array|mixed
  51. */
  52. public function getInfoByType($type)
  53. {
  54. $cacheKey = "caches:article:info_{$type}";
  55. $datas = RedisService::get($cacheKey);
  56. if($datas){
  57. return $datas;
  58. }
  59. $datas = $this->model->where(['type'=>$type,'status'=>1,'mark'=>1])
  60. ->select(['id','title','type','content','status'])
  61. ->orderBy('create_time','desc')
  62. ->first();
  63. $datas = $datas? $datas->toArray() : [];
  64. if($datas){
  65. $datas['content'] = preg_replace("/\n+/",'<br/>',$datas['content']);
  66. $datas['content'] = get_format_content($datas['content']);
  67. RedisService::set($cacheKey, $datas, 86400);
  68. }
  69. return $datas;
  70. }
  71. /**
  72. * @param $params
  73. * @param int $pageSize
  74. * @return array
  75. */
  76. public function getDataList($params, $pageSize = 15)
  77. {
  78. $page = isset($params['page'])? $params['page'] : 1;
  79. $cacheKey = "caches:articles:list_{$page}_{$pageSize}:".md5(json_encode($params));
  80. $datas = RedisService::get($cacheKey);
  81. if($datas){
  82. return $datas;
  83. }
  84. $query = $this->getQuery($params);
  85. $list = $query->select(['a.id','a.type','a.title','a.cover','a.view_num','a.create_time','a.description','a.status'])
  86. ->orderBy('a.create_time','desc')
  87. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  88. $list = $list? $list->toArray() :[];
  89. if($list){
  90. foreach($list['data'] as &$item){
  91. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d') : '';
  92. $item['cover'] = $item['cover']? get_image_url($item['cover']) : '';
  93. }
  94. }
  95. $rows = isset($list['data'])? $list['data'] : [];
  96. $datas = [
  97. 'pageSize'=> $pageSize,
  98. 'total'=> isset($list['total'])? $list['total'] : 0,
  99. 'list'=> $rows
  100. ];
  101. if($rows){
  102. RedisService::set($cacheKey, $datas, rand(3600, 7200));
  103. }
  104. return $datas;
  105. }
  106. /**
  107. * 查询
  108. * @param $params
  109. * @return mixed
  110. */
  111. public function getQuery($params)
  112. {
  113. $where = ['a.mark' => 1];
  114. $status = isset($params['status'])? $params['status'] : 0;
  115. $type = isset($params['type'])? $params['type'] : 0;
  116. $cateId = isset($params['cate_id'])? $params['cate_id'] : 0;
  117. if($status>0){
  118. $where['a.status'] = $status;
  119. }
  120. if($type>0){
  121. $where['a.type'] = $type;
  122. }
  123. if($cateId>0){
  124. $where['a.cate_id'] = $cateId;
  125. }
  126. return $this->model->from('article as a')
  127. ->where($where)
  128. ->where(function ($query) use($params){
  129. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  130. if($keyword){
  131. $query->where('a.title','like',"%{$keyword}%");
  132. }
  133. });
  134. }
  135. /**
  136. * 热门问答(都在问列表)
  137. * @param $userId
  138. * @param false $refresh
  139. * @return array|mixed
  140. */
  141. public function getHotList($userId,$refresh=false)
  142. {
  143. $cacheKey = "caches:articles:hotList_{$userId}";
  144. $datas = RedisService::get($cacheKey);
  145. if($datas && !$refresh){
  146. return $datas;
  147. }
  148. $model = $this->model->where(['type'=>9,'status'=>1,'mark'=>1]);
  149. $model1 = clone $model;
  150. if($model1->where('view_num','>', 0)->count('id')>5){
  151. $model = $model1;
  152. }
  153. $num = ConfigService::make()->getConfigByCode('custom_hot_num', 3);
  154. $num = $num>=2 && $num<= 20? $num : 3;
  155. $datas = $model->select(['id','cover','type','title','description','status'])
  156. ->orderByRaw('RAND()')
  157. ->take($num)
  158. ->get();
  159. $datas = $datas? $datas->toArray() : [];
  160. if($datas){
  161. RedisService::set($cacheKey, $datas, rand(300, 600));
  162. }
  163. return $datas;
  164. }
  165. /**
  166. * 推荐问答
  167. * @param $userId
  168. * @param false $refresh
  169. * @return array|mixed
  170. */
  171. public function getRecommendList($refresh=false)
  172. {
  173. $cacheKey = "caches:articles:recList";
  174. $datas = RedisService::get($cacheKey);
  175. if($datas && !$refresh){
  176. return $datas;
  177. }
  178. $num = ConfigService::make()->getConfigByCode('custom_rec_num', 3);
  179. $num = $num>=2 && $num<= 20? $num : 3;
  180. $datas = $this->model->where(['type'=>9,'status'=>1,'mark'=>1])
  181. ->select(['id','cover','title','type','description','status'])
  182. ->orderBy('view_num','desc')
  183. ->take($num)
  184. ->get();
  185. $datas = $datas? $datas->toArray() : [];
  186. if($datas){
  187. RedisService::set($cacheKey, $datas, rand(300, 600));
  188. }
  189. return $datas;
  190. }
  191. /**
  192. * 获取文章详情
  193. * @param $id
  194. * @return array|mixed
  195. */
  196. public function getInfo($id)
  197. {
  198. $cacheKey = "caches:articles:info_{$id}";
  199. $info = RedisService::get($cacheKey);
  200. if($info){
  201. return $info;
  202. }
  203. $info = $this->model->where(['id'=> $id,'status'=>1,'mark'=>1])
  204. ->select(['id','title','type','cover','show_type','file_url','view_num','author','description','create_time','type','content'])
  205. ->first();
  206. $info = $info? $info->toArray() : [];
  207. if($info){
  208. $info['create_time'] = $info['create_time']? datetime($info['create_time'],'Y-m-d') : '';
  209. $info['cover'] = get_image_url($info['cover']);
  210. $info['file_url'] = get_image_url($info['file_url']);
  211. $info['content'] = get_format_content($info['content']);
  212. $this->model->where(['id'=> $id])->increment('view_num',1);
  213. $info['view_num'] += intval($info['view_num']);
  214. RedisService::set($cacheKey, $info, rand(5,10));
  215. }
  216. return $info;
  217. }
  218. /**
  219. * 查找或查看
  220. * @param $params
  221. * @return array|false|mixed
  222. */
  223. public function search($params)
  224. {
  225. $cacheKey = "caches:articles:search_".md5(json_encode($params));
  226. $data = RedisService::get($cacheKey);
  227. if($data){
  228. return $data;
  229. }
  230. $id = isset($params['id'])? $params['id'] : 0;
  231. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  232. if(empty($keyword) && $id<=0){
  233. $this->error = '请选择或输入问题';
  234. return false;
  235. }
  236. $where = ['id'=>0,'type'=>9,'status'=>1,'mark'=>1];
  237. if($id){
  238. $where['id'] = $id;
  239. }else{
  240. unset($where['id']);
  241. }
  242. $data = $this->model->where($where)
  243. ->where(function($query) use($keyword){
  244. if($keyword){
  245. $query->where('title', 'like',"%{$keyword}%")->orWhere('content', 'like',"%{$keyword}%");
  246. }
  247. })
  248. ->select(['id','title','description','content','status'])
  249. ->orderByRaw('RAND()')
  250. ->orderBy('sort','desc')
  251. ->first();
  252. $data = $data? $data->toArray() :[];
  253. if($data){
  254. RedisService::set($cacheKey, $data, rand(3,5));
  255. }
  256. return $data;
  257. }
  258. /**
  259. * 获取分类文章推荐
  260. * @param int $cateId 推荐分类ID
  261. * @param int $type 类别:3-普通文章,4-客服回复
  262. * @return array|mixed
  263. */
  264. public function getCustomRecommend($cateId=0, $type=4)
  265. {
  266. $cacheKey = "caches:articles:list_{$cateId}";
  267. $datas = RedisService::get($cacheKey);
  268. if($datas){
  269. return $datas;
  270. }
  271. $limitNum = ConfigService::make()->getConfigByCode('custom_recommend_num', 6);
  272. $limitNum = $limitNum? $limitNum : 6;
  273. $datas = ArticleCateModel::where(function($query) use($cateId){
  274. if($cateId){
  275. $query->where('cate_id', $cateId);
  276. }
  277. })->where(['type'=>$type,'status'=>1,'mark'=>1])
  278. ->select(['id','cate_id','title','description','sort','type','status'])
  279. ->limit($limitNum)
  280. ->orderBy('sort','desc')
  281. ->orderBy('create_time','desc')
  282. ->get();
  283. $datas = $datas? $datas->toArray() : [];
  284. if($datas){
  285. RedisService::set($cacheKey, $datas, rand(300,600));
  286. }
  287. return $datas;
  288. }
  289. /**
  290. * 获取文章推荐分类
  291. * @param int $type 1-普通文章分类
  292. * @return array|mixed
  293. */
  294. public function getIndexList($type=1)
  295. {
  296. $cacheKey = "caches:articles:list_{$type}";
  297. $datas = RedisService::get($cacheKey);
  298. if($datas){
  299. return $datas;
  300. }
  301. $limitNum = ConfigService::make()->getConfigByCode('index_article_num', 6);
  302. $limitNum = $limitNum? $limitNum : 6;
  303. $datas = $this->model::where(['type'=> $type,'status'=>1,'mark'=>1])
  304. ->orderBy('sort','desc')
  305. ->orderBy('id','desc')
  306. ->limit($limitNum)
  307. ->get();
  308. $datas = $datas? $datas->toArray() : [];
  309. if($datas){
  310. RedisService::set($cacheKey, $datas, rand(300,600));
  311. }
  312. return $datas;
  313. }
  314. /**
  315. * 获取文章推荐分类
  316. * @param int $type 2-对口资料分类,3-专升本资料分类
  317. * @return array|mixed
  318. */
  319. public function getCateList($type=2, $sc=0)
  320. {
  321. $cacheKey = "caches:articles:cateList_{$type}";
  322. $datas = RedisService::get($cacheKey);
  323. // 复习资料访问统计
  324. if (empty($sc)) {
  325. ExamAccessLogModel::saveLog(date('Y-m-d'), $type, 5);
  326. }
  327. if($datas){
  328. return $datas;
  329. }
  330. $datas = ArticleCateModel::where(['type'=> $type,'status'=>1,'mark'=>1])
  331. ->select(['id','icon','name','pid','description','type','sort','status'])
  332. ->orderBy('sort','desc')
  333. ->orderBy('id','asc')
  334. ->get();
  335. $datas = $datas? $datas->toArray() : [];
  336. $list = [];
  337. if($datas){
  338. foreach ($datas as $item){
  339. $pid = isset($item['pid'])? $item['pid'] : 0;
  340. $id = isset($item['id'])? $item['id'] : 0;
  341. if($pid>0){
  342. $list[$pid] = isset($list[$pid])? $list[$pid] : [];
  343. $list[$pid]['children'][] = $item;
  344. }else{
  345. $item['children'] = [];
  346. $list[$id] = $item;
  347. }
  348. }
  349. RedisService::set($cacheKey, $list, rand(300,600));
  350. }
  351. return $list;
  352. }
  353. }