更新首页样式及跨域
This commit is contained in:
@@ -14,7 +14,7 @@ use app\model\FrontMenu;
|
||||
|
||||
class FrontMenuController extends BaseController
|
||||
{
|
||||
/**
|
||||
/**
|
||||
* 获取前端导航接口
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
@@ -163,7 +163,7 @@ class FrontMenuController extends BaseController
|
||||
} catch (DbException $e) {
|
||||
// 记录失败日志
|
||||
$this->logFail('前端导航管理', '更新前端导航', $e->getMessage());
|
||||
return json([
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => 'fail:' . $e->getMessage(),
|
||||
'data' => $e->getTraceAsString()
|
||||
@@ -176,7 +176,7 @@ class FrontMenuController extends BaseController
|
||||
{
|
||||
try {
|
||||
// 检查是否有子前端导航
|
||||
$hasChildren = FrontMenu::where('pid', $id)
|
||||
$hasChildren = FrontMenu::where('pid', $id)
|
||||
->where('pid', $id)
|
||||
->where('delete_time', null)
|
||||
->count() > 0;
|
||||
|
||||
@@ -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
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -24,4 +24,8 @@ Route::post('articleUnlikes/:id', 'app\index\controller\Article\ArticleControlle
|
||||
|
||||
// --- 前端导航与单页路由 ---
|
||||
Route::get('headmenu', 'app\index\controller\Index@getHeadMenu');
|
||||
Route::rule('onepage/:path', 'app\index\controller\Index@getOnePageByPath', 'GET')->pattern(['path' => '.*']);
|
||||
Route::rule('onepage/:path', 'app\index\controller\Index@getOnePageByPath', 'GET')->pattern(['path' => '.*']);
|
||||
|
||||
// --- 日志相关路由 ---
|
||||
Route::get('index/getLogs', 'app\index\controller\Index@getLogs');
|
||||
Route::get('getLogs', 'app\index\controller\Index@getLogs');
|
||||
@@ -0,0 +1,104 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>后端运行状态</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
margin: 20px;
|
||||
background-color: #f5f5f5;
|
||||
}
|
||||
h1 {
|
||||
color: #333;
|
||||
text-align: center;
|
||||
}
|
||||
p {
|
||||
color: #666;
|
||||
text-align: center;
|
||||
}
|
||||
#log-content {
|
||||
background-color: #fff;
|
||||
border-radius: 5px;
|
||||
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
||||
padding: 20px;
|
||||
margin: 20px auto;
|
||||
max-width: 1200px;
|
||||
max-height: 600px;
|
||||
overflow-y: auto;
|
||||
}
|
||||
#log-content div {
|
||||
padding: 5px 0;
|
||||
border-bottom: 1px solid #eee;
|
||||
font-size: 14px;
|
||||
line-height: 1.4;
|
||||
}
|
||||
#log-content div:hover {
|
||||
background-color: #f9f9f9;
|
||||
}
|
||||
#log-content p {
|
||||
text-align: left;
|
||||
}
|
||||
.refresh-info {
|
||||
text-align: center;
|
||||
font-size: 12px;
|
||||
color: #999;
|
||||
margin-top: 10px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>后端服务运行状态</h1>
|
||||
<p>实时监控系统运行情况</p>
|
||||
<div id="log-content">
|
||||
<p>正在加载日志...</p>
|
||||
</div>
|
||||
<div class="refresh-info">
|
||||
<span id="last-refresh">最后刷新: 从未</span>
|
||||
<span> | </span>
|
||||
<span>每5秒自动刷新</span>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
// 加载日志
|
||||
function loadLogs() {
|
||||
fetch('/getLogs')
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
const logContent = document.getElementById('log-content');
|
||||
if (data.code === 200) {
|
||||
logContent.innerHTML = '';
|
||||
if (data.data && data.data.length > 0) {
|
||||
data.data.forEach(log => {
|
||||
const logItem = document.createElement('div');
|
||||
logItem.textContent = log;
|
||||
logContent.appendChild(logItem);
|
||||
});
|
||||
} else {
|
||||
logContent.innerHTML = '<p>暂无日志记录</p>';
|
||||
}
|
||||
} else {
|
||||
logContent.innerHTML = '<p>加载日志失败: ' + (data.msg || '未知错误') + '</p>';
|
||||
}
|
||||
|
||||
// 更新最后刷新时间
|
||||
const now = new Date();
|
||||
const timeStr = now.toLocaleString('zh-CN');
|
||||
document.getElementById('last-refresh').textContent = '最后刷新: ' + timeStr;
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('加载日志失败:', error);
|
||||
const logContent = document.getElementById('log-content');
|
||||
logContent.innerHTML = '<p>加载日志失败: 网络错误</p>';
|
||||
});
|
||||
}
|
||||
|
||||
// 初始加载日志
|
||||
loadLogs();
|
||||
|
||||
// 定时刷新日志,每5秒一次
|
||||
setInterval(loadLogs, 5000);
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user