JdUnionController.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 jd_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. [HttpPost]
  103. public ActionResult estimate_comm_total([FromBody] JsonElement form)
  104. {
  105. int page = form.Read("current", 1);
  106. int size = form.Read("pageSize", 10);
  107. bool getTotal = form.Read("getTotal", true);
  108. string sort = form.Read<string>("sort");
  109. string order = form.Read<string>("order");
  110. var report_date = form.PathReadArray<string>("query_date[]");
  111. string filter = string.Empty;
  112. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  113. if (report_date.Count == 2)
  114. {
  115. if (DateTime.TryParse(report_date[0], out stime) && DateTime.TryParse(report_date[1], out etime))
  116. {
  117. etime = etime.AddDays(1).AddSeconds(-1);
  118. filter += $" AND report_date BETWEEN @stime AND @etime";
  119. }
  120. }
  121. filter = filter.StringTrimStart(" AND ");
  122. //排序
  123. string orderBy = "report_date DESC";
  124. if (!string.IsNullOrEmpty(order))
  125. {
  126. order = "descending".Equals(order) ? "DESC" : "ASC";
  127. orderBy = sort switch
  128. {
  129. _ => $"{sort} {order}",
  130. };
  131. }
  132. var result = new DBContext.Table("v_jd_estimate_comm")
  133. .Where(filter, new { stime, etime })
  134. .Page(size, page)
  135. .Order(orderBy)
  136. .PageList<JdEstimateCommDTO>(getTotal);
  137. return new APIResult(new { data = result });
  138. }
  139. [HttpPost]
  140. public ActionResult estimate_comm([FromBody] JsonElement form)
  141. {
  142. int page = form.Read("current", 1);
  143. int size = form.Read("pageSize", 10);
  144. bool getTotal = form.Read("getTotal", true);
  145. string sort = form.Read<string>("sort");
  146. string order = form.Read<string>("order");
  147. string name = form.Read<string>("keyword");
  148. var report_date = form.PathReadArray<string>("query_date[]");
  149. string filter = string.Empty;
  150. if (!string.IsNullOrEmpty(name))
  151. {
  152. filter += $" AND accountId IN (SELECT ID FROM jd_pool WHERE company=@name AND is_hide=0)";
  153. }
  154. else
  155. {
  156. filter += $" AND accountId IN (SELECT ID FROM jd_pool WHERE is_hide=0)";
  157. }
  158. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  159. if (report_date.Count == 2)
  160. {
  161. if (DateTime.TryParse(report_date[0], out stime) && DateTime.TryParse(report_date[1], out etime))
  162. {
  163. etime = etime.AddDays(1).AddSeconds(-1);
  164. filter += $" AND report_date BETWEEN @stime AND @etime";
  165. }
  166. }
  167. filter = filter.StringTrimStart(" AND ");
  168. //排序
  169. string orderBy = "report_date DESC";
  170. if (!string.IsNullOrEmpty(order))
  171. {
  172. order = "descending".Equals(order) ? "DESC" : "ASC";
  173. orderBy = sort switch
  174. {
  175. _ => $"{sort} {order}",
  176. };
  177. }
  178. var result = new DBContext.Table("jd_estimate_comm")
  179. .Where(filter, new { name, stime, etime })
  180. .Page(size, page)
  181. .Order(orderBy)
  182. .PageList<JdEstimateCommDTO>(getTotal);
  183. return new APIResult(new { data = result });
  184. }
  185. /// <summary>
  186. /// 账号列表
  187. /// </summary>
  188. /// <param name="form"></param>
  189. /// <returns></returns>
  190. [HttpPost]
  191. public ActionResult list([FromBody] JsonElement form)
  192. {
  193. var token = provider.Get(_accessor.HttpContext);
  194. int page = form.Read("current", 1);
  195. int size = form.Read("pageSize", 10);
  196. string keyword = form.Read("name", string.Empty);
  197. bool getTotal = form.Read("getTotal", true);
  198. string _status = form.Read("status", string.Empty);
  199. int status = _status switch
  200. {
  201. "online" => 1,
  202. "offline" => 0,
  203. _ => -1,
  204. };
  205. var lastTime = form.PathReadArray<string>("lastTime[]");
  206. string sort = form.Read<string>("sort");
  207. string order = form.Read<string>("order");
  208. bool show_hide = form.Read("show_hide", false);
  209. string filter = string.Empty;
  210. if (!show_hide) filter = "is_hide=0";
  211. if (!string.IsNullOrEmpty(keyword))
  212. {
  213. filter += $" AND (`name` LIKE @keyword OR `pin` LIKE @keyword OR `nodename` LIKE @keyword OR `company` LIKE @keyword OR `description` LIKE @keyword)";
  214. keyword = $"%{keyword}%";
  215. }
  216. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  217. if (lastTime.Count == 2)
  218. {
  219. if (DateTime.TryParse(lastTime[0], out stime) && DateTime.TryParse(lastTime[1], out etime))
  220. {
  221. etime = etime.AddDays(1).AddSeconds(-1);
  222. filter += $" AND last_time BETWEEN @stime AND @etime";
  223. }
  224. }
  225. if (status != -1)
  226. {
  227. filter += $" AND status=@status";
  228. }
  229. filter = filter.StringTrimStart(" AND ");
  230. //排序
  231. string orderBy = "id DESC";
  232. if (!string.IsNullOrEmpty(order))
  233. {
  234. order = "descending".Equals(order) ? "DESC" : "ASC";
  235. orderBy = sort switch
  236. {
  237. //"num" => $"num {order}",
  238. _ => $"{sort} {order}, id DESC",
  239. };
  240. }
  241. using var conn = DBContext.GetOpenConnection();
  242. var result = new DBContext.Table(conn, "jd_pool")
  243. .Where(filter, new { keyword, status, stime, etime })
  244. .Page(size, page)
  245. .Order(orderBy)
  246. .PageList<JdPoolDTO>(getTotal);
  247. var proxyNodes = (ProxyNodesCore.AllList() ?? Array.Empty<ProxyNodesDTO>())
  248. .ToList();
  249. foreach (var item in result.List)
  250. {
  251. item.app_secret = string.Empty;
  252. item.cookies = string.Empty;
  253. item.riskCookie = RiskControlCore.GetRiskCookie($"jd:{item.id}");
  254. item.proxyStatus = proxyNodes
  255. .FirstOrDefault(proxyNode =>
  256. ProxyNodesCore.IsMatchNode(proxyNode, item.nodeName))
  257. ?.status;
  258. }
  259. return new APIResult(new { data = result });
  260. }
  261. [HttpPost]
  262. public async Task<ActionResult> update([FromBody] JsonElement form)
  263. {
  264. var clientIp = _accessor.HttpContext.GetUserIp();
  265. var token = provider.Get(_accessor.HttpContext);
  266. int id = form.Read<int>("id", 0);
  267. string name = form.Read<string>("name", string.Empty);
  268. string val = form.Read<string>("val", string.Empty);
  269. if (id == 0)
  270. {
  271. return new APIResult(new { data = new { success = false, msg = "更新失败,请检查输入的cookie" } });
  272. }
  273. int result;
  274. switch (name)
  275. {
  276. case "enable_parse":
  277. case "enable_coupon":
  278. case "enable_sync_report":
  279. case "enable_sync_order":
  280. case "status":
  281. bool bVal = val.Equals("True");
  282. result = new DBContext.Table("jd_pool")
  283. .Add(name, bVal)
  284. .Add("last_time", DateTime.Now)
  285. .Where("id=@id", new { id })
  286. .Update();
  287. break;
  288. case "time_range":
  289. int.TryParse(val, out int iVal);
  290. result = new DBContext.Table("jd_pool")
  291. .Add(name, iVal)
  292. .Add("last_time", DateTime.Now)
  293. .Where("id=@id", new { id })
  294. .Update();
  295. break;
  296. default:
  297. return new APIResult(new { data = new { success = false, msg = "更新失败,未授权操作" } });
  298. }
  299. bool success = result > 0;
  300. if (success)
  301. {
  302. OperationLogCore.LogOperation(token.AccessKey, clientIp, $"jd_pool:{id}:{name}", string.Empty, val);
  303. await EndPointCore.NotifyReload(true);
  304. }
  305. return new APIResult(new
  306. {
  307. data = new { success, msg = success ? "更新成功" : "更新失败,请检查输入信息" },
  308. });
  309. }
  310. [HttpPost]
  311. public async Task<ActionResult> updateCookies([FromBody] JsonElement form)
  312. {
  313. var clientIp = _accessor.HttpContext.GetUserIp();
  314. var token = provider.Get(_accessor.HttpContext);
  315. string cookie = form.Read<string>("cookie", string.Empty);
  316. string h5st = form.Read<string>("h5st", string.Empty);
  317. string envStr = form.Read<string>("envStr", string.Empty);
  318. string user_agent = form.Read<string>("user_agent", string.Empty);
  319. cookie = cookie.Replace("\r", "").Replace("\n", "").Trim();
  320. if (!cookie.EndsWith(";")) cookie += ";";
  321. string union_cookies = form.Read<string>("union_cookies", string.Empty);
  322. union_cookies = union_cookies.Replace("\r", "").Replace("\n", "").Trim();
  323. if (!union_cookies.EndsWith(";")) union_cookies += ";";
  324. string[] parts = h5st.Split(';');
  325. h5st = string.Join(";", parts.Take(8));
  326. h5st = h5st.Trim();
  327. user_agent = user_agent.Trim();
  328. var accountId = JdPoolCore.UpdateCookies(cookie, union_cookies, user_agent, h5st, envStr);
  329. bool success = accountId > 0;
  330. if (success)
  331. {
  332. //string desc = ObjectComparer.PrintCompareToString(old, form).Trim();
  333. OperationLogCore.LogOperation(token.AccessKey, clientIp, $"jd_pool:{accountId}:cookie", string.Empty, cookie);
  334. OperationLogCore.LogOperation(token.AccessKey, clientIp, $"jd_pool:{accountId}:union_cookies", string.Empty, union_cookies);
  335. }
  336. return new APIResult(new
  337. {
  338. data = new { success, msg = success ? "更新成功" : "更新失败,请检查输入的cookie" },
  339. });
  340. }
  341. }
  342. }