更新首页样式及跨域

This commit is contained in:
2026-01-28 23:01:54 +08:00
parent 89d7761887
commit aa2612bcb9
8 changed files with 303 additions and 197 deletions
@@ -1,5 +1,6 @@
<?php
declare (strict_types = 1);
declare(strict_types=1);
namespace app\index\controller\Article;
@@ -16,17 +17,19 @@ use think\db\exception\DbException;
class ArticleController extends BaseController
{
/**
* 获取新闻中心顶部4篇文章
* 获取技术中心顶部4篇文章
* @return Json
*/
public function getNewsCenterTop4(): Json
{
try {
// 1. 查询新闻中心主分类
$newsCenterCategory = ArticlesCategory::where('name', '新闻中心')
// 1. 查询技术中心主分类
$newsCenterCategory = ArticlesCategory::where('name', '技术中心')
->where('delete_time', null)
->find();
var_dump($newsCenterCategory);
if (!$newsCenterCategory) {
return json([
'code' => 200,
@@ -34,18 +37,18 @@ class ArticleController extends BaseController
'list' => [],
]);
}
// 2. 获取新闻中心及其子分类的ID列表
// 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,
@@ -53,7 +56,7 @@ class ArticleController extends BaseController
'list' => [],
]);
}
// 3. 查询符合条件的文章,按指定优先级排序,取最新4篇
$articles = Articles::published()
->whereIn('cate', $categoryIds)
@@ -75,7 +78,7 @@ class ArticleController extends BaseController
])
->limit(4)
->select();
// 4. 处理文章数据,去除HTML标签并生成desc字段
foreach ($articles as &$article) {
// 使用PHP的strip_tags函数去除HTML标签,然后截取前100个字符
@@ -83,7 +86,7 @@ class ArticleController extends BaseController
// 移除原始content字段,减少返回数据大小
unset($article['content']);
}
return json([
'code' => 200,
'msg' => 'success',
@@ -92,10 +95,10 @@ class ArticleController extends BaseController
} catch (\Exception $e) {
// 打印完整错误信息到日志
$errorMsg = '错误信息:' . $e->getMessage() . ' | 错误行号:' . $e->getLine() . ' | 执行SQL' . Articles::getLastSql();
trace('新闻中心文章查询失败: ' . $errorMsg, 'error');
trace('技术中心文章查询失败: ' . $errorMsg, 'error');
return json([
'code' => 500,
'msg' => '新闻中心文章查询失败,请稍后重试',
'msg' => '技术中心文章查询失败,请稍后重试',
'list' => []
]);
}
@@ -118,7 +121,7 @@ class ArticleController extends BaseController
try {
$article = Articles::where('id', $id)->where('delete_time', null)->find();
if (!$article) {
return json([
'code' => 404,
@@ -164,7 +167,7 @@ class ArticleController extends BaseController
try {
$article = Articles::where('id', $id)->where('delete_time', null)->find();
if (!$article) {
return json([
'code' => 404,
@@ -210,15 +213,15 @@ class ArticleController extends BaseController
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,
@@ -226,10 +229,10 @@ class ArticleController extends BaseController
'list' => []
]);
}
$article->dec('likes');
$article->save();
return json([
'code' => 200,
'msg' => '取消点赞成功',
@@ -246,4 +249,4 @@ class ArticleController extends BaseController
]);
}
}
}
}
+45 -1
View File
@@ -9,12 +9,56 @@ use app\index\BaseController;
use app\model\FrontMenu;
use app\model\OnePage;
use think\db\exception\DbException;
use think\facade\Env;
class Index extends BaseController
{
public function index()
{
return '您好!这是一个[index]示例应用';
return view('index/index');
}
/**
* 获取日志列表
*/
public function getLogs()
{
$logPath = Env::get('runtime_path') . 'log/';
$logs = [];
// 读取最近的日志文件
$files = glob($logPath . '*.log');
if ($files) {
// 按修改时间排序,最新的在前
usort($files, function($a, $b) {
return filemtime($b) - filemtime($a);
});
// 读取最新的日志文件
$latestFile = reset($files);
if (file_exists($latestFile)) {
$content = file_get_contents($latestFile);
$lines = explode("\n", $content);
// 倒序遍历,只取最近的100条
$count = 0;
for ($i = count($lines) - 1; $i >= 0 && $count < 100; $i--) {
if (!empty(trim($lines[$i]))) {
$logs[] = $lines[$i];
$count++;
}
}
// 再倒序回来,使最新的日志在最后
$logs = array_reverse($logs);
}
}
return json([
'code' => 200,
'msg' => 'success',
'data' => $logs
]);
}
/**