model = $model; $this->blogTypeModel = $blogTypeModel; $this->blogTypeRelationModel = $blogTypeRelationModel; $this->blogTagModel = $blogTagModel; $this->blogTagRelationModel = $blogTagRelationModel; $this->plateModel = $plateModel; } /** * 获取文章类型列表 * */ public function getBlogTypeList($params) { $retData = $this->blogTypeModel->getBlogTypeList($params); if (!empty($retData['data'])) { $typeIds = array_column($retData['data'], 'id'); $newsCounts = $this->blogTypeRelationModel->getBlogTypeCount($typeIds); foreach ($retData['data'] as &$newsItem) { $newsItem['blog_count'] = $newsCounts[$newsItem['id']] ?? 0; $newsItem['expand_content'] = empty($newsItem['expand_content']) ? [] : json_decode($newsItem['expand_content'], true); } } return $retData; } /** * 根据类型id 获取文章列表 * */ public function getBlogByTypeIds($params) { $retData = $this->blogTypeRelationModel->getBlogByTypeIds($params); return $retData; } /** * 根据标签id 获取文章列表 * */ public function getBlogByTagIds($params) { $retData = $this->blogTagRelationModel->getBlogByTagIds($params); return $retData; } /** * 文章类型保存 * */ public function saveBlogType($params) { $saveData = []; $nowTime = nowTime(); $saveData['update_time'] = $nowTime; $userId = $this->getAuthUserId(); if (!empty($params['seo_data'])) { $urlaUnique = WebFacade::checkUrlaUnique($params['seo_data']['urla'], $params['seo_id']); if (!$urlaUnique) { throw new ApiException(10019, ['name' => $params['seo_data']['urla']]); } } if (!empty($params['id'])) { $saveData['id'] = $params['id']; if (isset($params['type_name'])) { $nameUnique = $this->blogTypeModel->checkTypeNameUnique($params['type_name'], $params['id']); if (!$nameUnique) { throw new ApiException(10018, ['name' => $params['type_name']]); } $saveData['type_name'] = $params['type_name']; } if (isset($params['status'])) { $saveData['status'] = $params['status']; } if (isset($params['sort'])) { $saveData['sort'] = $params['sort']; } if (!empty($params['plate_id'])) { $saveData['plate_id'] = $params['plate_id']; } if (isset($params['expand_content'])) { $saveData['expand_content'] = !empty($params['expand_content']) ? json_encode($params['expand_content']) : ''; } } else { $nameUnique = $this->blogTypeModel->checkTypeNameUnique($params['type_name']); if (!$nameUnique) { throw new ApiException(10018, ['name' => $params['type_name']]); } $saveData['user_id'] = $userId; if (!empty($params['plate_id'])) { $saveData['plate_id'] = $params['plate_id']; } $saveData['type_name'] = $params['type_name']; $saveData['expand_content'] = !empty($params['expand_content']) ? json_encode($params['expand_content']) : ''; $saveData['sort'] = empty($params['sort']) ? 0 : $params['sort']; $saveData['create_time'] = $nowTime; } $id = $this->blogTypeModel->saveTypeData($saveData); if (!empty($id) && isset($params['seo_data'])) { $seoParams = [ 'relation_table' => WebService::RELATION_TABLE_BLOG_TYPE, 'relation_id' => $id, 'urla' => $params['seo_data']['urla'], 'seo_title' => $params['seo_data']['seo_title'], 'seo_keyword' => $params['seo_data']['seo_keyword'], 'seo_describe' => $params['seo_data']['seo_describe'] ]; if (empty($params['seo_id'])) { $seoParams['user_id'] = $userId; $urlaId = WebFacade::saveSeoData($seoParams); $this->blogTypeModel->newInstance()->where('id', '=', $id)->update(['seo_id' => $urlaId]); } else { $seoParams['id'] = $params['seo_id']; $urlaId = WebFacade::saveSeoData($seoParams); } if (!empty($saveData['status']) && $saveData['status'] == 2) { //移除对应seo $delSeoParams = []; $delSeoParams['relation_table'] = WebService::RELATION_TABLE_BLOG_TYPE; $delSeoParams['relation_id'] = $id; $ret = WebFacade::delSeoByRelation($delSeoParams); } } //更新所有页面缓存 WebFacade::clearPageCache('urla:'); return $id; } /** * 保存文章类型关系 * */ public function typeRelationSave($params) { $userId = $this->getAuthUserId(); $ret = $this->blogTypeRelationModel->typeRelationSave($params, $userId); //更新所有页面缓存 WebFacade::clearPageCache('urla:'); return $ret; } /** * 根据文章类型 保存文章关系 * */ public function addRelationByTypeId($typeId, $blogIds) { $userId = $this->getAuthUserId(); $ret = $this->blogTypeRelationModel->addByTypeId($typeId, $blogIds, $userId); //更新所有页面缓存 WebFacade::clearPageCache('urla:'); return $ret; } /** * 根据类型id及文章id 删除关联关系 * */ public function delTypeRelation($typeId, $blogId) { $ret = $this->blogTypeRelationModel->delTypeRelation($typeId, $blogId); //更新所有页面缓存 WebFacade::clearPageCache('urla:'); return $ret; } /** * 获取文章标签列表 * */ public function getBlogTagList($params) { $retData = $this->blogTagModel->getBlogTagList($params); if (!empty($retData['data'])) { $tagIds = array_column($retData['data'], 'id'); $newsCounts = $this->blogTagRelationModel->getBlogTagCount($tagIds); foreach ($retData['data'] as &$newsItem) { $newsItem['blog_count'] = $newsCounts[$newsItem['id']] ?? 0; } } return $retData; } /** * 文章标签保存 * */ public function saveBlogTag($params) { $saveData = []; $nowTime = nowTime(); $saveData['update_time'] = $nowTime; $userId = $this->getAuthUserId(); if (!empty($params['seo_data'])) { $urlaUnique = WebFacade::checkUrlaUnique($params['seo_data']['urla'], $params['seo_id']); if (!$urlaUnique) { throw new ApiException(10019, ['name' => $params['seo_data']['urla']]); } } if (!empty($params['id'])) { $saveData['id'] = $params['id']; if (isset($params['tag_name'])) { $nameUnique = $this->blogTagModel->checkTagNameUnique($params['tag_name'], $params['id']); if (!$nameUnique) { throw new ApiException(10018, ['name' => $params['tag_name']]); } $saveData['tag_name'] = $params['tag_name']; } if (isset($params['status'])) { $saveData['status'] = $params['status']; } if (isset($params['sort'])) { $saveData['sort'] = $params['sort']; } if (isset($params['is_hot'])) { $saveData['is_hot'] = $params['is_hot']; } } else { $nameUnique = $this->blogTagModel->checkTagNameUnique($params['tag_name']); if (!$nameUnique) { throw new ApiException(10018, ['name' => $params['tag_name']]); } $saveData['user_id'] = $userId; $saveData['tag_name'] = $params['tag_name']; $saveData['sort'] = empty($params['sort']) ? 0 : $params['sort']; $saveData['create_time'] = $nowTime; } $id = $this->blogTagModel->saveTagData($saveData); if (!empty($id) && isset($params['seo_data'])) { $seoParams = [ 'relation_table' => WebService::RELATION_TABLE_BLOG_TAG, 'relation_id' => $id, 'urla' => $params['seo_data']['urla'], 'seo_title' => $params['seo_data']['seo_title'], 'seo_keyword' => $params['seo_data']['seo_keyword'], 'seo_describe' => $params['seo_data']['seo_describe'] ]; if (empty($params['seo_id'])) { $seoParams['user_id'] = $userId; $urlaId = WebFacade::saveSeoData($seoParams); $this->blogTagModel->newInstance()->where('id', '=', $id)->update(['seo_id' => $urlaId]); } else { $seoParams['id'] = $params['seo_id']; $urlaId = WebFacade::saveSeoData($seoParams); } if (!empty($saveData['status']) && $saveData['status'] == 2) { //移除对应seo $delSeoParams = []; $delSeoParams['relation_table'] = WebService::RELATION_TABLE_BLOG_TAG; $delSeoParams['relation_id'] = $id; $ret = WebFacade::delSeoByRelation($delSeoParams); } } return $id; } /** * 保存文章标签关系 * */ public function tagRelationSave($params) { $userId = $this->getAuthUserId(); $ret = $this->blogTagRelationModel->tagRelationSave($params, $userId); //更新所有页面缓存 WebFacade::clearPageCache('urla:'); return $ret; } /** * 根据文章标签 保存文章关系 * */ public function addRelationByTagId($tagId, $blogIds) { $userId = $this->getAuthUserId(); $ret = $this->blogTagRelationModel->addByTagId($tagId, $blogIds, $userId); //更新所有页面缓存 WebFacade::clearPageCache('urla:'); return $ret; } /** * 根据标签id及文章id 删除关联关系 * */ public function delTagRelation($tagId, $blogId) { $ret = $this->blogTagRelationModel->delTagRelation($tagId, $blogId); //更新所有页面缓存 WebFacade::clearPageCache('urla:'); return $ret; } /** * 获取文章板块列表 * */ public function getBlogPlateList($params) { return $this->plateModel->getBlogPlateList($params); } /** * 文章板块保存 * */ public function saveBlogPlate($params) { $saveData = []; $userId = $this->getAuthUserId(); if (!empty($params['seo_data'])) { $urlaUnique = WebFacade::checkUrlaUnique($params['seo_data']['urla'], $params['seo_id']); if (!$urlaUnique) { throw new ApiException(10019, ['name' => $params['seo_data']['urla']]); } } if (!empty($params['id'])) { $saveData['id'] = $params['id']; if (isset($params['plate_name'])) { $nameUnique = $this->plateModel->checkPlateNameUnique($params['plate_name'], $params['id']); if (!$nameUnique) { throw new ApiException(10018, ['name' => $params['plate_name']]); } $saveData['plate_name'] = $params['plate_name']; } if (isset($params['status'])) { $saveData['status'] = $params['status']; } if (isset($params['sort'])) { $saveData['sort'] = $params['sort']; } } else { $nameUnique = $this->plateModel->checkPlateNameUnique($params['plate_name']); if (!$nameUnique) { throw new ApiException(10018, ['name' => $params['plate_name']]); } $saveData['user_id'] = $userId; $saveData['plate_name'] = $params['plate_name']; $saveData['sort'] = empty($params['sort']) ? 0 : $params['sort']; } $id = $this->plateModel->savePlateData($saveData); if (!empty($id) && isset($params['seo_data'])) { $seoParams = [ 'relation_table' => WebService::RELATION_TABLE_BLOG_PLATE, 'relation_id' => $id, 'urla' => $params['seo_data']['urla'], 'seo_title' => $params['seo_data']['seo_title'], 'seo_keyword' => $params['seo_data']['seo_keyword'], 'seo_describe' => $params['seo_data']['seo_describe'] ]; if (empty($params['seo_id'])) { $seoParams['user_id'] = $userId; $urlaId = WebFacade::saveSeoData($seoParams); $this->plateModel->newInstance()->where('id', '=', $id)->update(['seo_id' => $urlaId]); } else { $seoParams['id'] = $params['seo_id']; $urlaId = WebFacade::saveSeoData($seoParams); } if (!empty($saveData['status']) && $saveData['status'] == 2) { //移除对应seo $delSeoParams = []; $delSeoParams['relation_table'] = WebService::RELATION_TABLE_BLOG_PLATE; $delSeoParams['relation_id'] = $id; $ret = WebFacade::delSeoByRelation($delSeoParams); } } //更新所有页面缓存 WebFacade::clearPageCache('urla:'); return $id; } /** * 获取文章板块详情 * */ public function getBlogPlateInfo($params) { return $this->plateModel->getBlogPlateInfo($params); } /** * 保存文章数据 * */ public function saveBlog($params) { $saveData = $this->buildBlogData($params); $userId = $this->getAuthUserId(); $typeIds = empty($params['type_ids']) ? [] : $params['type_ids']; $tagIds = empty($params['tag_ids']) ? [] : $params['tag_ids']; $seoData = empty($params['seo_data']) ? [] : $params['seo_data']; $urlaId = empty($saveData['seo_id']) ? 0 : $saveData['seo_id']; if (!empty($seoData)) { $urlaUnique = WebFacade::checkUrlaUnique($seoData['urla'], $urlaId); if (!$urlaUnique) { throw new ApiException(10019, ['name' => $seoData['urla']]); } } $id = $this->model->saveBlogData($saveData); if (!empty($id)) { if (isset($params['seo_data'])) { $seoParams = []; $seoParams['relation_table'] = WebService::RELATION_TABLE_BLOG; $seoParams['relation_id'] = $id; $seoParams['urla'] = $seoData['urla']; $seoParams['seo_title'] = $seoData['seo_title']; $seoParams['seo_keyword'] = $seoData['seo_keyword']; $seoParams['seo_describe'] = $seoData['seo_describe']; if (empty($urlaId)) { $seoParams['user_id'] = $userId; $urlaId = WebFacade::saveSeoData($seoParams); $this->model->newInstance()->where('id', '=', $id)->update(['seo_id' => $urlaId]); } else { $seoParams['id'] = $urlaId; $urlaId = WebFacade::saveSeoData($seoParams); } } if (isset($params['type_ids'])) { $blogTypeParams = []; $blogTypeParams['blog_id'] = $id; $blogTypeParams['type_ids'] = $typeIds; $this->blogTypeRelationModel->typeRelationSave($blogTypeParams, $userId); } if (isset($params['tag_ids'])) { $blogTagParams = []; $blogTagParams['blog_id'] = $id; $blogTagParams['tag_ids'] = $tagIds; $this->blogTagRelationModel->tagRelationSave($blogTagParams, $userId); } // 获取是否需要修改排序数据 if (isset($saveData['sort'])) { $getData = $this->model->selectRaw('id') ->where('sort', '=', $saveData['sort']) ->where('plate_id', '=', $params['plate_id']) ->where('id', '<>', $id)->first(); if (!empty($getData['id'])) { $this->model->where('sort', '>=', $saveData['sort']) ->where('plate_id', '=', $params['plate_id']) ->where('id', '<>', $id) ->update(['sort' => DB::raw('sort + 1')]); } } if (!empty($params['id']) && !empty($params['virtual_view'])) { $this->updatePv($params['id'], $params['virtual_view']); } if (!empty($saveData['status']) && $saveData['status'] == 2) { //移除对应seo $delSeoParams = []; $delSeoParams['relation_table'] = WebService::RELATION_TABLE_BLOG; $delSeoParams['relation_id'] = $id; $ret = WebFacade::delSeoByRelation($delSeoParams); } } //更新所有页面缓存 WebFacade::clearPageCache('urla:'); return $id; } /** * 构造虚拟阅读量 * */ public function blogUvFactory() { $page = 1; $total = 0; do { $where = []; $where['page_size'] = 500; $where['page'] = $page; $where['status'] = 0; $where['lt_total_view'] = 1000; $blogData = $this->model->getBlogList($where, 'a.id,a.virtual_view,a.total_view'); if (!empty($blogData) && !empty($blogData['data'])) { $total = $blogData['total']; $data = $blogData['data']; $updateUvStr = ''; // 规则:小于1千数随记增加 150以内位数 foreach ($data as $item) { $value = 0; if ($item['total_view'] < 1000) { $value = mt_rand(0, 150); } /* if($item['total_view']<1000){ $value= mt_rand(0, 150); }else if($item['total_view']<5000){ $value= mt_rand(0, 99); }else{ $value= mt_rand(0, 9); }*/ $this->upBlogPv($item['id'], $value); } } $page++; } while ($total > ($page * 500)); WebFacade::clearPageCache('urla:'); } /** * 文章推荐设置 * */ public function recommendSet($params) { $id = empty($params['id']) ? 0 : $params['id']; $isRecommend = empty($params['is_recommend']) ? 0 : $params['is_recommend']; $ret = $this->model->where('id', '=', $id) ->update(['is_recommend' => $isRecommend, 'update_time' => nowTime()]); //更新所有页面缓存 WebFacade::clearPageCache('urla:'); return $id; } /** * 构建文章保存数据 * */ private function buildBlogData($params) { $saveData = []; $userId = $this->getAuthUserId(); $nowTime = nowTime(); $saveData['update_time'] = $nowTime; if (!empty($params['id'])) { $saveData['id'] = $params['id']; if (isset($params['seo_id'])) { $saveData['seo_id'] = $params['seo_id']; } if (isset($params['pub_date'])) { $saveData['pub_date'] = empty($params['pub_date']) ? null : $params['pub_date'];; } if (isset($params['title'])) { $nameUnique = $this->model->checkNameUnique($params['title'], $params['id']); if (!$nameUnique) { throw new ApiException(10018, ['name' => $params['title']]); } $saveData['title'] = empty($params['title']) ? '' : $params['title']; } if (isset($params['content'])) { $saveData['content'] = empty($params['content']) ? '' : $params['content']; } if (isset($params['outline_bar'])) { $saveData['outline_bar'] = empty($params['outline_bar']) ? '' : json_encode($params['outline_bar']); } if (isset($params['description'])) { $saveData['description'] = empty($params['description']) ? '' : $params['description']; } if (isset($params['image_url'])) { $saveData['image_url'] = empty($params['image_url']) ? '' : $params['image_url']; } if (isset($params['image_alt'])) { $saveData['image_alt'] = empty($params['image_alt']) ? '' : $params['image_alt']; } if (isset($params['main_image'])) { $saveData['main_image'] = empty($params['main_image']) ? '' : $params['main_image']; } if (isset($params['main_image_alt'])) { $saveData['main_image_alt'] = empty($params['main_image_alt']) ? '' : $params['main_image_alt']; } if (isset($params['download_url'])) { $saveData['download_url'] = empty($params['download_url']) ? '' : json_encode($params['download_url']); } if (isset($params['sort'])) { $saveData['sort'] = empty($params['sort']) ? 0 : $params['sort']; } if (isset($params['is_top'])) { $saveData['is_top'] = empty($params['is_top']) ? 0 : $params['is_top']; } if (!empty($params['plate_id'])) { $saveData['plate_id'] = $params['plate_id']; } if (isset($params['status'])) { $saveData['status'] = $params['status']; } if (isset($params['route_path'])) { $saveData['route_path'] = $params['route_path']; } if (isset($params['author'])) { $saveData['author'] = $params['author']; } if (isset($params['expand_content'])) { $saveData['expand_content'] = empty($params['expand_content']) ? '' : json_encode($params['expand_content']); } } else { $nameUnique = $this->model->checkNameUnique($params['title']); if (!$nameUnique) { throw new ApiException(10018, ['name' => $params['title']]); } if (empty($params['plate_id'])) { throw new ApiException(11001); } if (empty($params['title'])) { throw new ApiException(11002); } if (isset($params['status'])) { $saveData['status'] = $params['status']; } $saveData['user_id'] = $userId; $saveData['plate_id'] = $params['plate_id']; $saveData['pub_date'] = empty($params['pub_date']) ? null : $params['pub_date']; $saveData['title'] = empty($params['title']) ? '' : $params['title']; $saveData['content'] = empty($params['content']) ? '' : $params['content']; $saveData['outline_bar'] = empty($params['outline_bar']) ? '' : json_encode($params['outline_bar']); $saveData['description'] = empty($params['description']) ? '' : $params['description']; $saveData['image_url'] = empty($params['image_url']) ? '' : $params['image_url']; $saveData['image_alt'] = empty($params['image_alt']) ? '' : $params['image_alt']; $saveData['main_image'] = empty($params['main_image']) ? '' : $params['main_image']; $saveData['main_image_alt'] = empty($params['main_image_alt']) ? '' : $params['main_image_alt']; $saveData['virtual_view'] = empty($params['virtual_view']) ? 0 : $params['virtual_view']; $saveData['total_view'] = $saveData['virtual_view']; $saveData['download_url'] = empty($params['download_url']) ? '' : json_encode($params['download_url']); $saveData['expand_content'] = empty($params['expand_content']) ? '' : json_encode($params['expand_content']); $saveData['sort'] = empty($params['sort']) ? 0 : $params['sort']; $saveData['create_time'] = $nowTime; if (isset($params['author'])) { $saveData['author'] = $params['author']; } } return $saveData; } public function updatePv($id, $newVirtualView) { $virtualView = $this->getFieldById('virtual_view', $id); $value = $newVirtualView - $virtualView; $ret = $this->model->where('id', '=', $id)->increment('total_view', $value); $ret = $this->model->where('id', '=', $id)->increment('virtual_view', $value); WebFacade::clearPageCache('urla:'); return $ret; } /** * 更新文章浏览量 * */ public function upBlogPv($blogId, $value = 1) { if ($value > 1) { $ret = $this->model->where('id', '=', $blogId)->increment('total_view', $value); $ret = $this->model->where('id', '=', $blogId)->increment('virtual_view', $value); } else { $totalView = $this->getFieldById('total_view', $blogId); $value = (($value * -1) > $totalView) ? ($totalView * -1) : $value; $ret = $this->model->where('id', '=', $blogId)->increment('total_view', $value); } return $ret; } /** * 文章列表 * */ public function getBlogList($params) { $ret = $this->model->getBlogList($params); $pageListData = empty($ret['data']) ? [] : $ret['data']; if (!empty($pageListData)) { foreach ($pageListData as &$pageItem) { $pageItem['expand_content'] = empty($pageItem['expand_content']) ? [] : json_decode($pageItem['expand_content'], true); } } $ret['data'] = $pageListData; return $ret; } /** * 获取文章详情 * */ public function getBlogInfo($params) { $blogInfo = $this->model->getBlogInfo($params); if (!empty($blogInfo)) { if (!empty($blogInfo['seo_id'])) { $blogInfo['seo_data'] = WebFacade::getSeoInfo(['id' => $blogInfo['seo_id']]); } else { $blogInfo['seo_data'] = [ 'seo_describe' => '', 'seo_keyword' => '', 'seo_title' => '', 'urla' => '' ]; } if (!empty($blogInfo['download_url'])) { $blogInfo['download_url'] = json_decode($blogInfo['download_url'], true); } else { $blogInfo['download_url'] = []; } $blogInfo['type_ids'] = $this->blogTypeRelationModel->getTypeIds($blogInfo['id']); $blogInfo['tag_ids'] = $this->blogTagRelationModel->getTagIds($blogInfo['id']); $blogInfo['expand_content'] = empty($blogInfo['expand_content']) ? [] : json_decode($blogInfo['expand_content'], true); if (!empty($blogInfo['outline_bar'])) { $blogInfo['outline_bar'] = json_decode($blogInfo['outline_bar'], true); } } return $blogInfo; } /** * 获取文章分类未关联的文章 * */ public function getUnrelatedBlogByTypeIds($params) { $ret = $this->model->getUnrelatedBlogByTypeIds($params); return $ret; } /** * 获取文章标签未关联的文章 * */ public function getUnrelatedBlogByTagIds($params) { $ret = $this->model->getUnrelatedBlogByTagIds($params); return $ret; } /** * 获取已发布的连接 * */ public function getPublishBlogList($params, $fields = '') { if (empty($fields)) { $fields = 'a.id,a.plate_id,a.pub_date,a.title,a.description,a.image_url,a.image_alt,a.is_top,a.is_recommend,a.download_url,a.total_view,b.urla'; } $blogList = $this->model->getPublishBlogList($params, $fields); $blogIds = array_column($blogList, 'id'); if ($blogIds) { $newestTypeAndTagData = $this->getTypeAndTagDataByBlogId($blogIds); foreach ($blogList as &$newestItem) { if (!empty($newestTypeAndTagData[$newestItem['id']]['type_items'])) { $newestItem['type_items'] = $newestTypeAndTagData[$newestItem['id']]['type_items']; } else { $newestItem['type_items'] = []; } if (!empty($newestTypeAndTagData[$newestItem['id']]['tag_items'])) { $newestItem['tag_items'] = $newestTypeAndTagData[$newestItem['id']]['tag_items']; } else { $newestItem['tag_items'] = []; } $newestItem['expand_content'] = empty($newestItem['expand_content']) ? [] : json_decode($newestItem['expand_content'], true); } } return $blogList; } /** * 获取文章详情渲染数据 * */ public function getBlogRenderData($params) { $blogInfo = $this->model->getBlogInfo($params); if (!empty($blogInfo)) { if (!empty($blogInfo['outline_bar'])) { $blogInfo['outline_bar'] = json_decode($blogInfo['outline_bar'], true); } $newestTypeAndTagData = $this->getTypeAndTagDataByBlogId([$blogInfo['id']]); if (!empty($newestTypeAndTagData[$blogInfo['id']]['type_items'])) { $blogInfo['type_items'] = $newestTypeAndTagData[$blogInfo['id']]['type_items']; } else { $blogInfo['type_items'] = []; } if (!empty($newestTypeAndTagData[$blogInfo['id']]['tag_items'])) { $blogInfo['tag_items'] = $newestTypeAndTagData[$blogInfo['id']]['tag_items']; } else { $blogInfo['tag_items'] = []; } $blogInfo['seo_data'] = WebFacade::getSeoInfo(['id' => $blogInfo['seo_id']]); $blogInfo['expand_content'] = empty($blogInfo['expand_content']) ? [] : json_decode($blogInfo['expand_content'], true); } return $blogInfo; } /** * 获取文章列表页渲染数据 * */ public function getBlogRecentlyData($params = []) { $data = [ 'newest_list' => [],//最新文章列表 'newest_plate_list' => [],//最新板块文章列表 'hot_news_list' => [],//热门文章列表 'recommend_news_list' => [],//推荐文章列表 'hot_news_tag' => [],//热门文章标签 'ad_block_list' => [],//广告块 ]; $data['newest_plate_list'] = $this->getNewestPlateList($params); $data['newest_list'] = $this->getNewestList($params); $data['hot_news_list'] = $this->getHotList($params); $data['recommend_news_list'] = $this->getRecommendList($params); $data['hot_news_tag'] = $this->blogTagModel->getBaseTagList(['is_hot' => 1]); return $data; } /** * 获取板块文章列表页渲染数据 * */ public function getBlogPlateRenderData($params = []) { $data = [ 'plate_info' => [],//最新板块文章列表 'plate_blog_list' => [],//文章板块列表 'hot_news_list' => [],//热门文章列表 'recommend_news_list' => [],//推荐文章列表 'hot_news_tag' => [],//热门标签 ]; $plateWhere = []; $plateWhere['status'] = 0; $plateWhere['id'] = empty($params['plate_id']) ? 0 : $params['plate_id']; $blogPlateInfo = $this->plateModel->getBlogPlateInfo($plateWhere); $data['plate_info'] = $blogPlateInfo; $data['plate_blog_list'] = $this->getBlogPlateTypeData($blogPlateInfo['id']); //板块热门 $data['hot_news_list'] = $this->getHotList($params); //板块推荐 $data['recommend_news_list'] = $this->getRecommendList($params); //板块热门标签 $data['hot_news_tag'] = $this->blogTagModel->getBaseTagList(['is_hot' => 1]); return $data; } /** * 获取类型文章列表页渲染数据 * */ public function getBlogTypeRenderData($params = []) { $data = [ 'type_info' => [],//最新板块文章列表 'type_blog_list' => [],//文章板块列表 'hot_news_list' => [],//热门文章列表 'recommend_news_list' => [],//推荐文章列表 'hot_news_tag' => [],//热门标签 ]; $typeWhere = []; $typeWhere['status'] = 0; $typeWhere['id'] = empty($params['type_id']) ? 0 : $params['type_id']; $typeInfo = $this->blogTypeModel->getBlogTypeInfo($typeWhere); $blogTypeListParams = []; $blogTypeListParams['page_size'] = 6; $fields = 'a.id,a.plate_id,a.pub_date,a.title,a.description,a.image_url,a.image_alt,a.is_top,a.is_recommend,a.download_url,a.total_view,b.type_id,c.urla'; $blogList = $this->model->getRenderListByTypeIds([$typeInfo['id']], $blogTypeListParams, $fields); $blogIds = array_column($blogList, 'id'); $typeInfo['blog_list'] = []; if ($blogIds) { $blogTypeAndTagData = $this->getTypeAndTagDataByBlogId($blogIds); foreach ($blogList as $blogItem) { if (!empty($blogTypeAndTagData[$blogItem['id']]['type_items'])) { $blogItem['type_items'] = $blogTypeAndTagData[$blogItem['id']]['type_items']; } else { $blogItem['type_items'] = []; } if (!empty($blogTypeAndTagData[$blogItem['id']]['tag_items'])) { $blogItem['tag_items'] = $blogTypeAndTagData[$blogItem['id']]['tag_items']; } else { $blogItem['tag_items'] = []; } $typeInfo['blog_list'][] = $blogItem; } } $data['type_info'] = $typeInfo; //热门文章列表 $data['hot_news_list'] = $this->getHotList($params); //推荐文章列表 $data['recommend_news_list'] = $this->getRecommendList($params); //热门标签 $data['hot_news_tag'] = $this->blogTagModel->getBaseTagList(['is_hot' => 1]); return $data; } /** * 获取标签文章列表页渲染数据 * */ public function getBlogTagRenderData($params = []) { $data = [ 'tag_info' => [],//最新板块文章列表 'tag_blog_list' => [],//文章板块列表 'hot_news_list' => [],//热门文章列表 'recommend_news_list' => [],//推荐文章列表 'hot_news_tag' => [],//热门标签 ]; $tagWhere = []; $tagWhere['status'] = 0; $tagWhere['id'] = empty($params['tag_id']) ? 0 : $params['tag_id']; $tagInfo = $this->blogTagModel->getBlogTagInfo($tagWhere); $blogTagListParams = []; $blogTagListParams['page_size'] = 6; $fields = 'a.id,a.plate_id,a.pub_date,a.title,a.description,a.image_url,a.image_alt,a.is_top,a.is_recommend,a.download_url,a.total_view,b.tag_id,c.urla'; $blogList = $this->model->getRenderListByTagIds([$tagInfo['id']], $blogTagListParams, $fields); $blogIds = array_column($blogList, 'id'); $tagInfo['blog_list'] = []; if ($blogIds) { $blogTypeAndTagData = $this->getTypeAndTagDataByBlogId($blogIds); foreach ($blogList as $blogItem) { if (!empty($blogTypeAndTagData[$blogItem['id']]['type_items'])) { $blogItem['type_items'] = $blogTypeAndTagData[$blogItem['id']]['type_items']; } else { $blogItem['type_items'] = []; } if (!empty($blogTypeAndTagData[$blogItem['id']]['tag_items'])) { $blogItem['tag_items'] = $blogTypeAndTagData[$blogItem['id']]['tag_items']; } else { $blogItem['tag_items'] = []; } $tagInfo['blog_list'][] = $blogItem; } } $data['tag_info'] = $tagInfo; //热门文章列表 $data['hot_news_list'] = $this->getHotList($params); //推荐文章列表 $data['recommend_news_list'] = $this->getRecommendList($params); //热门标签 $data['hot_news_tag'] = $this->blogTagModel->getBaseTagList(['is_hot' => 1]); return $data; } /** * 获取文章板块类型对应的默认列表数据 * */ public function getBlogPlateTypeData($plate_id) { $blogTypeDataList = $this->blogTypeModel->getBlogTypeList(['status' => 0, 'page_size' => 999]); $blogTypeData = empty($blogTypeDataList['data']) ? [] : $blogTypeDataList['data']; $resultData = []; if (!empty($blogTypeData)) { $blogTypeData = mapByKey($blogTypeData, 'id'); $typeIds = array_keys($blogTypeData); } if (!empty($typeIds)) { $blogTypeListParams = []; $blogTypeListParams['page_size'] = 6; $blogTypeListParams['plate_id'] = $plate_id; $fields = 'a.id,a.plate_id,a.pub_date,a.title,a.description,a.image_url,a.image_alt,a.is_top,a.is_recommend,a.download_url,a.total_view,b.type_id,c.urla'; $blogList = $this->model->getRenderListByTypeIds($typeIds, $blogTypeListParams, $fields); $blogIds = array_column($blogList, 'id'); $plateBlogListData = []; if ($blogIds) { $blogTypeAndTagData = $this->getTypeAndTagDataByBlogId($blogIds); foreach ($blogList as $blogItem) { if (!empty($blogTypeAndTagData[$blogItem['id']]['type_items'])) { $blogItem['type_items'] = $blogTypeAndTagData[$blogItem['id']]['type_items']; } else { $blogItem['type_items'] = []; } if (!empty($blogTypeAndTagData[$blogItem['id']]['tag_items'])) { $blogItem['tag_items'] = $blogTypeAndTagData[$blogItem['id']]['tag_items']; } else { $blogItem['tag_items'] = []; } $plateBlogListData[$blogItem['type_id']][] = $blogItem; } } $allResultData = [ 'id' => 0, 'user_id' => 0, 'type_name' => '全部', 'sort' => 0, 'seo_id' => 0, 'status' => 0, 'blog_list' => [] ]; $fields = 'a.id,a.plate_id,a.pub_date,a.title,a.description,a.image_url,a.image_alt,a.is_top,a.is_recommend,a.download_url,a.total_view,b.urla'; $blogTypeListParams['sort']['is_top'] = 1; $blogTypeListParams['sort']['pub_date'] = 1; $allPlateList = $this->getPublishBlogList($blogTypeListParams, $fields); if (!empty($allPlateList)) { $allResultData['blog_list'] = $allPlateList; } $resultData[] = $allResultData; foreach ($blogTypeData as $value) { if (!empty($plateBlogListData[$value['id']])) { $value['blog_list'] = $plateBlogListData[$value['id']]; $resultData[] = $value; } } } return $resultData; } /** * 最新文章列表 * */ public function getNewestList($params) { $newestListParams = []; $newestListParams['page_size'] = empty($params['newest_page_size']) ? 7 : $params['newest_page_size']; $newestListParams['sort']['is_top'] = 1; $newestListParams['sort']['pub_date'] = 1; $newestList = $this->getPublishBlogList($newestListParams); return $newestList; } /** * 热门文章列表 * */ public function getHotList($params) { $hotListParams = []; $hotListParams['page_size'] = empty($params['hot_news_size']) ? 4 : $params['hot_news_size']; $hotListParams['sort']['is_hot'] = 1; $hotListParams['sort']['pub_date'] = 1; $hotList = $this->getPublishBlogList($hotListParams); return $hotList; } /** * 推荐文章列表 * */ public function getRecommendList($params) { $recommendListParams = []; $recommendListParams['page_size'] = empty($params['recommend_news_size']) ? 4 : $params['recommend_news_size']; $recommendListParams['sort']['is_recommend'] = 1; $recommendListParams['sort']['pub_date'] = 1; $recommendList = $this->getPublishBlogList($recommendListParams); return $recommendList; } /** * 最新板块文章列表 * */ public function getNewestPlateList($params) { $plateIds = []; $resultData = []; $plateWhere = []; $plateWhere['status'] = 0; $plateWhere['page_size'] = 999; $plateDataList = $this->plateModel->getBlogPlateList($plateWhere, 'a.id,a.plate_name,a.plate_des,b.urla'); $plateData = empty($plateDataList['data']) ? [] : $plateDataList['data']; if (!empty($plateData)) { $plateData = mapByKey($plateData, 'id'); $plateIds = array_keys($plateData); } if (!empty($plateIds)) { $newestPlateParams = []; $newestPlateParams['page_size'] = empty($params['newest_plate_page_size']) ? 6 : $params['newest_plate_page_size']; $fields = 'a.id,a.plate_id,a.pub_date,a.title,a.description,a.image_url,a.image_alt,a.is_top,a.is_recommend,a.download_url,a.total_view,b.urla'; $newestPlateList = $this->model->getNewestPlateList($plateIds, $newestPlateParams, $fields); $newestBlogId = array_column($newestPlateList, 'id'); $plateBlogListData = []; if ($newestBlogId) { $newestTypeAndTagData = $this->getTypeAndTagDataByBlogId($newestBlogId); foreach ($newestPlateList as $newestItem) { if (!empty($newestTypeAndTagData[$newestItem['id']]['type_items'])) { $newestItem['type_items'] = $newestTypeAndTagData[$newestItem['id']]['type_items']; } else { $newestItem['type_items'] = []; } if (!empty($newestTypeAndTagData[$newestItem['id']]['tag_items'])) { $newestItem['tag_items'] = $newestTypeAndTagData[$newestItem['id']]['tag_items']; } else { $newestItem['tag_items'] = []; } $plateBlogListData[$newestItem['plate_id']][] = $newestItem; } } foreach ($plateData as $value) { if (!empty($plateBlogListData[$value['id']])) { $value['blog_list'] = $plateBlogListData[$value['id']]; $resultData[] = $value; } } } return $resultData; } /** * 根据文章id获取文章类型及文章标签数据 * */ public function getTypeAndTagDataByBlogId($newestBlogIds) { $resultData = []; $newestTypeData = $this->blogTypeRelationModel->getTypeListByBlogIds($newestBlogIds); foreach ($newestTypeData as $typeItem) { $resultData[$typeItem['blog_id']]['type_items'][] = $typeItem; } $newestTagData = $this->blogTagRelationModel->getTagListByBlogIds($newestBlogIds); foreach ($newestTagData as $tagItem) { $resultData[$tagItem['blog_id']]['tag_items'][] = $tagItem; } return $resultData; } /** * 根据类型id获取文章 * */ public function getPublishBlogListByTypeId($params) { $fields = 'a.id,a.plate_id,a.pub_date,a.title,a.description,a.image_url,a.image_alt,a.is_top,a.is_recommend,a.download_url,a.total_view,b.type_id,c.urla'; $ret = $this->model->getPublishBlogListByTypeId($params, $fields); $blogData = empty($ret['data']) ? [] : $ret['data']; if (!empty($blogData)) { $newestBlogIds = array_column($blogData, 'id'); $newestTypeAndTagData = $this->getTypeAndTagDataByBlogId($newestBlogIds); foreach ($blogData as &$newestItem) { if (!empty($newestTypeAndTagData[$newestItem['id']]['type_items'])) { $newestItem['type_items'] = $newestTypeAndTagData[$newestItem['id']]['type_items']; } else { $newestItem['type_items'] = []; } if (!empty($newestTypeAndTagData[$newestItem['id']]['tag_items'])) { $newestItem['tag_items'] = $newestTypeAndTagData[$newestItem['id']]['tag_items']; } else { $newestItem['tag_items'] = []; } } $ret['data'] = $blogData; } return $ret; } /** * 根据标签id获取文章 * */ public function getPublishBlogListByTagId($params) { $fields = 'a.id,a.plate_id,a.pub_date,a.title,a.description,a.image_url,a.image_alt,a.is_top,a.is_recommend,a.download_url,a.total_view,b.tag_id,c.urla'; $ret = $this->model->getPublishBlogListByTagId($params, $fields); $blogData = empty($ret['data']) ? [] : $ret['data']; if (!empty($blogData)) { $newestBlogIds = array_column($blogData, 'id'); $newestTypeAndTagData = $this->getTypeAndTagDataByBlogId($newestBlogIds); foreach ($blogData as &$newestItem) { if (!empty($newestTypeAndTagData[$newestItem['id']]['type_items'])) { $newestItem['type_items'] = $newestTypeAndTagData[$newestItem['id']]['type_items']; } else { $newestItem['type_items'] = []; } if (!empty($newestTypeAndTagData[$newestItem['id']]['tag_items'])) { $newestItem['tag_items'] = $newestTypeAndTagData[$newestItem['id']]['tag_items']; } else { $newestItem['tag_items'] = []; } } $ret['data'] = $blogData; } return $ret; } /** * 搜索获取已发布的文章 * */ public function getSearchBlogList($params) { if (empty($fields)) { $fields = 'a.id,a.title,b.urla'; } $blogList = $this->model->getPublishBlogList($params, $fields); return $blogList; } /** * 新闻排序 */ public function changeBlogSort($blogId, $sort) { $data = []; $getData = $this->findOneById($blogId, 'id,sort,plate_id'); if ($sort == 0) { // 置顶 $sortData = $this->findOneBy([ 'sort' => $sort, 'plate_id' => $getData['plate_id'], 'id' => ['<>', $blogId] ], 'id'); if (empty($sortData['id'])) { throw new ApiException(1004, ['msg' => '当前已经是最高一行']); } if ($getData['sort'] == 0) { $this->incrementBy([ 'id' => ['<>', $blogId], 'plate_id' => $getData['plate_id'], ], 'sort'); } else { $this->incrementBy([ 'id' => ['<', $blogId], 'plate_id' => $getData['plate_id'], ], 'sort'); } $upData['sort'] = $sort; } elseif ($sort == '-1') { // 上移 $upSortData = $this->model->where('sort', '<', $getData['sort']) ->where('plate_id', '=', $getData['plate_id']) ->orderBy('sort', 'desc')->first(); if (empty($upSortData)) { throw new ApiException(1004, ['msg' => '当前已经是最高一行']); } else { $upData = $upSortData->toArray(); } } elseif ($sort == '+1') { // 下移 $downSortData = $this->model->where('sort', '>', $getData['sort']) ->where('plate_id', '=', $getData['plate_id']) ->orderBy('sort', 'asc')->first(); if (empty($downSortData)) { throw new ApiException(1004, ['msg' => '当前已经是最后一行']); } else { $upData = $downSortData->toArray(); } } $data['sort'] = $upData['sort']; $this->updateBy([ 'sort' => $data['sort'], 'plate_id' => $getData['plate_id'], ], [ 'sort' => $getData['sort'] ]); $is_top = 0; if ($data['sort'] == 0) { $is_top = 1; } $rst = $this->update($blogId, [ 'sort' => $data['sort'], 'is_top' => $is_top ]); //更新所有页面缓存 WebFacade::clearPageCache('urla:'); return $rst; } /** * 批量添加分类 * @param $params * @return int */ public function typePluralEdit($params) { $userId = $this->getAuthUserId(); $ret = 1; foreach ($params['ids'] as $v) { $ret = $this->blogTypeRelationModel->addByBlogId($v, $params['type_ids'], $userId); } return $ret; } /** * 批量打标签 * @param $params * @return int */ public function tagPluralEdit($params) { $userId = $this->getAuthUserId(); $ret = 1; foreach ($params['ids'] as $v) { $ret = $this->blogTagRelationModel->addByBlogId($v, $params['tag_ids'], $userId); } return $ret; } /** * 批量删除 * @param $params * @return mixed */ public function batchDelete($params) { $ids = $params['ids']; $userId = $this->getAuthUserId(); $rst = $this->model->whereIn('id', $ids)->update(['status' => 2]); //删除处理 if ($rst) { $this->blogTypeRelationModel->delByBlogId($ids, $userId); $this->blogTagRelationModel->delByBlogId($ids, $userId); } return $rst; } /** * 批量更新状态 * @param $params * @return mixed */ public function batchStatus($params) { $ids = $params['ids']; $status = $params['status']; $rst = $this->model->whereIn('id', $ids)->update(['status' => $status]); return $rst; } }