PddUnionController.cs 6.3 KB

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