123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- 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;
- }
- /// <summary>
- /// bool =true 獲取全部 false 則從request.lang 篩選指定語言的,默認zh-CN
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("SubjectDefault")]
- public async Task<BaseJosnRPCResponse> SubjectDefault(JosnRPCRequest<bool> request)
- {
- JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
- string contentRootPath = webHostEnvironment.ContentRootPath;
- JsonDocument document= await GetJson(contentRootPath, "Subject/SubjectDefault");
- if (request.@params) {
- List<JsonElement> jsonElements = new List<JsonElement>();
- 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<JsonDocument> 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;
- }
- }
- }
|