first commot
This commit is contained in:
@@ -0,0 +1,249 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\index\controller\Article;
|
||||
|
||||
use app\index\BaseController;
|
||||
use app\model\Articles;
|
||||
use app\model\ArticlesCategory;
|
||||
use Symfony\Component\VarDumper\VarDumper;
|
||||
use think\exception\ValidateException;
|
||||
use think\facade\Request;
|
||||
use think\facade\Session;
|
||||
use think\response\Json;
|
||||
use think\db\exception\DbException;
|
||||
|
||||
class ArticleController extends BaseController
|
||||
{
|
||||
/**
|
||||
* 获取新闻中心顶部4篇文章
|
||||
* @return Json
|
||||
*/
|
||||
public function getNewsCenterTop4(): Json
|
||||
{
|
||||
try {
|
||||
// 1. 查询新闻中心主分类
|
||||
$newsCenterCategory = ArticlesCategory::where('name', '新闻中心')
|
||||
->where('delete_time', null)
|
||||
->find();
|
||||
|
||||
if (!$newsCenterCategory) {
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => 'success',
|
||||
'list' => [],
|
||||
]);
|
||||
}
|
||||
|
||||
// 2. 获取新闻中心及其子分类的ID列表
|
||||
$mainCategoryId = $newsCenterCategory['id'];
|
||||
|
||||
// 查询子分类
|
||||
$subCategories = ArticlesCategory::where('cid', $mainCategoryId)
|
||||
->where('delete_time', null)
|
||||
->column('id');
|
||||
|
||||
// 合并主分类和子分类ID
|
||||
$categoryIds = array_merge([$mainCategoryId], $subCategories);
|
||||
|
||||
if (empty($categoryIds)) {
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => 'success',
|
||||
'list' => [],
|
||||
]);
|
||||
}
|
||||
|
||||
// 3. 查询符合条件的文章,按指定优先级排序,取最新4篇
|
||||
$articles = Articles::published()
|
||||
->whereIn('cate', $categoryIds)
|
||||
->order([
|
||||
'recommend' => 'desc',
|
||||
'publishdate' => 'desc',
|
||||
'sort' => 'desc',
|
||||
'id' => 'desc'
|
||||
])
|
||||
->field([
|
||||
'id',
|
||||
'title',
|
||||
'content',
|
||||
'publishdate',
|
||||
'DAY(publishdate) AS `date`',
|
||||
'DATE_FORMAT(publishdate, "%Y-%m") AS `month`',
|
||||
'image',
|
||||
'recommend'
|
||||
])
|
||||
->limit(4)
|
||||
->select();
|
||||
|
||||
// 4. 处理文章数据,去除HTML标签并生成desc字段
|
||||
foreach ($articles as &$article) {
|
||||
// 使用PHP的strip_tags函数去除HTML标签,然后截取前100个字符
|
||||
$article['desc'] = mb_substr(trim(strip_tags($article['content'])), 0, 100, 'UTF-8');
|
||||
// 移除原始content字段,减少返回数据大小
|
||||
unset($article['content']);
|
||||
}
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => 'success',
|
||||
'list' => $articles,
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
// 打印完整错误信息到日志
|
||||
$errorMsg = '错误信息:' . $e->getMessage() . ' | 错误行号:' . $e->getLine() . ' | 执行SQL:' . Articles::getLastSql();
|
||||
trace('新闻中心文章查询失败: ' . $errorMsg, 'error');
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => '新闻中心文章查询失败,请稍后重试',
|
||||
'list' => []
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 游客文章阅读量函数
|
||||
* @return Json
|
||||
*/
|
||||
public function articleViews(): Json
|
||||
{
|
||||
$id = Request::param('id');
|
||||
if (!$id) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => '没有文章id',
|
||||
'list' => []
|
||||
]);
|
||||
}
|
||||
|
||||
try {
|
||||
$article = Articles::where('id', $id)->where('delete_time', null)->find();
|
||||
|
||||
if (!$article) {
|
||||
return json([
|
||||
'code' => 404,
|
||||
'msg' => '文章不存在',
|
||||
'list' => []
|
||||
]);
|
||||
}
|
||||
|
||||
$article->inc('views');
|
||||
$article->save();
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '阅读量更新成功',
|
||||
'data' => [
|
||||
'views' => $article->views
|
||||
]
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
trace('阅读量更新失败: ' . $e->getMessage(), 'error');
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => '阅读量更新失败,请稍后重试',
|
||||
'list' => []
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 游客文章点赞函数
|
||||
* @return Json
|
||||
*/
|
||||
public function articleLikes(): Json
|
||||
{
|
||||
$id = Request::param('id');
|
||||
if (!$id) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => '没有文章id',
|
||||
'list' => []
|
||||
]);
|
||||
}
|
||||
|
||||
try {
|
||||
$article = Articles::where('id', $id)->where('delete_time', null)->find();
|
||||
|
||||
if (!$article) {
|
||||
return json([
|
||||
'code' => 404,
|
||||
'msg' => '文章不存在',
|
||||
'list' => []
|
||||
]);
|
||||
}
|
||||
|
||||
$article->inc('likes');
|
||||
$article->save();
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '点赞成功',
|
||||
'data' => [
|
||||
'likes' => $article->likes
|
||||
]
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
trace('点赞失败: ' . $e->getMessage(), 'error');
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => '点赞失败,请稍后重试',
|
||||
'list' => []
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 游客文章取消点赞函数
|
||||
* @return Json
|
||||
*/
|
||||
public function articleUnlikes(): Json
|
||||
{
|
||||
$id = Request::param('id');
|
||||
if (!$id) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => '没有文章id',
|
||||
'list' => []
|
||||
]);
|
||||
}
|
||||
|
||||
try {
|
||||
$article = Articles::where('id', $id)->where('delete_time', null)->find();
|
||||
|
||||
if (!$article) {
|
||||
return json([
|
||||
'code' => 404,
|
||||
'msg' => '文章不存在',
|
||||
'list' => []
|
||||
]);
|
||||
}
|
||||
|
||||
if ($article->likes <= 0) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => '文章点赞数为0,不能取消点赞',
|
||||
'list' => []
|
||||
]);
|
||||
}
|
||||
|
||||
$article->dec('likes');
|
||||
$article->save();
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '取消点赞成功',
|
||||
'data' => [
|
||||
'likes' => $article->likes
|
||||
]
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
trace('取消点赞失败: ' . $e->getMessage(), 'error');
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => '取消点赞失败,请稍后重试',
|
||||
'list' => []
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\index\controller;
|
||||
|
||||
use app\model\Banner;
|
||||
use app\index\BaseController;
|
||||
use app\model\FrontMenu;
|
||||
use app\model\OnePage;
|
||||
use think\db\exception\DbException;
|
||||
|
||||
class Index extends BaseController
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
return '您好!这是一个[index]示例应用';
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取前端导航接口
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function getHeadMenu()
|
||||
{
|
||||
try {
|
||||
// 获取所有未删除的菜单
|
||||
$frontMenus = FrontMenu::where('delete_time', null)
|
||||
->field('id,pid,title,type,image,path,component_path,sort,desc')
|
||||
->order('sort', 'desc')
|
||||
->select()
|
||||
->toArray(); // 转换为数组
|
||||
|
||||
// 使用 buildFrontMenuTree 构建树形结构
|
||||
$treeFrontMenus = $this->buildFrontMenuTree($frontMenus);
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => 'success',
|
||||
'data' => $treeFrontMenus
|
||||
]);
|
||||
} catch (DbException $e) {
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => 'fail:' . $e->getMessage(),
|
||||
'data' => $e->getTraceAsString()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建前端导航树形结构
|
||||
* @param array $frontMenus 前端导航列表
|
||||
* @param int $pid 父前端导航ID
|
||||
* @return array
|
||||
*/
|
||||
private function buildFrontMenuTree(array $frontMenus, int $pid = 0): array
|
||||
{
|
||||
$tree = [];
|
||||
|
||||
foreach ($frontMenus as $frontMenu) {
|
||||
// 将null的pid视为0处理
|
||||
$menuPid = is_null($frontMenu['pid']) ? 0 : $frontMenu['pid'];
|
||||
if ($menuPid == $pid) {
|
||||
$children = $this->buildFrontMenuTree($frontMenus, $frontMenu['id']);
|
||||
if (!empty($children)) {
|
||||
$frontMenu['children'] = $children;
|
||||
}
|
||||
$tree[] = $frontMenu;
|
||||
}
|
||||
}
|
||||
|
||||
// 按 sort 字段降序排序
|
||||
usort($tree, function ($a, $b) {
|
||||
$sortA = $a['sort'] ?? 0;
|
||||
$sortB = $b['sort'] ?? 0;
|
||||
return $sortB - $sortA;
|
||||
});
|
||||
|
||||
return $tree;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据路径获取单页内容
|
||||
* @param string $path 路由路径
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function getOnePageByPath($path = '')
|
||||
{
|
||||
try {
|
||||
// 从路由参数获取路径
|
||||
if (empty($path)) {
|
||||
// 如果没有路由参数,尝试从 pathinfo 获取
|
||||
$requestPath = $this->request->pathinfo();
|
||||
$path = str_replace('onepage/', '', $requestPath);
|
||||
}
|
||||
|
||||
// 确保路径以 / 开头
|
||||
if (empty($path) || $path[0] !== '/') {
|
||||
$path = '/' . $path;
|
||||
}
|
||||
|
||||
// 解码路径
|
||||
$path = urldecode($path);
|
||||
|
||||
// 查找对应路径的单页
|
||||
$onePage = OnePage::where('path', $path)
|
||||
->where('status', 1)
|
||||
->where('delete_time', null)
|
||||
->find();
|
||||
|
||||
if (!$onePage) {
|
||||
return json([
|
||||
'code' => 404,
|
||||
'msg' => '单页不存在或已禁用',
|
||||
'data' => null
|
||||
]);
|
||||
}
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => 'success',
|
||||
'data' => $onePage->toArray()
|
||||
]);
|
||||
} catch (DbException $e) {
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => 'fail:' . $e->getMessage(),
|
||||
'data' => null
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,434 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\index\controller;
|
||||
|
||||
use app\index\BaseController;
|
||||
use Symfony\Component\VarDumper\VarDumper;
|
||||
use think\exception\ValidateException;
|
||||
use think\facade\Request;
|
||||
use think\facade\Session;
|
||||
use think\response\Json;
|
||||
use think\db\exception\DbException;
|
||||
|
||||
use app\model\Articles;
|
||||
use app\model\ArticlesCategory;
|
||||
|
||||
|
||||
class NewsCenterController extends BaseController
|
||||
{
|
||||
/**
|
||||
* 根据分类获取文章
|
||||
* @param string $category 分类名称
|
||||
* @return Json
|
||||
*/
|
||||
public function getNewsByCategory(string $category): Json
|
||||
{
|
||||
// 查询分类
|
||||
$categoryInfo = ArticlesCategory::where('name', $category)
|
||||
->where('delete_time', null)
|
||||
->find();
|
||||
|
||||
if (!$categoryInfo) {
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => 'success',
|
||||
'list' => [],
|
||||
]);
|
||||
}
|
||||
|
||||
// 查询文章
|
||||
$articles = Articles::published()
|
||||
->where('cate', $categoryInfo['id'])
|
||||
->order('publishdate', 'desc')
|
||||
->limit(4)
|
||||
->select();
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => 'success',
|
||||
'list' => $articles,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取企业新闻
|
||||
* @return Json
|
||||
*/
|
||||
public function getCompanyNews(): Json
|
||||
{
|
||||
// 获取分页参数
|
||||
$page = Request::param('page', 1);
|
||||
$limit = Request::param('limit', 10);
|
||||
$page = max(1, intval($page));
|
||||
$limit = max(1, min(50, intval($limit))); // 限制每页最多50条
|
||||
|
||||
// 查询分类
|
||||
$categoryInfo = ArticlesCategory::where('name', '企业新闻')
|
||||
->where('delete_time', null)
|
||||
->find();
|
||||
|
||||
if (!$categoryInfo) {
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => 'success',
|
||||
'list' => [],
|
||||
'total' => 0,
|
||||
'page' => $page,
|
||||
'limit' => $limit,
|
||||
]);
|
||||
}
|
||||
|
||||
// 查询总数
|
||||
$total = Articles::where('cate', $categoryInfo['id'])
|
||||
->where('delete_time', null)
|
||||
->where('status', 2)
|
||||
->count();
|
||||
|
||||
// 查询文章
|
||||
$articles = Articles::where('cate', $categoryInfo['id'])
|
||||
->where('delete_time', null)
|
||||
->where('status', 2)
|
||||
->order('top', 'desc')
|
||||
->order('recommend', 'desc')
|
||||
->order('sort', 'desc')
|
||||
->order('publishdate', 'desc')
|
||||
->page($page, $limit)
|
||||
->select();
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => 'success',
|
||||
'list' => $articles,
|
||||
'total' => $total,
|
||||
'page' => $page,
|
||||
'limit' => $limit,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取金蝶新闻
|
||||
* @return Json
|
||||
*/
|
||||
public function getKingdeeNews(): Json
|
||||
{
|
||||
// 获取分页参数
|
||||
$page = Request::param('page', 1);
|
||||
$limit = Request::param('limit', 10);
|
||||
$page = max(1, intval($page));
|
||||
$limit = max(1, min(50, intval($limit))); // 限制每页最多50条
|
||||
|
||||
// 查询分类
|
||||
$categoryInfo = ArticlesCategory::where('name', '金蝶新闻')
|
||||
->where('delete_time', null)
|
||||
->find();
|
||||
|
||||
if (!$categoryInfo) {
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => 'success',
|
||||
'list' => [],
|
||||
'total' => 0,
|
||||
'page' => $page,
|
||||
'limit' => $limit,
|
||||
]);
|
||||
}
|
||||
|
||||
// 查询总数
|
||||
$total = Articles::where('cate', $categoryInfo['id'])
|
||||
->where('delete_time', null)
|
||||
->where('status', 2)
|
||||
->count();
|
||||
|
||||
// 查询文章
|
||||
$articles = Articles::where('cate', $categoryInfo['id'])
|
||||
->where('delete_time', null)
|
||||
->where('status', 2)
|
||||
->order('top', 'desc')
|
||||
->order('recommend', 'desc')
|
||||
->order('sort', 'desc')
|
||||
->order('publishdate', 'desc')
|
||||
->page($page, $limit)
|
||||
->select();
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => 'success',
|
||||
'list' => $articles,
|
||||
'total' => $total,
|
||||
'page' => $page,
|
||||
'limit' => $limit,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取技术中心子分类
|
||||
* @return Json
|
||||
*/
|
||||
public function getTechnologyCategories(): Json
|
||||
{
|
||||
// 获取"技术中心"主分类
|
||||
$parentCategory = ArticlesCategory::where('name', '技术中心')
|
||||
->where('delete_time', null)
|
||||
->find();
|
||||
|
||||
if (!$parentCategory) {
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => 'success',
|
||||
'data' => []
|
||||
]);
|
||||
}
|
||||
|
||||
// 查找所有子分类
|
||||
$subCategories = ArticlesCategory::where('cid', $parentCategory['id'])
|
||||
->where('delete_time', null)
|
||||
->field('id,name,desc,sort,image')
|
||||
->order('sort', 'desc')
|
||||
->select();
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => 'success',
|
||||
'data' => $subCategories
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取技术中心
|
||||
* @return Json
|
||||
*/
|
||||
public function getTechnologyCenter(): Json
|
||||
{
|
||||
// 1. 分页参数规范化
|
||||
$page = max(1, intval(Request::param('page', 1)));
|
||||
$limit = max(1, min(50, intval(Request::param('limit', 10))));
|
||||
|
||||
// 获取分类ID参数(可选)
|
||||
$categoryId = Request::param('category_id', null);
|
||||
|
||||
// 2. 第一步:获取“技术中心”主分类的 id
|
||||
$parentCategory = ArticlesCategory::where('name', '技术中心')
|
||||
->where('delete_time', null)
|
||||
->find();
|
||||
|
||||
// print_r($parentCategory['id']);
|
||||
|
||||
if (!$parentCategory) {
|
||||
return $this->emptyResponse($page, $limit);
|
||||
}
|
||||
|
||||
// 3. 第二步:查找 cid 等于主分类 id 的所有子分类 id
|
||||
$subCategoryQuery = ArticlesCategory::where('cid', $parentCategory['id'])
|
||||
->where('delete_time', null);
|
||||
|
||||
// print_r($subCategoryQuery->select());
|
||||
|
||||
// 如果指定了分类ID,则只查询该分类
|
||||
if ($categoryId) {
|
||||
$subCategoryQuery->where('id', $categoryId);
|
||||
}
|
||||
|
||||
$subCategoryIds = $subCategoryQuery->column('id');
|
||||
|
||||
// print_r($subCategoryIds);
|
||||
|
||||
if (empty($subCategoryIds)) {
|
||||
return $this->emptyResponse($page, $limit);
|
||||
}
|
||||
|
||||
// 5. 第三步:查询 Articles 表,并限定字段
|
||||
$articleQuery = Articles::whereIn('cate', $subCategoryIds)
|
||||
->where('delete_time', null)
|
||||
->where('status', 2);
|
||||
|
||||
// 获取总数
|
||||
$total = (clone $articleQuery)->count();
|
||||
|
||||
// 获取列表:限定返回 id, title, desc, publishdate, image, likes, views
|
||||
$articles = $articleQuery->field('id,title,desc,publishdate,image,likes,views,cate')
|
||||
->order([
|
||||
'top' => 'desc',
|
||||
'recommend' => 'desc',
|
||||
'sort' => 'desc',
|
||||
'publishdate' => 'desc'
|
||||
])
|
||||
->page($page, $limit)
|
||||
->select();
|
||||
|
||||
// 如果文章没有image,查询对应分类的image
|
||||
$categoryImages = ArticlesCategory::whereIn('id', $subCategoryIds)->column('image', 'id');
|
||||
|
||||
foreach ($articles as &$article) {
|
||||
if (empty($article['image']) && isset($categoryImages[$article['cate']])) {
|
||||
$article['image'] = $categoryImages[$article['cate']];
|
||||
}
|
||||
}
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => 'success',
|
||||
'list' => $articles,
|
||||
'total' => $total,
|
||||
'page' => $page,
|
||||
'limit' => $limit,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 统一空返回
|
||||
*/
|
||||
private function emptyResponse($page, $limit): Json
|
||||
{
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => 'success',
|
||||
'list' => [],
|
||||
'total' => 0,
|
||||
'page' => $page,
|
||||
'limit' => $limit,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取金蝶新闻详情
|
||||
*/
|
||||
public function getKingdeeNewsDetail(int $id): Json
|
||||
{
|
||||
$article = Articles::where('id', $id)
|
||||
->where('delete_time', null)
|
||||
->where('status', 2)
|
||||
->field('id,title,cate,image,desc,author,content,publisher,publishdate,views,likes,is_trans,transurl')
|
||||
->find();
|
||||
|
||||
if (!$article) {
|
||||
return json(['code' => 404, 'msg' => '文章不存在', 'data' => null]);
|
||||
}
|
||||
|
||||
$cate = (int) $article['cate'];
|
||||
|
||||
// 转换为数组再处理
|
||||
$articleData = $article->toArray();
|
||||
// $articleData['catename'] = $this->getCategoryName($cate);
|
||||
|
||||
// 获取相关文章
|
||||
$articleData['relatedArticles'] = $this->getRelatedArticles($id, $cate);
|
||||
|
||||
// 获取上一篇下一篇
|
||||
$articleData['nextPreviousArticles'] = $this->getNextPreviousArticles($id, $cate);
|
||||
|
||||
// 增加浏览量
|
||||
Articles::where('id', $id)->inc('views')->update();
|
||||
|
||||
return json(['code' => 200, 'msg' => 'success', 'data' => $articleData]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取企业新闻详情
|
||||
*/
|
||||
public function getCompanyNewsDetail(int $id): Json
|
||||
{
|
||||
$article = Articles::where('id', $id)
|
||||
->where('delete_time', null)
|
||||
->where('status', 2)
|
||||
->find();
|
||||
|
||||
if (!$article) {
|
||||
return json(['code' => 404, 'msg' => '文章不存在', 'data' => null]);
|
||||
}
|
||||
|
||||
$cate = (int) $article['cate'];
|
||||
|
||||
// 转换为数组再处理
|
||||
$articleData = $article->toArray();
|
||||
$articleData['catename'] = $this->getCategoryName($cate);
|
||||
|
||||
// 获取相关文章
|
||||
$articleData['relatedArticles'] = $this->getRelatedArticles($id, $cate);
|
||||
|
||||
// 获取上一篇下一篇
|
||||
$articleData['nextPreviousArticles'] = $this->getNextPreviousArticles($id, $cate);
|
||||
|
||||
// 增加浏览量
|
||||
Articles::where('id', $id)->inc('views')->update();
|
||||
|
||||
return json(['code' => 200, 'msg' => 'success', 'data' => $articleData]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取上一篇下一篇
|
||||
* @param int $id 文章ID
|
||||
* @param int $cate 分类ID
|
||||
* @return array
|
||||
*/
|
||||
private function getNextPreviousArticles(int $id, int $cate): array
|
||||
{
|
||||
$nextArticle = Articles::where('id', '<', $id)
|
||||
->where('cate', $cate)
|
||||
->where('delete_time', null)
|
||||
->where('status', 2)
|
||||
->field('id,title')
|
||||
->find();
|
||||
|
||||
$previousArticle = Articles::where('id', '>', $id)
|
||||
->where('cate', $cate)
|
||||
->where('delete_time', null)
|
||||
->where('status', 2)
|
||||
->field('id,title')
|
||||
->find();
|
||||
|
||||
return [
|
||||
'code' => 200,
|
||||
'msg' => 'success',
|
||||
'next' => $nextArticle,
|
||||
'previous' => $previousArticle,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过分类 ID 获取分类名称
|
||||
* @param int $cateId 分类ID
|
||||
* @return string
|
||||
*/
|
||||
private function getCategoryName(int $id): string
|
||||
{
|
||||
$categoryInfo = ArticlesCategory::where('id', $id)
|
||||
->where('delete_time', null)
|
||||
->find();
|
||||
|
||||
if (!$categoryInfo) {
|
||||
return '未分类';
|
||||
}
|
||||
|
||||
return $categoryInfo['name'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取相关文章
|
||||
*/
|
||||
private function getRelatedArticles(int $id, int $cate): array
|
||||
{
|
||||
$articles = Articles::where('id', '<>', $id)
|
||||
->where('cate', $cate)
|
||||
->where('delete_time', null)
|
||||
->where('status', 2)
|
||||
->order('top', 'desc')
|
||||
->order('recommend', 'desc')
|
||||
->order('sort', 'desc')
|
||||
->order('id', 'desc')
|
||||
->limit(5)
|
||||
->select();
|
||||
|
||||
foreach ($articles as &$article) {
|
||||
$article['cate'] = $this->getCategoryName($article['cate']);
|
||||
}
|
||||
|
||||
return [
|
||||
'code' => 200,
|
||||
'msg' => 'success',
|
||||
'list' => $articles,
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user