CommonController.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using Microsoft.AspNetCore.Hosting;
  2. using Microsoft.AspNetCore.Mvc;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Text.Json;
  9. using System.Threading.Tasks;
  10. using TEAMModelOS.SDK.Context.Exception;
  11. using TEAMModelOS.SDK.Extension.DataResult.JsonRpcRequest;
  12. using TEAMModelOS.SDK.Extension.DataResult.JsonRpcResponse;
  13. using TEAMModelOS.SDK.Helper.Common.JsonHelper.JsonPath;
  14. using static System.Text.Json.JsonElement;
  15. namespace TEAMModelOS.API.Controllers.Core
  16. {
  17. [Route("api/[controller]")]
  18. [ApiController]
  19. // [Authorize]
  20. public class CommonController : BaseController
  21. {
  22. public readonly IWebHostEnvironment webHostEnvironment;
  23. public CommonController(IWebHostEnvironment _webHostEnvironment) {
  24. webHostEnvironment = _webHostEnvironment;
  25. }
  26. /// <summary>
  27. /// bool =true 獲取全部 false 則從request.lang 篩選指定語言的,默認zh-CN
  28. /// </summary>
  29. /// <param name="request"></param>
  30. /// <returns></returns>
  31. [HttpPost("SubjectDefault")]
  32. public async Task<BaseJosnRPCResponse> SubjectDefault(JosnRPCRequest<bool> request)
  33. {
  34. JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
  35. string contentRootPath = webHostEnvironment.ContentRootPath;
  36. JsonDocument document= await GetJson(contentRootPath, "Subject/SubjectDefault");
  37. if (request.@params) {
  38. List<JsonElement> jsonElements = new List<JsonElement>();
  39. foreach (JsonElement element in document.RootElement.EnumerateArray())
  40. {
  41. string date = element.GetProperty("lang").GetString();
  42. if (!string.IsNullOrEmpty(date)&&date.Equals(request.lang)) {
  43. jsonElements.Add(element);
  44. }
  45. }
  46. return builder.Data(jsonElements).build();
  47. }
  48. else
  49. {
  50. return builder.Data(document.RootElement).build();
  51. }
  52. }
  53. public static async Task<JsonDocument> GetJson(string contentRootPath, string name)
  54. {
  55. //string webRootPath = _hostingEnvironment.WebRootPath;
  56. try
  57. {
  58. string path = contentRootPath + "/JsonFile/" + name + ".json";
  59. //获取正在占用的文件
  60. var fs = await System.IO. File.ReadAllTextAsync(path,Encoding.UTF8);
  61. return JsonDocument.Parse(fs);
  62. }
  63. catch (Exception e)
  64. {
  65. new BizException(e.Message);
  66. }
  67. return null;
  68. }
  69. }
  70. }