| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- 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+/",'<br/>',$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',[8,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];
- }
- }
|