JdUnionController.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 ActionResult settle_bills_total([FromBody] JsonElement form)
  25. {
  26. int page = form.Read("current", 1);
  27. int size = form.Read("pageSize", 10);
  28. bool getTotal = form.Read("getTotal", true);
  29. string sort = form.Read<string>("sort");
  30. string order = form.Read<string>("order");
  31. var report_date = form.PathReadArray<string>("query_date[]");
  32. string filter = string.Empty;
  33. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  34. if (report_date.Count == 2)
  35. {
  36. if (DateTime.TryParse(report_date[0], out stime) && DateTime.TryParse(report_date[1], out etime))
  37. {
  38. etime = etime.AddDays(1).AddSeconds(-1);
  39. filter += $" AND report_date BETWEEN @stime AND @etime";
  40. }
  41. }
  42. filter = filter.StringTrimStart(" AND ");
  43. //排序
  44. string orderBy = "report_date DESC";
  45. if (!string.IsNullOrEmpty(order))
  46. {
  47. order = "descending".Equals(order) ? "DESC" : "ASC";
  48. orderBy = sort switch
  49. {
  50. _ => $"{sort} {order}",
  51. };
  52. }
  53. var result = new DBContext.Table("v_jd_spread_report")
  54. .Where(filter, new { stime, etime })
  55. .Page(size, page)
  56. .Order(orderBy)
  57. .PageList<JdSpreadReportDTO>(getTotal);
  58. return new APIResult(new { data = result });
  59. }
  60. [HttpPost]
  61. public ActionResult settle_bills([FromBody] JsonElement form)
  62. {
  63. int page = form.Read("current", 1);
  64. int size = form.Read("pageSize", 10);
  65. bool getTotal = form.Read("getTotal", true);
  66. string sort = form.Read<string>("sort");
  67. string order = form.Read<string>("order");
  68. string name = form.Read<string>("keyword");
  69. var report_date = form.PathReadArray<string>("query_date[]");
  70. string filter = string.Empty;
  71. if (!string.IsNullOrEmpty(name))
  72. {
  73. filter += $" AND accountId IN (SELECT ID FROM tk_pool WHERE company=@name)";
  74. }
  75. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  76. if (report_date.Count == 2)
  77. {
  78. if (DateTime.TryParse(report_date[0], out stime) && DateTime.TryParse(report_date[1], out etime))
  79. {
  80. etime = etime.AddDays(1).AddSeconds(-1);
  81. filter += $" AND report_date BETWEEN @stime AND @etime";
  82. }
  83. }
  84. filter = filter.StringTrimStart(" AND ");
  85. //排序
  86. string orderBy = "report_date DESC";
  87. if (!string.IsNullOrEmpty(order))
  88. {
  89. order = "descending".Equals(order) ? "DESC" : "ASC";
  90. orderBy = sort switch
  91. {
  92. _ => $"{sort} {order}",
  93. };
  94. }
  95. var result = new DBContext.Table("jd_spread_report")
  96. .Where(filter, new { name, stime, etime })
  97. .Page(size, page)
  98. .Order(orderBy)
  99. .PageList<JdSpreadReportDTO>(getTotal);
  100. return new APIResult(new { data = result });
  101. }
  102. /// <summary>
  103. /// 账号列表
  104. /// </summary>
  105. /// <param name="form"></param>
  106. /// <returns></returns>
  107. [HttpPost]
  108. public ActionResult list([FromBody] JsonElement form)
  109. {
  110. var token = provider.Get(_accessor.HttpContext);
  111. int page = form.Read("current", 1);
  112. int size = form.Read("pageSize", 10);
  113. string keyword = form.Read("name", string.Empty);
  114. bool getTotal = form.Read("getTotal", true);
  115. string _status = form.Read("status", string.Empty);
  116. int status = _status switch
  117. {
  118. "online" => 1,
  119. "offline" => 0,
  120. _ => -1,
  121. };
  122. var lastTime = form.PathReadArray<string>("lastTime[]");
  123. string sort = form.Read<string>("sort");
  124. string order = form.Read<string>("order");
  125. string filter = "is_hide=0";
  126. if (!string.IsNullOrEmpty(keyword))
  127. {
  128. filter += $" AND (`name` LIKE @keyword OR `description` LIKE @keyword)";
  129. keyword = $"%{keyword}%";
  130. }
  131. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  132. if (lastTime.Count == 2)
  133. {
  134. if (DateTime.TryParse(lastTime[0], out stime) && DateTime.TryParse(lastTime[1], out etime))
  135. {
  136. etime = etime.AddDays(1).AddSeconds(-1);
  137. filter += $" AND last_time BETWEEN @stime AND @etime";
  138. }
  139. }
  140. if (status != -1)
  141. {
  142. filter += $" AND status=@status";
  143. }
  144. filter = filter.StringTrimStart(" AND ");
  145. //排序
  146. string orderBy = "id DESC";
  147. if (!string.IsNullOrEmpty(order))
  148. {
  149. order = "descending".Equals(order) ? "DESC" : "ASC";
  150. orderBy = sort switch
  151. {
  152. //"num" => $"num {order}",
  153. _ => $"{sort} {order}",
  154. };
  155. }
  156. using var conn = DBContext.GetOpenConnection();
  157. var result = new DBContext.Table(conn, "jd_pool")
  158. .Where(filter, new { keyword, status, stime, etime })
  159. .Page(size, page)
  160. .Order(orderBy)
  161. .PageList<JdPoolDTO>(getTotal);
  162. foreach (var item in result.List)
  163. {
  164. item.app_secret = string.Empty;
  165. item.cookies = string.Empty;
  166. }
  167. return new APIResult(new { data = result });
  168. }
  169. [HttpPost]
  170. public async Task<ActionResult> update([FromBody] JsonElement form)
  171. {
  172. var clientIp = _accessor.HttpContext.GetUserIp();
  173. var token = provider.Get(_accessor.HttpContext);
  174. int id = form.Read<int>("id", 0);
  175. string name = form.Read<string>("name", string.Empty);
  176. string val = form.Read<string>("val", string.Empty);
  177. if (id == 0)
  178. {
  179. return new APIResult(new { data = new { success = false, msg = "更新失败,请检查输入的cookie" } });
  180. }
  181. int result;
  182. switch (name)
  183. {
  184. case "enable_parse":
  185. case "enable_coupon":
  186. case "status":
  187. bool bVal = val.Equals("True");
  188. result = new DBContext.Table("jd_pool")
  189. .Add(name, bVal)
  190. .Add("last_time", DateTime.Now)
  191. .Where("id=@id", new { id })
  192. .Update();
  193. break;
  194. case "time_range":
  195. int.TryParse(val, out int iVal);
  196. result = new DBContext.Table("jd_pool")
  197. .Add(name, iVal)
  198. .Add("last_time", DateTime.Now)
  199. .Where("id=@id", new { id })
  200. .Update();
  201. break;
  202. default:
  203. return new APIResult(new { data = new { success = false, msg = "更新失败,未授权操作" } });
  204. }
  205. bool success = result > 0;
  206. if (success)
  207. {
  208. OperationLogCore.LogOperation(token.AccessKey, clientIp, $"jd_pool:{id}:{name}", string.Empty, val);
  209. await EndPointCore.NotifyReload(true);
  210. }
  211. return new APIResult(new
  212. {
  213. data = new { success, msg = success ? "更新成功" : "更新失败,请检查输入信息" },
  214. });
  215. }
  216. [HttpPost]
  217. public async Task<ActionResult> updateCookies([FromBody] JsonElement form)
  218. {
  219. var clientIp = _accessor.HttpContext.GetUserIp();
  220. var token = provider.Get(_accessor.HttpContext);
  221. string user_agent = _accessor.HttpContext.Request.UserAgent();
  222. string cookie = form.Read<string>("cookie", string.Empty);
  223. cookie = cookie.Replace("\r", "").Replace("\n", "").Trim();
  224. if (!cookie.EndsWith(";")) cookie += ";";
  225. var accountId = JdPoolCore.UpdateCookies(cookie, user_agent);
  226. bool success = accountId > 0;
  227. if (success)
  228. {
  229. //string desc = ObjectComparer.PrintCompareToString(old, form).Trim();
  230. OperationLogCore.LogOperation(token.AccessKey, clientIp, $"jd_pool:{accountId}:cookie", string.Empty, cookie);
  231. }
  232. return new APIResult(new
  233. {
  234. data = new { success, msg = success ? "更新成功" : "更新失败,请检查输入的cookie" },
  235. });
  236. }
  237. }
  238. }