ArticleService.php 9.6 KB

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