253 lines
7.2 KiB
PHP
253 lines
7.2 KiB
PHP
<?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();
|
||
|
||
var_dump($newsCenterCategory);
|
||
|
||
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',
|
||
'publish_date' => 'desc',
|
||
'sort' => 'desc',
|
||
'id' => 'desc'
|
||
])
|
||
->field([
|
||
'id',
|
||
'title',
|
||
'content',
|
||
'publish_date',
|
||
'DAY(publish_date) AS `date`',
|
||
'DATE_FORMAT(publish_date, "%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' => []
|
||
]);
|
||
}
|
||
}
|
||
}
|