ApiAccountController.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using molilian.core;
  2. using dodohold.core;
  3. using Microsoft.AspNetCore.Mvc;
  4. using System.Text.Json;
  5. using static molilian.core.ChartDataCore;
  6. using TencentCloud.Soe.V20180724.Models;
  7. using TencentCloud.Mrs.V20200910.Models;
  8. using System.Xml.Linq;
  9. using System.Security.Cryptography;
  10. using TencentCloud.Bi.V20220105.Models;
  11. namespace molilian.api.Controllers
  12. {
  13. [ApiController]
  14. [MyAuthorize("admin")]
  15. [Route("api/[controller]/[action]")]
  16. public class ApiAccountController : ControllerBase
  17. {
  18. readonly IAuthorizationProvider provider = new AdminProvider();
  19. protected IHttpContextAccessor _accessor;
  20. public ApiAccountController(IHttpContextAccessor accessor)
  21. {
  22. _accessor = accessor;
  23. }
  24. [HttpPost]
  25. public ActionResult list([FromBody] JsonElement form)
  26. {
  27. var token = provider.Get(_accessor.HttpContext);
  28. int page = form.Read("current", 1);
  29. int size = form.Read("pageSize", 10);
  30. bool getTotal = form.Read("getTotal", true);
  31. string sort = form.Read<string>("sort");
  32. string order = form.Read<string>("order");
  33. string filter = string.Empty;
  34. var keyword = form.Read("keyword", string.Empty);
  35. if (!string.IsNullOrEmpty(keyword))
  36. {
  37. filter += $" AND (name like @keyword OR description like @keyword OR api_key like @keyword)";
  38. keyword = $"%{keyword}%";
  39. }
  40. filter = filter.StringTrimStart(" AND ");
  41. string orderBy = "id DESC";
  42. if (!string.IsNullOrEmpty(order))
  43. {
  44. order = "descending".Equals(order) ? "DESC" : "ASC";
  45. orderBy = sort switch
  46. {
  47. _ => $"{sort} {order}",
  48. };
  49. }
  50. var result = new DBContext.Table("api_account")
  51. .Where(filter, new { keyword })
  52. .Page(size, page)
  53. .Order(orderBy)
  54. .PageList<dynamic>(getTotal);
  55. return new APIResult(new { data = result });
  56. }
  57. [HttpPost]
  58. public async Task<ActionResult> update([FromBody] ApiAccountDTO data)
  59. {
  60. var clientIp = _accessor.HttpContext.GetUserIp();
  61. var token = provider.Get(_accessor.HttpContext);
  62. using var conn = DBContext.GetOpenConnection();
  63. if (data.id == 0) return new APIResult(new { data = new { success = false, msg = "更新失败,请检查输入参数" } });
  64. var item = new DBContext.Table(conn, "api_account").Get<ApiAccountDTO>("id=@id", new { data.id });
  65. if (item == null) return new APIResult(new { data = new { success = false, msg = "更新失败,记录不存在" } });
  66. data.last_time = DateTime.Now;
  67. var result = ApiAccountCore.Update(data, conn);
  68. bool success = result > 0;
  69. if (success)
  70. {
  71. string desc = ObjectComparer.PrintCompareToString(item, data).Trim();
  72. OperationLogCore.LogOperation(token.AccessKey, clientIp, $"api_account:{data.name}", item.Convert2Json(), desc);
  73. }
  74. return new APIResult(new
  75. {
  76. data = new { success, msg = success ? "更新成功" : "更新失败,请检查输入信息" },
  77. });
  78. }
  79. [HttpPost]
  80. public async Task<ActionResult> create([FromBody] ApiAccountDTO data)
  81. {
  82. var clientIp = _accessor.HttpContext.GetUserIp();
  83. var token = provider.Get(_accessor.HttpContext);
  84. data.create_time = DateTime.Now;
  85. data.last_time = DateTime.Now;
  86. using var conn = DBContext.GetOpenConnection();
  87. var result = ApiAccountCore.Create(data, conn);
  88. bool success = result > 0;
  89. if (success)
  90. {
  91. OperationLogCore.LogOperation(token.AccessKey, clientIp, $"api_account:{data.name}", string.Empty, data.Convert2Json());
  92. }
  93. return new APIResult(new
  94. {
  95. data = new { success, msg = success ? "更新成功" : "更新失败,请检查输入信息" },
  96. });
  97. }
  98. }
  99. }