JdUnionController.cs 6.4 KB

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