CompetitionAPI_dotnet/CompetitionAPI/Controllers/back/GetFaultTreeController.cs

65 lines
2.1 KiB
C#

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using Competition.Common.Util;
using CompetitionAPI.Util;
namespace CompetitionAPI.Controllers.back
{
[Route("api/[controller]")]
[ApiController]
public class GetFaultTreeController : Controller
{
private readonly IWebHostEnvironment _webHostEnvironment;
Competition.Mysql.BLL.pow_exam exam_bll = new Competition.Mysql.BLL.pow_exam();
public GetFaultTreeController(IWebHostEnvironment webHostEnvironment)
{
_webHostEnvironment = webHostEnvironment;
}
/// <summary>
/// 获取故障树接口
/// </summary>
/// <returns></returns>
[Authorize]
[HttpGet]
[APIFilter]
public JsonResult Index()
{
try
{
var tree_list = ReadTree();
return Json(Tool.GetJsonWithCode(APICode.Success, tree_list));
}
catch (Exception ex)
{
LogHelper.WriteLog(ex.Message + ",行号:" + ex.StackTrace);
return Json(Tool.GetJsonWithCode(APICode.Fail, "发生错误,请联系管理员。"));
}
}
/// <summary>
/// 获取树结构json
/// </summary>
/// <returns></returns>
private List<Competition.Mysql.Other.fault_tree> ReadTree()
{
var tree_list = new List<Competition.Mysql.Other.fault_tree>();
string fault_tree_file = _webHostEnvironment.WebRootPath + "/Json/FaultTree.json";
//读取json文件
using (StreamReader file = System.IO.File.OpenText(fault_tree_file))
{
using (JsonTextReader reader = new JsonTextReader(file))
{
tree_list = JsonConvert.DeserializeObject<List<Competition.Mysql.Other.fault_tree>>(JToken.ReadFrom(reader).ToString());
}
}
return tree_list;
}
}
}