first commot
This commit is contained in:
@@ -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
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user