JdUnionController.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. using WebSocketSharp.Net;
  10. namespace molilian.api.Controllers
  11. {
  12. [ApiController]
  13. [MyAuthorize("admin")]
  14. [Route("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. cookie = cookie.Replace("\r", "").Replace("\n", "").Trim();
  31. if (!cookie.EndsWith(";")) cookie += ";";
  32. var accountId = JdPoolCore.UpdateCookies(cookie, user_agent);
  33. bool success = accountId > 0;
  34. if (success)
  35. {
  36. //string desc = ObjectComparer.PrintCompareToString(old, form).Trim();
  37. OperationLogCore.LogOperation(token.AccessKey, clientIp, $"jd_pool:{accountId}:cookie", string.Empty, cookie);
  38. }
  39. return new APIResult(new
  40. {
  41. data = new { success, msg = success ? "更新成功" : "更新失败,请检查输入的cookie" },
  42. });
  43. }
  44. /// <summary>
  45. /// 账号列表
  46. /// </summary>
  47. /// <param name="form"></param>
  48. /// <returns></returns>
  49. [HttpPost]
  50. public ActionResult list([FromBody] JsonElement form)
  51. {
  52. var token = provider.Get(_accessor.HttpContext);
  53. int page = form.Read("current", 1);
  54. int size = form.Read("pageSize", 10);
  55. string keyword = form.Read("name", string.Empty);
  56. bool getTotal = form.Read("getTotal", true);
  57. string _status = form.Read("status", string.Empty);
  58. int status = _status switch
  59. {
  60. "online" => 1,
  61. "offline" => 0,
  62. _ => -1,
  63. };
  64. var lastTime = form.PathReadArray<string>("lastTime[]");
  65. string sort = form.Read<string>("sort");
  66. string order = form.Read<string>("order");
  67. string filter = " is_hide=0";
  68. if (!string.IsNullOrEmpty(keyword))
  69. {
  70. filter += $" AND (`name` LIKE @keyword OR `description` LIKE @keyword)";
  71. keyword = $"%{keyword}%";
  72. }
  73. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  74. if (lastTime.Count == 2)
  75. {
  76. if (DateTime.TryParse(lastTime[0], out stime) && DateTime.TryParse(lastTime[1], out etime))
  77. {
  78. etime = etime.AddDays(1).AddSeconds(-1);
  79. filter += $" AND last_time BETWEEN @stime AND @etime";
  80. }
  81. }
  82. if (status != -1)
  83. {
  84. filter += $" AND status=@status";
  85. }
  86. filter = filter.StringTrimStart(" AND ");
  87. //排序
  88. string orderBy = "id DESC";
  89. if (!string.IsNullOrEmpty(order))
  90. {
  91. order = "descending".Equals(order) ? "DESC" : "ASC";
  92. orderBy = sort switch
  93. {
  94. //"num" => $"num {order}",
  95. _ => $"{sort} {order}",
  96. };
  97. }
  98. using var conn = DBContext.GetOpenConnection();
  99. var result = new DBContext.Table(conn, "jd_pool")
  100. .Where(filter, new { keyword, status, stime, etime })
  101. .Page(size, page)
  102. .Order(orderBy)
  103. .PageList<JdPoolDTO>(getTotal);
  104. foreach (var item in result.List)
  105. {
  106. item.app_secret = string.Empty;
  107. item.cookies = string.Empty;
  108. }
  109. return new APIResult(new { data = result });
  110. }
  111. [HttpPost]
  112. public async Task<ActionResult> update([FromBody] JsonElement form)
  113. {
  114. var clientIp = _accessor.HttpContext.GetUserIp();
  115. var token = provider.Get(_accessor.HttpContext);
  116. int id = form.Read<int>("id", 0);
  117. string name = form.Read<string>("name", string.Empty);
  118. string val = form.Read<string>("val", string.Empty);
  119. if (id == 0)
  120. {
  121. return new APIResult(new { data = new { success = false, msg = "更新失败,请检查输入的cookie" } });
  122. }
  123. int result;
  124. switch (name)
  125. {
  126. case "enable_parse":
  127. case "enable_coupon":
  128. case "status":
  129. bool bVal = val.Equals("True");
  130. result = new DBContext.Table("jd_pool")
  131. .Add(name, bVal)
  132. .Add("last_time", DateTime.Now)
  133. .Where("id=@id", new { id })
  134. .Update();
  135. break;
  136. case "time_range":
  137. int.TryParse(val, out int iVal);
  138. result = new DBContext.Table("jd_pool")
  139. .Add(name, iVal)
  140. .Add("last_time", DateTime.Now)
  141. .Where("id=@id", new { id })
  142. .Update();
  143. break;
  144. default:
  145. return new APIResult(new { data = new { success = false, msg = "更新失败,未授权操作" } });
  146. }
  147. bool success = result > 0;
  148. if (success)
  149. {
  150. OperationLogCore.LogOperation(token.AccessKey, clientIp, $"jd_pool:{id}:{name}", string.Empty, val);
  151. await EndPointCore.NotifyReload(true);
  152. }
  153. return new APIResult(new
  154. {
  155. data = new { success, msg = success ? "更新成功" : "更新失败,请检查输入信息" },
  156. });
  157. }
  158. }
  159. }