ArticleService.php 10.0 KB

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