// +---------------------------------------------------------------------- namespace App\Services\Api; use App\Models\ArticleCateModel; use App\Models\ArticleConsultRecordsModel; use App\Models\ArticleModel; use App\Services\BaseService; use App\Services\ConfigService; use App\Services\RedisService; /** * 文章-服务类 * @author laravel开发员 * @since 2020/11/11 * @package App\Services\Api */ class ArticleService extends BaseService { // 静态对象 protected static $instance = null; /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 */ public function __construct() { $this->model = new ArticleModel(); } /** * 静态入口 */ public static function make() { if (!self::$instance) { self::$instance = new static(); } return self::$instance; } /** * 信息 * @param int $num * @return array|mixed */ public function getInfoByType($type) { $cacheKey = "caches:article:info_{$type}"; $datas = RedisService::get($cacheKey); if($datas){ return $datas; } $datas = $this->model->where(['type'=>$type,'status'=>1,'mark'=>1]) ->select(['id','title','type','publish_at','content','status']) ->orderBy('sort','desc') ->orderBy('publish_at','desc') ->orderBy('id','desc') ->first(); $datas = $datas? $datas->toArray() : []; if($datas){ $datas['content'] = preg_replace("/\n+/",'
',$datas['content']); $datas['content'] = get_format_content($datas['content']); RedisService::set($cacheKey, $datas, 86400); } return $datas; } /** * @param $params * @param int $pageSize * @return array */ public function getDataList($params, $pageSize = 15) { $query = $this->getQuery($params); $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']) ->orderBy('a.sort','desc') ->orderBy('a.publish_at','desc') ->orderBy('a.id','desc') ->paginate($pageSize > 0 ? $pageSize : 9999999); $list = $list? $list->toArray() :[]; if($list){ foreach($list['data'] as &$item){ $item['create_time'] = $item['publish_at']? dateFormat($item['publish_at']) : dateFormat($item['create_time']); $item['cover'] = $item['cover']? get_image_url($item['cover']) : ''; $item['content'] = $item['content']? get_format_content($item['content']) : ''; } } return [ 'pageSize'=> $pageSize, 'total'=>isset($list['total'])? $list['total'] : 0, 'list'=> isset($list['data'])? $list['data'] : [] ]; } /** * 查询 * @param $params * @return mixed */ public function getQuery($params) { $where = ['a.mark' => 1]; $status = isset($params['status'])? $params['status'] : 0; $type = isset($params['type'])? $params['type'] : 0; $cateId = isset($params['cate_id'])? $params['cate_id'] : 0; if($status>0){ $where['a.status'] = $status; } if($cateId>0){ $where['a.cate_id'] = $cateId; } if($type>0 && $cateId<=0){ $where['a.type'] = $type; } return $this->model->with(['category'])->from('article as a') ->where($where) ->where(function ($query) use($params){ $keyword = isset($params['keyword'])? $params['keyword'] : ''; if($keyword){ $query->where('a.title','like',"%{$keyword}%"); } // 协议 $showType = isset($params['show_type'])? $params['show_type'] : 0; if($showType == 3){ $query->where('a.type','>=',3)->whereNotIn('a.type',[9,10]); } }); } /** * 获取文章详情 * @param $id * @return array|mixed */ public function getInfo($id) { $cacheKey = "caches:articles:info_{$id}"; $info = RedisService::get($cacheKey); if($info){ return $info; } $info = $this->model->where(['id'=> $id,'status'=>1,'mark'=>1]) ->select(['id','title','cover','type','content_type','author','publish_at','view_num','cate_id','create_time','description','type','content']) ->first(); $info = $info? $info->toArray() : []; if($info){ // $info['create_time'] = $info['create_time']? datetime($info['create_time'],'Y-m-d H.i.s') : ''; $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'); $info['cover'] = get_image_url($info['cover']); if($info['content_type'] == 2){ $info['content'] = json_decode(format_content($info['content']),true); }else{ $info['content'] = get_format_content($info['content']); } $this->model->where(['id'=> $id])->increment('view_num',1); $info['view_num'] += intval($info['view_num']); RedisService::set($cacheKey, $info, rand(3600,7200)); } return $info; } /** * 按类型获取文章 * @param int $type * @return array|mixed */ public function getListByType($type=1) { $cacheKey = "caches:articles:indexList_{$type}"; $datas = RedisService::get($cacheKey); if($datas){ return $datas; } $limit = ConfigService::make()->getConfigByCode('index_article_num',6); $datas = ArticleCateModel::with(['articles'])->where(['type'=>$type,'status'=>1,'mark'=>1]) ->select(['id','name','sort','type']) ->orderBy('sort','desc') ->orderBy('id','desc') ->get(); $datas = $datas? $datas->toArray() : []; if($datas){ foreach ($datas as &$item){ $item['articles'] = $item['articles']? array_slice($item['articles'],0,$limit) :[]; } unset($item); RedisService::set($cacheKey, $datas, rand(300,600)); } return $datas; } /** * 获取文章推荐分类 * @param int $type * @return array|mixed */ public function getCateList($type=1) { $cacheKey = "caches:articles:cateList_{$type}"; $datas = RedisService::get($cacheKey); if($datas){ return $datas; } $datas = ArticleCateModel::where(['type'=> $type,'status'=>1,'mark'=>1]) ->select(['id','name','sort','type']) ->orderBy('sort','desc') ->orderBy('id','desc') ->get(); $datas = $datas? $datas->toArray() : []; if($datas){ RedisService::set($cacheKey, $datas, rand(300,600)); } return $datas; } /** * 咨询信息提交 * @param $userId * @param $params * @return array|false */ public function consultSubmit($userId, $params) { $sourceId = isset($params['source_id']) ? intval($params['source_id']) : 0; $realname = isset($params['realname']) ? trim($params['realname']) : ''; $mobile = isset($params['mobile']) ? trim($params['mobile']) : ''; $industry = isset($params['industry']) ? trim($params['industry']) : ''; $position = isset($params['position']) ? trim($params['position']) : ''; if (empty($realname)) { $this->error = '请填写姓名'; return false; } if (empty($mobile)) { $this->error = '请填写联系方式'; return false; } if (empty($industry)) { $this->error = '请填写所在行业'; return false; } if (empty($position)) { $this->error = '请填写当前角色/职务'; return false; } $cacheKey = "caches:articles:consult:{$userId}_{$sourceId}"; if (RedisService::get($cacheKey)) { $this->error = '您近期已经提交过,请30秒后重试'; return false; } $data = [ 'user_id' => $userId, 'source_id' => $sourceId, 'realname' => $realname, 'mobile' => $mobile, 'industry' => $industry, 'position' => $position, 'work_year' => isset($params['work_year'])? trim($params['work_year']) : '', 'interest' => isset($params['interest'])? trim($params['interest']) : '', 'experiences' => isset($params['experiences'])? trim($params['experiences']) : '', 'create_time' => time(), 'update_time' => time(), 'status' => 1, 'mark' => 1, ]; if (!$id = ArticleConsultRecordsModel::where(['source_id'=>$sourceId,'user_id'=>$userId])->value('id')) { if(!$id = ArticleConsultRecordsModel::insertGetId($data)){ RedisService::clear($cacheKey); $this->error = '提交失败'; return false; } }else{ ArticleConsultRecordsModel::where(['id'=>$id])->update($data); } $this->error = '提交成功'; RedisService::set($cacheKey, $data, 30); return ['id' => $id]; } }