JdUnionController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 `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}",
  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. foreach (var item in result.List)
  248. {
  249. item.app_secret = string.Empty;
  250. item.cookies = string.Empty;
  251. }
  252. return new APIResult(new { data = result });
  253. }
  254. [HttpPost]
  255. public async Task<ActionResult> update([FromBody] JsonElement form)
  256. {
  257. var clientIp = _accessor.HttpContext.GetUserIp();
  258. var token = provider.Get(_accessor.HttpContext);
  259. int id = form.Read<int>("id", 0);
  260. string name = form.Read<string>("name", string.Empty);
  261. string val = form.Read<string>("val", string.Empty);
  262. if (id == 0)
  263. {
  264. return new APIResult(new { data = new { success = false, msg = "更新失败,请检查输入的cookie" } });
  265. }
  266. int result;
  267. switch (name)
  268. {
  269. case "enable_parse":
  270. case "enable_coupon":
  271. case "enable_sync_report":
  272. case "enable_sync_order":
  273. case "status":
  274. bool bVal = val.Equals("True");
  275. result = new DBContext.Table("jd_pool")
  276. .Add(name, bVal)
  277. .Add("last_time", DateTime.Now)
  278. .Where("id=@id", new { id })
  279. .Update();
  280. break;
  281. case "time_range":
  282. int.TryParse(val, out int iVal);
  283. result = new DBContext.Table("jd_pool")
  284. .Add(name, iVal)
  285. .Add("last_time", DateTime.Now)
  286. .Where("id=@id", new { id })
  287. .Update();
  288. break;
  289. default:
  290. return new APIResult(new { data = new { success = false, msg = "更新失败,未授权操作" } });
  291. }
  292. bool success = result > 0;
  293. if (success)
  294. {
  295. OperationLogCore.LogOperation(token.AccessKey, clientIp, $"jd_pool:{id}:{name}", string.Empty, val);
  296. await EndPointCore.NotifyReload(true);
  297. }
  298. return new APIResult(new
  299. {
  300. data = new { success, msg = success ? "更新成功" : "更新失败,请检查输入信息" },
  301. });
  302. }
  303. [HttpPost]
  304. public async Task<ActionResult> updateCookies([FromBody] JsonElement form)
  305. {
  306. var clientIp = _accessor.HttpContext.GetUserIp();
  307. var token = provider.Get(_accessor.HttpContext);
  308. string cookie = form.Read<string>("cookie", string.Empty);
  309. string h5st = form.Read<string>("h5st", string.Empty);
  310. string user_agent = form.Read<string>("user_agent", string.Empty);
  311. cookie = cookie.Replace("\r", "").Replace("\n", "").Trim();
  312. if (!cookie.EndsWith(";")) cookie += ";";
  313. string union_cookies = form.Read<string>("union_cookies", string.Empty);
  314. union_cookies = union_cookies.Replace("\r", "").Replace("\n", "").Trim();
  315. if (!union_cookies.EndsWith(";")) union_cookies += ";";
  316. string[] parts = h5st.Split(';');
  317. h5st = string.Join(";", parts.Take(8));
  318. h5st = h5st.Trim();
  319. user_agent = user_agent.Trim();
  320. var accountId = JdPoolCore.UpdateCookies(cookie, union_cookies, user_agent, h5st);
  321. bool success = accountId > 0;
  322. if (success)
  323. {
  324. //string desc = ObjectComparer.PrintCompareToString(old, form).Trim();
  325. OperationLogCore.LogOperation(token.AccessKey, clientIp, $"jd_pool:{accountId}:cookie", string.Empty, cookie);
  326. OperationLogCore.LogOperation(token.AccessKey, clientIp, $"jd_pool:{accountId}:union_cookies", string.Empty, union_cookies);
  327. }
  328. return new APIResult(new
  329. {
  330. data = new { success, msg = success ? "更新成功" : "更新失败,请检查输入的cookie" },
  331. });
  332. }
  333. }
  334. }