using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using TEAMModelOS.SDK.Context.Exception;
using TEAMModelOS.SDK.Extension.DataResult.JsonRpcRequest;
using TEAMModelOS.SDK.Extension.DataResult.JsonRpcResponse;
using TEAMModelOS.SDK.Helper.Common.JsonHelper.JsonPath;
using static System.Text.Json.JsonElement;
namespace TEAMModelOS.API.Controllers.Core
{
[Route("api/[controller]")]
[ApiController]
// [Authorize]
public class CommonController : BaseController
{
public readonly IWebHostEnvironment webHostEnvironment;
public CommonController(IWebHostEnvironment _webHostEnvironment) {
webHostEnvironment = _webHostEnvironment;
}
///
/// bool =true 獲取全部 false 則從request.lang 篩選指定語言的,默認zh-CN
///
///
///
[HttpPost("SubjectDefault")]
public async Task SubjectDefault(JosnRPCRequest request)
{
JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
string contentRootPath = webHostEnvironment.ContentRootPath;
JsonDocument document= await GetJson(contentRootPath, "Subject/SubjectDefault");
if (request.@params) {
List jsonElements = new List();
foreach (JsonElement element in document.RootElement.EnumerateArray())
{
string date = element.GetProperty("lang").GetString();
if (!string.IsNullOrEmpty(date)&&date.Equals(request.lang)) {
jsonElements.Add(element);
}
}
return builder.Data(jsonElements).build();
}
else
{
return builder.Data(document.RootElement).build();
}
}
public static async Task GetJson(string contentRootPath, string name)
{
//string webRootPath = _hostingEnvironment.WebRootPath;
try
{
string path = contentRootPath + "/JsonFile/" + name + ".json";
//获取正在占用的文件
var fs = await System.IO. File.ReadAllTextAsync(path,Encoding.UTF8);
return JsonDocument.Parse(fs);
}
catch (Exception e)
{
new BizException(e.Message);
}
return null;
}
}
}