JdUnionController.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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, "tk_pool")
  98. .Where(filter, new { keyword, status, stime, etime })
  99. .Page(size, page)
  100. .Order(orderBy)
  101. .PageList<TkPoolDTO>(getTotal);
  102. foreach (var item in result.List)
  103. {
  104. item.current_amt = Math.Round(TkPoolCore.GetIncomeAmt($"{item.id}"), 2);
  105. item.cookies = string.Empty;
  106. }
  107. return new APIResult(new { data = result });
  108. }
  109. /*
  110. {
  111. "code": 420,
  112. "message": "未登录",
  113. "totalNum": 0
  114. }
  115. */
  116. [HttpPost]
  117. public async Task<ActionResult> update([FromBody] JsonElement form)
  118. {
  119. var clientIp = _accessor.HttpContext.GetUserIp();
  120. var token = provider.Get(_accessor.HttpContext);
  121. int id = form.Read<int>("id", 0);
  122. string name = form.Read<string>("name", string.Empty);
  123. string val = form.Read<string>("val", string.Empty);
  124. if (id == 0)
  125. {
  126. return new APIResult(new { data = new { success = false, msg = "更新失败,请检查输入的cookie" } });
  127. }
  128. int result;
  129. switch (name)
  130. {
  131. case "enable_fake_click":
  132. case "enable_parse":
  133. case "enable_coupon":
  134. case "enable_sync_order":
  135. case "enable_promotionQuery":
  136. case "status":
  137. bool bVal = val.Equals("True");
  138. result = new DBContext.Table("tk_pool")
  139. .Add(name, bVal)
  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. //string desc = ObjectComparer.PrintCompareToString(old, form).Trim();
  151. OperationLogCore.LogOperation(token.AccessKey, clientIp, $"tk_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. }