add: home select exp
This commit is contained in:
55
server/src/TutorialController.cs
Normal file
55
server/src/TutorialController.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace server.Controllers;
|
||||
|
||||
/// <summary>
|
||||
/// 教程 API
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class TutorialController : ControllerBase
|
||||
{
|
||||
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
private readonly IWebHostEnvironment _environment;
|
||||
|
||||
public TutorialController(IWebHostEnvironment environment)
|
||||
{
|
||||
_environment = environment;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有可用的教程目录
|
||||
/// </summary>
|
||||
/// <returns>教程目录列表</returns>
|
||||
[HttpGet]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public IActionResult GetTutorials()
|
||||
{
|
||||
try
|
||||
{
|
||||
// 获取文档目录
|
||||
string docPath = Path.Combine(_environment.WebRootPath, "doc");
|
||||
|
||||
if (!Directory.Exists(docPath))
|
||||
{
|
||||
return Ok(new { tutorials = new List<string>() });
|
||||
}
|
||||
|
||||
// 获取所有子目录
|
||||
var directories = Directory.GetDirectories(docPath)
|
||||
.Select(Path.GetFileName)
|
||||
.Where(dir => !string.IsNullOrEmpty(dir))
|
||||
.ToList();
|
||||
|
||||
return Ok(new { tutorials = directories });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "获取教程目录失败");
|
||||
return StatusCode(500, new { error = "无法读取教程目录" });
|
||||
}
|
||||
}
|
||||
}
|
30
server/src/TutorialService.ts
Normal file
30
server/src/TutorialService.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
// 此接口提供获取例程目录服务
|
||||
// GET /api/tutorials 返回所有可用的例程目录
|
||||
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
import { Request, Response } from 'express';
|
||||
|
||||
// 获取当前文件的目录
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
const publicDir = path.resolve(__dirname, '../public');
|
||||
|
||||
export function getTutorials(req: Request, res: Response) {
|
||||
try {
|
||||
const docDir = path.join(publicDir, 'doc');
|
||||
|
||||
// 读取doc目录下的所有文件夹
|
||||
const entries = fs.readdirSync(docDir, { withFileTypes: true });
|
||||
const dirs = entries
|
||||
.filter(dirent => dirent.isDirectory())
|
||||
.map(dirent => dirent.name);
|
||||
|
||||
// 返回文件夹列表
|
||||
res.json({ tutorials: dirs });
|
||||
} catch (error) {
|
||||
console.error('获取例程目录失败:', error);
|
||||
res.status(500).json({ error: '无法读取例程目录' });
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user