ArticleService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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\ArticleConsultRecordsModel;
  14. use App\Models\ArticleModel;
  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','publish_at','content','status'])
  61. ->orderBy('sort','desc')
  62. ->orderBy('publish_at','desc')
  63. ->orderBy('id','desc')
  64. ->first();
  65. $datas = $datas? $datas->toArray() : [];
  66. if($datas){
  67. $datas['content'] = preg_replace("/\n+/",'<br/>',$datas['content']);
  68. $datas['content'] = get_format_content($datas['content']);
  69. RedisService::set($cacheKey, $datas, 86400);
  70. }
  71. return $datas;
  72. }
  73. /**
  74. * @param $params
  75. * @param int $pageSize
  76. * @return array
  77. */
  78. public function getDataList($params, $pageSize = 15)
  79. {
  80. $query = $this->getQuery($params);
  81. $list = $query->select(['a.id','a.type','a.title','a.cover','a.author','a.view_num','a.cate_id','publish_at','a.create_time','a.content','a.status'])
  82. ->orderBy('a.sort','desc')
  83. ->orderBy('a.publish_at','desc')
  84. ->orderBy('a.id','desc')
  85. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  86. $list = $list? $list->toArray() :[];
  87. if($list){
  88. foreach($list['data'] as &$item){
  89. $item['create_time'] = $item['publish_at']? dateFormat($item['publish_at']) : dateFormat($item['create_time']);
  90. $item['cover'] = $item['cover']? get_image_url($item['cover']) : '';
  91. $item['content'] = $item['content']? get_format_content($item['content']) : '';
  92. }
  93. }
  94. return [
  95. 'pageSize'=> $pageSize,
  96. 'total'=>isset($list['total'])? $list['total'] : 0,
  97. 'list'=> isset($list['data'])? $list['data'] : []
  98. ];
  99. }
  100. /**
  101. * 查询
  102. * @param $params
  103. * @return mixed
  104. */
  105. public function getQuery($params)
  106. {
  107. $where = ['a.mark' => 1];
  108. $status = isset($params['status'])? $params['status'] : 0;
  109. $type = isset($params['type'])? $params['type'] : 0;
  110. $cateId = isset($params['cate_id'])? $params['cate_id'] : 0;
  111. if($status>0){
  112. $where['a.status'] = $status;
  113. }else{
  114. $where['a.status'] = 1;
  115. }
  116. if($cateId>0){
  117. $where['a.cate_id'] = $cateId;
  118. }
  119. if($type>0 && $cateId<=0){
  120. $where['a.type'] = $type;
  121. }
  122. return $this->model->with(['category'])->from('article as a')
  123. ->where($where)
  124. ->where(function ($query) use($params){
  125. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  126. if($keyword){
  127. $query->where('a.title','like',"%{$keyword}%");
  128. }
  129. // 协议
  130. $showType = isset($params['show_type'])? $params['show_type'] : 0;
  131. if($showType == 3){
  132. $query->where('a.type','>=',3)->whereNotIn('a.type',[8,9,10]);
  133. }
  134. });
  135. }
  136. /**
  137. * 获取文章详情
  138. * @param $id
  139. * @return array|mixed
  140. */
  141. public function getInfo($id)
  142. {
  143. $cacheKey = "caches:articles:info_{$id}";
  144. $info = RedisService::get($cacheKey);
  145. if($info){
  146. return $info;
  147. }
  148. $info = $this->model->where(['id'=> $id,'status'=>1,'mark'=>1])
  149. ->select(['id','title','cover','type','content_type','author','publish_at','view_num','cate_id','create_time','description','type','content'])
  150. ->first();
  151. $info = $info? $info->toArray() : [];
  152. if($info){
  153. // $info['create_time'] = $info['create_time']? datetime($info['create_time'],'Y-m-d H.i.s') : '';
  154. $info['create_time'] = $info['publish_at']? datetime($info['publish_at'],'Y-m-d H.i.s') : datetime($info['create_time'],'Y-m-d H.i.s');
  155. $info['cover'] = get_image_url($info['cover']);
  156. if($info['content_type'] == 2){
  157. $info['content'] = json_decode(format_content($info['content']),true);
  158. }else{
  159. $info['content'] = get_format_content($info['content']);
  160. }
  161. $this->model->where(['id'=> $id])->increment('view_num',1);
  162. $info['view_num'] += intval($info['view_num']);
  163. RedisService::set($cacheKey, $info, rand(3600,7200));
  164. }
  165. return $info;
  166. }
  167. /**
  168. * 按类型获取文章
  169. * @param int $type
  170. * @return array|mixed
  171. */
  172. public function getListByType($type=1)
  173. {
  174. $cacheKey = "caches:articles:indexList_{$type}";
  175. $datas = RedisService::get($cacheKey);
  176. if($datas){
  177. return $datas;
  178. }
  179. $limit = ConfigService::make()->getConfigByCode('index_article_num',6);
  180. $datas = ArticleCateModel::with(['articles'])->where(['type'=>$type,'status'=>1,'mark'=>1])
  181. ->select(['id','name','sort','type'])
  182. ->orderBy('sort','desc')
  183. ->orderBy('id','desc')
  184. ->get();
  185. $datas = $datas? $datas->toArray() : [];
  186. if($datas){
  187. foreach ($datas as &$item){
  188. $item['articles'] = $item['articles']? array_slice($item['articles'],0,$limit) :[];
  189. }
  190. unset($item);
  191. RedisService::set($cacheKey, $datas, rand(300,600));
  192. }
  193. return $datas;
  194. }
  195. /**
  196. * 获取文章推荐分类
  197. * @param int $type
  198. * @return array|mixed
  199. */
  200. public function getCateList($type=1)
  201. {
  202. $cacheKey = "caches:articles:cateList_{$type}";
  203. $datas = RedisService::get($cacheKey);
  204. if($datas){
  205. return $datas;
  206. }
  207. $datas = ArticleCateModel::where(['type'=> $type,'status'=>1,'mark'=>1])
  208. ->select(['id','name','sort','type'])
  209. ->orderBy('sort','desc')
  210. ->orderBy('id','desc')
  211. ->get();
  212. $datas = $datas? $datas->toArray() : [];
  213. if($datas){
  214. RedisService::set($cacheKey, $datas, rand(300,600));
  215. }
  216. return $datas;
  217. }
  218. /**
  219. * 咨询信息提交
  220. * @param $userId
  221. * @param $params
  222. * @return array|false
  223. */
  224. public function consultSubmit($userId, $params)
  225. {
  226. $sourceId = isset($params['source_id']) ? intval($params['source_id']) : 0;
  227. $realname = isset($params['realname']) ? trim($params['realname']) : '';
  228. $mobile = isset($params['mobile']) ? trim($params['mobile']) : '';
  229. $industry = isset($params['industry']) ? trim($params['industry']) : '';
  230. $position = isset($params['position']) ? trim($params['position']) : '';
  231. if (empty($realname)) {
  232. $this->error = '请填写姓名';
  233. return false;
  234. }
  235. if (empty($mobile)) {
  236. $this->error = '请填写联系方式';
  237. return false;
  238. }
  239. if (empty($industry)) {
  240. $this->error = '请填写所在行业';
  241. return false;
  242. }
  243. if (empty($position)) {
  244. $this->error = '请填写当前角色/职务';
  245. return false;
  246. }
  247. $cacheKey = "caches:articles:consult:{$userId}_{$sourceId}";
  248. if (RedisService::get($cacheKey)) {
  249. $this->error = '您近期已经提交过,请30秒后重试';
  250. return false;
  251. }
  252. $data = [
  253. 'user_id' => $userId,
  254. 'source_id' => $sourceId,
  255. 'realname' => $realname,
  256. 'mobile' => $mobile,
  257. 'industry' => $industry,
  258. 'position' => $position,
  259. 'work_year' => isset($params['work_year'])? trim($params['work_year']) : '',
  260. 'interest' => isset($params['interest'])? trim($params['interest']) : '',
  261. 'experiences' => isset($params['experiences'])? trim($params['experiences']) : '',
  262. 'create_time' => time(),
  263. 'update_time' => time(),
  264. 'status' => 1,
  265. 'mark' => 1,
  266. ];
  267. if (!$id = ArticleConsultRecordsModel::where(['source_id'=>$sourceId,'user_id'=>$userId])->value('id')) {
  268. if(!$id = ArticleConsultRecordsModel::insertGetId($data)){
  269. RedisService::clear($cacheKey);
  270. $this->error = '提交失败';
  271. return false;
  272. }
  273. }else{
  274. ArticleConsultRecordsModel::where(['id'=>$id])->update($data);
  275. }
  276. $this->error = '提交成功';
  277. RedisService::set($cacheKey, $data, 30);
  278. return ['id' => $id];
  279. }
  280. }