ArticleService.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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\Models\SupervisorsConsultRecordsModel;
  16. use App\Services\BaseService;
  17. use App\Services\ConfigService;
  18. use App\Services\RedisService;
  19. /**
  20. * 文章-服务类
  21. * @author laravel开发员
  22. * @since 2020/11/11
  23. * @package App\Services\Api
  24. */
  25. class ArticleService extends BaseService
  26. {
  27. // 静态对象
  28. protected static $instance = null;
  29. /**
  30. * 构造函数
  31. * @author laravel开发员
  32. * @since 2020/11/11
  33. */
  34. public function __construct()
  35. {
  36. $this->model = new ArticleModel();
  37. }
  38. /**
  39. * 静态入口
  40. */
  41. public static function make()
  42. {
  43. if (!self::$instance) {
  44. self::$instance = new static();
  45. }
  46. return self::$instance;
  47. }
  48. /**
  49. * 信息
  50. * @param int $num
  51. * @return array|mixed
  52. */
  53. public function getInfoByType($type)
  54. {
  55. $cacheKey = "caches:article:info_{$type}";
  56. $datas = RedisService::get($cacheKey);
  57. if($datas){
  58. return $datas;
  59. }
  60. $datas = $this->model->where(['type'=>$type,'status'=>1,'mark'=>1])
  61. ->select(['id','title','type','content','status'])
  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.create_time','desc')
  82. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  83. $list = $list? $list->toArray() :[];
  84. if($list){
  85. foreach($list['data'] as &$item){
  86. $item['create_time'] = $item['create_time']? dateFormat($item['create_time']) : '';
  87. $item['cover'] = $item['cover']? get_image_url($item['cover']) : '';
  88. $item['content'] = $item['content']? get_format_content($item['content']) : '';
  89. }
  90. }
  91. return [
  92. 'pageSize'=> $pageSize,
  93. 'total'=>isset($list['total'])? $list['total'] : 0,
  94. 'list'=> isset($list['data'])? $list['data'] : []
  95. ];
  96. }
  97. /**
  98. * 查询
  99. * @param $params
  100. * @return mixed
  101. */
  102. public function getQuery($params)
  103. {
  104. $where = ['a.mark' => 1];
  105. $status = isset($params['status'])? $params['status'] : 0;
  106. $type = isset($params['type'])? $params['type'] : 0;
  107. $cateId = isset($params['cate_id'])? $params['cate_id'] : 0;
  108. if($status>0){
  109. $where['a.status'] = $status;
  110. }
  111. if($type>0){
  112. $where['a.type'] = $type;
  113. }
  114. if($cateId>0){
  115. $where['a.cate_id'] = $cateId;
  116. }
  117. return $this->model->with(['category'])->from('article as a')
  118. ->where($where)
  119. ->where(function ($query) use($params){
  120. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  121. if($keyword){
  122. $query->where('a.title','like',"%{$keyword}%");
  123. }
  124. });
  125. }
  126. /**
  127. * 获取文章详情
  128. * @param $id
  129. * @return array|mixed
  130. */
  131. public function getInfo($id)
  132. {
  133. $cacheKey = "caches:articles:info_{$id}";
  134. $info = RedisService::get($cacheKey);
  135. if($info){
  136. return $info;
  137. }
  138. $info = $this->model->where(['id'=> $id,'status'=>1,'mark'=>1])
  139. ->select(['id','title','cover','type','content_type','author','view_num','cate_id','create_time','description','type','content'])
  140. ->first();
  141. $info = $info? $info->toArray() : [];
  142. if($info){
  143. $info['create_time'] = $info['create_time']? datetime($info['create_time'],'Y-m-d H.i.s') : '';
  144. $info['cover'] = get_image_url($info['cover']);
  145. if($info['content_type'] == 2){
  146. $info['content'] = json_decode(format_content($info['content']),true);
  147. }else{
  148. $info['content'] = get_format_content($info['content']);
  149. }
  150. $this->model->where(['id'=> $id])->increment('view_num',1);
  151. $info['view_num'] += intval($info['view_num']);
  152. RedisService::set($cacheKey, $info, rand(3600,7200));
  153. }
  154. return $info;
  155. }
  156. /**
  157. * 按类型获取文章
  158. * @param int $type
  159. * @return array|mixed
  160. */
  161. public function getListByType($type=1)
  162. {
  163. $cacheKey = "caches:articles:indexList_{$type}";
  164. $datas = RedisService::get($cacheKey);
  165. if($datas){
  166. return $datas;
  167. }
  168. $limit = ConfigService::make()->getConfigByCode('index_article_num',3);
  169. $datas = ArticleCateModel::with(['articles'])->where(['type'=>$type,'status'=>1,'mark'=>1])
  170. ->select(['id','name','sort','type'])
  171. ->get();
  172. $datas = $datas? $datas->toArray() : [];
  173. if($datas){
  174. foreach ($datas as &$item){
  175. $item['articles'] = $item['articles']? array_slice($item['articles'],0,$limit) :[];
  176. }
  177. unset($item);
  178. RedisService::set($cacheKey, $datas, rand(300,600));
  179. }
  180. return $datas;
  181. }
  182. /**
  183. * 获取文章推荐分类
  184. * @param int $type
  185. * @return array|mixed
  186. */
  187. public function getCateList($type=1)
  188. {
  189. $cacheKey = "caches:articles:cateList_{$type}";
  190. $datas = RedisService::get($cacheKey);
  191. if($datas){
  192. return $datas;
  193. }
  194. $datas = ArticleCateModel::where(['type'=> $type,'status'=>1,'mark'=>1])
  195. ->select(['id','name','sort','type'])
  196. ->get();
  197. $datas = $datas? $datas->toArray() : [];
  198. if($datas){
  199. RedisService::set($cacheKey, $datas, rand(300,600));
  200. }
  201. return $datas;
  202. }
  203. /**
  204. * 咨询信息提交
  205. * @param $userId
  206. * @param $params
  207. * @return array|false
  208. */
  209. public function consultSubmit($userId, $params)
  210. {
  211. $sourceId = isset($params['source_id']) ? intval($params['source_id']) : 0;
  212. $realname = isset($params['realname']) ? trim($params['realname']) : '';
  213. $mobile = isset($params['mobile']) ? trim($params['mobile']) : '';
  214. $industry = isset($params['industry']) ? trim($params['industry']) : '';
  215. $position = isset($params['position']) ? trim($params['position']) : '';
  216. if (empty($realname)) {
  217. $this->error = '请填写姓名';
  218. return false;
  219. }
  220. if (empty($mobile)) {
  221. $this->error = '请填写联系方式';
  222. return false;
  223. }
  224. if (empty($industry)) {
  225. $this->error = '请填写所在行业';
  226. return false;
  227. }
  228. if (empty($position)) {
  229. $this->error = '请填写当前角色/职务';
  230. return false;
  231. }
  232. $cacheKey = "caches:articles:consult:{$userId}_{$sourceId}";
  233. if (RedisService::get($cacheKey)) {
  234. $this->error = '您近期已经提交过,请30秒后重试';
  235. return false;
  236. }
  237. $data = [
  238. 'user_id' => $userId,
  239. 'source_id' => $sourceId,
  240. 'realname' => $realname,
  241. 'mobile' => $mobile,
  242. 'industry' => $industry,
  243. 'position' => $position,
  244. 'work_year' => isset($params['work_year'])? trim($params['work_year']) : '',
  245. 'interest' => isset($params['interest'])? trim($params['interest']) : '',
  246. 'experiences' => isset($params['experiences'])? trim($params['experiences']) : '',
  247. 'create_time' => time(),
  248. 'update_time' => time(),
  249. 'status' => 1,
  250. 'mark' => 1,
  251. ];
  252. if (!$id = ArticleConsultRecordsModel::where(['source_id'=>$sourceId,'user_id'=>$userId])->value('id')) {
  253. if(!$id = ArticleConsultRecordsModel::insertGetId($data)){
  254. RedisService::clear($cacheKey);
  255. $this->error = '提交失败';
  256. return false;
  257. }
  258. }else{
  259. ArticleConsultRecordsModel::where(['id'=>$id])->update($data);
  260. }
  261. $this->error = '提交成功';
  262. RedisService::set($cacheKey, $data, 30);
  263. return ['id' => $id];
  264. }
  265. }