tp/app/index/controller/Index.php

178 lines
5.0 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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;
use think\facade\Env;
class Index extends BaseController
{
public function 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
]);
}
/**
* 获取前端导航接口
* @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
]);
}
}
}