// +---------------------------------------------------------------------- namespace App\Services\Api; use App\Models\ArticleCateModel; use App\Models\ArticleModel; use App\Models\ExamAccessLogModel; use App\Services\BaseService; use App\Services\ConfigService; use App\Services\RedisService; use PhpOffice\PhpWord\IOFactory; /** * 文章-服务类 * @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','content','status']) ->orderBy('create_time','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) { $page = isset($params['page'])? $params['page'] : 1; $cacheKey = "caches:articles:list_{$page}_{$pageSize}:".md5(json_encode($params)); $datas = RedisService::get($cacheKey); if($datas){ return $datas; } $query = $this->getQuery($params); $list = $query->select(['a.id','a.type','a.title','a.cover','a.view_num','a.show_type','a.create_time','a.description','a.status']) ->orderBy('a.create_time','desc') ->paginate($pageSize > 0 ? $pageSize : 9999999); $list = $list? $list->toArray() :[]; if($list){ foreach($list['data'] as &$item){ $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d') : ''; $item['cover'] = $item['cover']? get_image_url($item['cover']) : ''; } } $rows = isset($list['data'])? $list['data'] : []; $datas = [ 'pageSize'=> $pageSize, 'total'=> isset($list['total'])? $list['total'] : 0, 'list'=> $rows ]; if($rows){ RedisService::set($cacheKey, $datas, rand(3600, 7200)); } return $datas; } /** * 查询 * @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; $answerType = isset($params['answer_type'])? $params['answer_type'] : 0; $cateId = isset($params['cate_id'])? $params['cate_id'] : 0; if($status>0){ $where['a.status'] = $status; } if($type>0){ $where['a.type'] = $type; } if($answerType>0){ $where['a.answer_type'] = $answerType; } if($cateId>=0){ $where['a.cate_id'] = $cateId; } return $this->model->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}%"); } }); } /** * 热门问答(都在问列表) * @param $userId * @param $answerType 问答类型:1-职高,3-专升本 * @param false $refresh * @return array|mixed */ public function getHotList($userId,$answerType=1,$refresh=false) { $cacheKey = "caches:articles:hotList_{$userId}_{$answerType}"; $datas = RedisService::get($cacheKey); if($datas && !$refresh){ return $datas; } $model = $this->model->where(['type'=>9,'answer_type'=>$answerType,'status'=>1,'mark'=>1]); $model1 = clone $model; if($model1->where('view_num','>', 0)->count('id')>5){ $model = $model1; } $num = ConfigService::make()->getConfigByCode('custom_hot_num', 3); $num = $num>=2 && $num<= 20? $num : 3; $datas = $model->select(['id','cover','type','answer_type','title','description','status']) ->orderByRaw('RAND()') ->take($num) ->get(); $datas = $datas? $datas->toArray() : []; if($datas){ RedisService::set($cacheKey, $datas, rand(300, 600)); } return $datas; } /** * 推荐问答 * @param $answerType * @param false $refresh * @return array|mixed */ public function getRecommendList($answerType=1,$refresh=false) { $cacheKey = "caches:articles:recList_{$answerType}"; $datas = RedisService::get($cacheKey); if($datas && !$refresh){ return $datas; } $num = ConfigService::make()->getConfigByCode('custom_rec_num', 3); $num = $num>=2 && $num<= 20? $num : 3; $datas = $this->model->where(['type'=>9,'answer_type'=>$answerType,'status'=>1,'mark'=>1]) ->select(['id','cover','title','answer_type','type','description','status']) ->orderBy('view_num','desc') ->take($num) ->get(); $datas = $datas? $datas->toArray() : []; if($datas){ RedisService::set($cacheKey, $datas, rand(300, 600)); } return $datas; } /** * 获取文章详情 * @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','type','answer_type','cover','show_type','file_url','view_num','author','description','create_time','type','content']) ->first(); $info = $info? $info->toArray() : []; if($info){ $info['create_time'] = $info['create_time']? datetime($info['create_time'],'Y-m-d') : ''; $info['cover'] = get_image_url($info['cover']); $info['file_url'] = get_image_url($info['file_url']); if($info['show_type']==2){ if($content = $this->convertDocxToHtml($info['file_url'])){ $info['file_content'] = $content; }else{ $info['file_content'] = $this->error; } } $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(5,10)); } return $info; } /** * @param $inputFile * @return false|string * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception */ public function convertDocxToHtml($inputFile) { $filePath = get_image_path($inputFile); $outputFile = str_replace('.docx', '.html', $filePath); $outputFile = str_replace('.doc', '.html', $outputFile); $filePath = ATTACHMENT_PATH.get_image_path($filePath); // 检查文件是否存在 if (!file_exists($filePath)) { $this->error = '解析文档不存在,请确认已成功上传'; return false; } if (file_exists(ATTACHMENT_PATH.$outputFile)) { return file_get_contents(ATTACHMENT_PATH.$outputFile); } try { // 加载文档 $phpWord = IOFactory::load($filePath); //保存为HTML $htmlWriter = IOFactory::createWriter($phpWord, 'HTML'); $htmlWriter->save(ATTACHMENT_PATH.$outputFile); if (file_exists(ATTACHMENT_PATH.$outputFile)) { return file_get_contents(ATTACHMENT_PATH.$outputFile); } else { $this->error = '解析文档失败'; return false; } }catch (\Exception $exception){ $this->error = '解析文档失败:'.$exception->getMessage(); return false; } } /** * 查找或查看 * @param $params * @return array|false|mixed */ public function search($params) { $cacheKey = "caches:articles:search_".md5(json_encode($params)); $data = RedisService::get($cacheKey); if($data){ return $data; } $id = isset($params['id'])? $params['id'] : 0; $answerType = isset($params['answer_type']) && $params['answer_type']? $params['answer_type'] : 1; $keyword = isset($params['keyword'])? $params['keyword'] : ''; if(empty($keyword) && $id<=0){ $this->error = '请选择或输入问题'; return false; } $where = ['id'=>0,'type'=>9,'answer_type'=>$answerType,'status'=>1,'mark'=>1]; if($id){ $where['id'] = $id; }else{ unset($where['id']); } $data = $this->model->where($where) ->where(function($query) use($keyword){ if($keyword){ $query->where('title', 'like',"%{$keyword}%")->orWhere('content', 'like',"%{$keyword}%"); } }) ->select(['id','title','type','answer_type','description','content','status']) ->orderByRaw('RAND()') ->orderBy('sort','desc') ->first(); $data = $data? $data->toArray() :[]; if($data){ RedisService::set($cacheKey, $data, rand(3,5)); } return $data; } /** * 获取分类文章推荐 * @param int $cateId 推荐分类ID * @param int $type 类别:3-普通文章,4-客服回复 * @return array|mixed */ public function getCustomRecommend($cateId=0, $type=4) { $cacheKey = "caches:articles:list_{$cateId}"; $datas = RedisService::get($cacheKey); if($datas){ return $datas; } $limitNum = ConfigService::make()->getConfigByCode('custom_recommend_num', 6); $limitNum = $limitNum? $limitNum : 6; $datas = ArticleCateModel::where(function($query) use($cateId){ if($cateId){ $query->where('cate_id', $cateId); } })->where(['type'=>$type,'status'=>1,'mark'=>1]) ->select(['id','cate_id','title','description','sort','type','status']) ->limit($limitNum) ->orderBy('sort','desc') ->orderBy('create_time','desc') ->get(); $datas = $datas? $datas->toArray() : []; if($datas){ RedisService::set($cacheKey, $datas, rand(300,600)); } return $datas; } /** * 获取文章推荐分类 * @param int $type 1-普通文章分类 * @return array|mixed */ public function getIndexList($type=1) { $cacheKey = "caches:articles:list_{$type}"; $datas = RedisService::get($cacheKey); if($datas){ return $datas; } $limitNum = ConfigService::make()->getConfigByCode('index_article_num', 6); $limitNum = $limitNum? $limitNum : 6; $datas = $this->model::where(['type'=> $type,'status'=>1,'mark'=>1]) ->orderBy('sort','desc') ->orderBy('id','desc') ->limit($limitNum) ->get(); $datas = $datas? $datas->toArray() : []; if($datas){ RedisService::set($cacheKey, $datas, rand(300,600)); } return $datas; } /** * 获取文章推荐分类 * @param int $type 2-对口资料分类,3-专升本资料分类 * @return array|mixed */ public function getCateList($type=2, $sc=0) { $cacheKey = "caches:articles:cateList_{$type}"; $datas = RedisService::get($cacheKey); // 复习资料访问统计 if (empty($sc)) { ExamAccessLogModel::saveLog(date('Y-m-d'), $type, 5); } if($datas){ return $datas; } $datas = ArticleCateModel::where(['type'=> $type,'status'=>1,'mark'=>1]) ->select(['id','icon','name','pid','description','type','sort','status']) ->orderBy('sort','desc') ->orderBy('id','asc') ->get(); $datas = $datas? $datas->toArray() : []; //var_dump($datas); $list = []; if($datas){ foreach ($datas as $item){ $pid = isset($item['pid'])? $item['pid'] : 0; $id = isset($item['id'])? $item['id'] : 0; if($pid>0){ $list[$pid] = isset($list[$pid])? $list[$pid] : []; $list[$pid]['children'][] = $item; }else { $item['children'] = isset($list[$id]['children'])? $list[$id]['children'] : []; $list[$id] = $item; } } RedisService::set($cacheKey, $list, rand(300,600)); } return $list; } }