ArticleService.php 9.5 KB

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