themeService = new ThemeService(); } public function index() { return view('index/index'); } /** * 前端初始化接口 - 返回当前模板和填充数据 * @return \think\response\Json */ public function init() { // 获取租户ID(从请求参数) $tid = Request::param('tid', 0, 'int'); // 从TemplateSiteConfig获取配置(根据租户ID) $config = null; if ($tid > 0) { $config = TemplateSiteConfig::where('tid', $tid) ->where('key', 'current_theme') ->find(); } $themeKey = $config['value'] ?? 'default'; // 模板路径:/themes/{theme_key}/,优先使用index.php,其次index.html $themeBasePath = root_path() . 'public' . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $themeKey; if (is_file($themeBasePath . DIRECTORY_SEPARATOR . 'index.php')) { $themePath = '/themes/' . $themeKey . '/index.php'; } else { $themePath = '/themes/' . $themeKey . '/index.html'; } return json([ 'code' => 200, 'msg' => 'success', 'data' => [ 'theme_key' => $themeKey, 'theme_path' => $themePath ] ]); } /** * 获取日志列表 */ 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 ]); } } /** * 前端底部数据 * @return \think\response\Json */ public function getFooterData() { try { $footerData = SystemSiteSettings::where('delete_time', null) ->field('label, value') ->select() ->toArray(); return json([ 'code' => 200, 'msg' => 'success', 'data' => $footerData ]); } catch (DbException $e) { return json([ 'code' => 500, 'msg' => 'fail:' . $e->getMessage(), 'data' => null ]); } } }