JdUnionController.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using molilian.core;
  2. using dodohold.core;
  3. using Microsoft.AspNetCore.Mvc;
  4. using System.Text.Json;
  5. using System.Net;
  6. using static QRCoder.PayloadGenerator;
  7. using MySqlX.XDevAPI;
  8. using OfficeOpenXml.FormulaParsing.LexicalAnalysis;
  9. namespace molilian.api.Controllers
  10. {
  11. [ApiController]
  12. [MyAuthorize("admin")]
  13. [Route("api/[controller]/[action]")]
  14. [Route("coupon_api/[controller]/[action]")]
  15. public class JdUnionController : ControllerBase
  16. {
  17. readonly IAuthorizationProvider provider = new AdminProvider();
  18. protected IHttpContextAccessor _accessor;
  19. public JdUnionController(IHttpContextAccessor accessor)
  20. {
  21. _accessor = accessor;
  22. }
  23. [HttpPost]
  24. public async Task<ActionResult> updateCookies([FromBody] JsonElement form)
  25. {
  26. var clientIp = _accessor.HttpContext.GetUserIp();
  27. var token = provider.Get(_accessor.HttpContext);
  28. string user_agent = _accessor.HttpContext.Request.UserAgent();
  29. string cookie = form.Read<string>("cookie", string.Empty);
  30. var accountId = JdPoolCore.UpdateCookies(cookie, user_agent);
  31. bool success = accountId > 0;
  32. if (success)
  33. {
  34. //string desc = ObjectComparer.PrintCompareToString(old, form).Trim();
  35. OperationLogCore.LogOperation(token.AccessKey, clientIp, $"tk_pool:{accountId}:cookie", string.Empty, cookie);
  36. }
  37. return new APIResult(new
  38. {
  39. data = new { success, msg = success ? "更新成功" : "更新失败,请检查输入的cookie" },
  40. });
  41. }
  42. /// <summary>
  43. /// 账号列表
  44. /// </summary>
  45. /// <param name="form"></param>
  46. /// <returns></returns>
  47. [HttpPost]
  48. public ActionResult list([FromBody] JsonElement form)
  49. {
  50. var token = provider.Get(_accessor.HttpContext);
  51. int page = form.Read("current", 1);
  52. int size = form.Read("pageSize", 10);
  53. string keyword = form.Read("name", string.Empty);
  54. bool getTotal = form.Read("getTotal", true);
  55. string _status = form.Read("status", string.Empty);
  56. int status = _status switch
  57. {
  58. "online" => 1,
  59. "offline" => 0,
  60. _ => -1,
  61. };
  62. var lastTime = form.PathReadArray<string>("lastTime[]");
  63. string sort = form.Read<string>("sort");
  64. string order = form.Read<string>("order");
  65. string filter = string.Empty;
  66. if (!string.IsNullOrEmpty(keyword))
  67. {
  68. filter += $" AND (`name` LIKE @keyword OR `description` LIKE @keyword)";
  69. keyword = $"%{keyword}%";
  70. }
  71. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  72. if (lastTime.Count == 2)
  73. {
  74. if (DateTime.TryParse(lastTime[0], out stime) && DateTime.TryParse(lastTime[1], out etime))
  75. {
  76. etime = etime.AddDays(1).AddSeconds(-1);
  77. filter += $" AND last_time BETWEEN @stime AND @etime";
  78. }
  79. }
  80. if (status != -1)
  81. {
  82. filter += $" AND status=@status";
  83. }
  84. filter = filter.StringTrimStart(" AND ");
  85. //排序
  86. string orderBy = "id DESC";
  87. if (!string.IsNullOrEmpty(order))
  88. {
  89. order = "descending".Equals(order) ? "DESC" : "ASC";
  90. orderBy = sort switch
  91. {
  92. //"num" => $"num {order}",
  93. _ => $"{sort} {order}",
  94. };
  95. }
  96. using var conn = DBContext.GetOpenConnection();
  97. var result = new DBContext.Table(conn, "jd_pool")
  98. .Where(filter, new { keyword, status, stime, etime })
  99. .Page(size, page)
  100. .Order(orderBy)
  101. .PageList<JdPoolDTO>(getTotal);
  102. foreach (var item in result.List)
  103. {
  104. item.app_secret = string.Empty;
  105. item.cookies = string.Empty;
  106. }
  107. return new APIResult(new { data = result });
  108. }
  109. [HttpPost]
  110. public async Task<ActionResult> update([FromBody] JsonElement form)
  111. {
  112. var clientIp = _accessor.HttpContext.GetUserIp();
  113. var token = provider.Get(_accessor.HttpContext);
  114. int id = form.Read<int>("id", 0);
  115. string name = form.Read<string>("name", string.Empty);
  116. string val = form.Read<string>("val", string.Empty);
  117. if (id == 0)
  118. {
  119. return new APIResult(new { data = new { success = false, msg = "更新失败,请检查输入的cookie" } });
  120. }
  121. int result;
  122. switch (name)
  123. {
  124. case "enable_parse":
  125. case "enable_coupon":
  126. case "status":
  127. bool bVal = val.Equals("True");
  128. result = new DBContext.Table("tk_pool")
  129. .Add(name, bVal)
  130. .Add("last_time", DateTime.Now)
  131. .Where("id=@id", new { id })
  132. .Update();
  133. break;
  134. default:
  135. return new APIResult(new { data = new { success = false, msg = "更新失败,未授权操作" } });
  136. }
  137. bool success = result > 0;
  138. if (success)
  139. {
  140. OperationLogCore.LogOperation(token.AccessKey, clientIp, $"jd_pool:{id}:{name}", string.Empty, val);
  141. await EndPointCore.NotifyReload(true);
  142. }
  143. return new APIResult(new
  144. {
  145. data = new { success, msg = success ? "更新成功" : "更新失败,请检查输入信息" },
  146. });
  147. }
  148. }
  149. }