JdUnionController.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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)";
  153. }
  154. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  155. if (report_date.Count == 2)
  156. {
  157. if (DateTime.TryParse(report_date[0], out stime) && DateTime.TryParse(report_date[1], out etime))
  158. {
  159. etime = etime.AddDays(1).AddSeconds(-1);
  160. filter += $" AND report_date BETWEEN @stime AND @etime";
  161. }
  162. }
  163. filter = filter.StringTrimStart(" AND ");
  164. //排序
  165. string orderBy = "report_date DESC";
  166. if (!string.IsNullOrEmpty(order))
  167. {
  168. order = "descending".Equals(order) ? "DESC" : "ASC";
  169. orderBy = sort switch
  170. {
  171. _ => $"{sort} {order}",
  172. };
  173. }
  174. var result = new DBContext.Table("jd_estimate_comm")
  175. .Where(filter, new { name, stime, etime })
  176. .Page(size, page)
  177. .Order(orderBy)
  178. .PageList<JdEstimateCommDTO>(getTotal);
  179. return new APIResult(new { data = result });
  180. }
  181. /// <summary>
  182. /// 账号列表
  183. /// </summary>
  184. /// <param name="form"></param>
  185. /// <returns></returns>
  186. [HttpPost]
  187. public ActionResult list([FromBody] JsonElement form)
  188. {
  189. var token = provider.Get(_accessor.HttpContext);
  190. int page = form.Read("current", 1);
  191. int size = form.Read("pageSize", 10);
  192. string keyword = form.Read("name", string.Empty);
  193. bool getTotal = form.Read("getTotal", true);
  194. string _status = form.Read("status", string.Empty);
  195. int status = _status switch
  196. {
  197. "online" => 1,
  198. "offline" => 0,
  199. _ => -1,
  200. };
  201. var lastTime = form.PathReadArray<string>("lastTime[]");
  202. string sort = form.Read<string>("sort");
  203. string order = form.Read<string>("order");
  204. string filter = "is_hide=0";
  205. if (!string.IsNullOrEmpty(keyword))
  206. {
  207. filter += $" AND (`name` LIKE @keyword OR `description` LIKE @keyword)";
  208. keyword = $"%{keyword}%";
  209. }
  210. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  211. if (lastTime.Count == 2)
  212. {
  213. if (DateTime.TryParse(lastTime[0], out stime) && DateTime.TryParse(lastTime[1], out etime))
  214. {
  215. etime = etime.AddDays(1).AddSeconds(-1);
  216. filter += $" AND last_time BETWEEN @stime AND @etime";
  217. }
  218. }
  219. if (status != -1)
  220. {
  221. filter += $" AND status=@status";
  222. }
  223. filter = filter.StringTrimStart(" AND ");
  224. //排序
  225. string orderBy = "id DESC";
  226. if (!string.IsNullOrEmpty(order))
  227. {
  228. order = "descending".Equals(order) ? "DESC" : "ASC";
  229. orderBy = sort switch
  230. {
  231. //"num" => $"num {order}",
  232. _ => $"{sort} {order}",
  233. };
  234. }
  235. using var conn = DBContext.GetOpenConnection();
  236. var result = new DBContext.Table(conn, "jd_pool")
  237. .Where(filter, new { keyword, status, stime, etime })
  238. .Page(size, page)
  239. .Order(orderBy)
  240. .PageList<JdPoolDTO>(getTotal);
  241. foreach (var item in result.List)
  242. {
  243. item.app_secret = string.Empty;
  244. item.cookies = string.Empty;
  245. }
  246. return new APIResult(new { data = result });
  247. }
  248. [HttpPost]
  249. public async Task<ActionResult> update([FromBody] JsonElement form)
  250. {
  251. var clientIp = _accessor.HttpContext.GetUserIp();
  252. var token = provider.Get(_accessor.HttpContext);
  253. int id = form.Read<int>("id", 0);
  254. string name = form.Read<string>("name", string.Empty);
  255. string val = form.Read<string>("val", string.Empty);
  256. if (id == 0)
  257. {
  258. return new APIResult(new { data = new { success = false, msg = "更新失败,请检查输入的cookie" } });
  259. }
  260. int result;
  261. switch (name)
  262. {
  263. case "enable_parse":
  264. case "enable_coupon":
  265. case "status":
  266. bool bVal = val.Equals("True");
  267. result = new DBContext.Table("jd_pool")
  268. .Add(name, bVal)
  269. .Add("last_time", DateTime.Now)
  270. .Where("id=@id", new { id })
  271. .Update();
  272. break;
  273. case "time_range":
  274. int.TryParse(val, out int iVal);
  275. result = new DBContext.Table("jd_pool")
  276. .Add(name, iVal)
  277. .Add("last_time", DateTime.Now)
  278. .Where("id=@id", new { id })
  279. .Update();
  280. break;
  281. default:
  282. return new APIResult(new { data = new { success = false, msg = "更新失败,未授权操作" } });
  283. }
  284. bool success = result > 0;
  285. if (success)
  286. {
  287. OperationLogCore.LogOperation(token.AccessKey, clientIp, $"jd_pool:{id}:{name}", string.Empty, val);
  288. await EndPointCore.NotifyReload(true);
  289. }
  290. return new APIResult(new
  291. {
  292. data = new { success, msg = success ? "更新成功" : "更新失败,请检查输入信息" },
  293. });
  294. }
  295. [HttpPost]
  296. public async Task<ActionResult> updateCookies([FromBody] JsonElement form)
  297. {
  298. var clientIp = _accessor.HttpContext.GetUserIp();
  299. var token = provider.Get(_accessor.HttpContext);
  300. string user_agent = _accessor.HttpContext.Request.UserAgent();
  301. string cookie = form.Read<string>("cookie", string.Empty);
  302. cookie = cookie.Replace("\r", "").Replace("\n", "").Trim();
  303. if (!cookie.EndsWith(";")) cookie += ";";
  304. var accountId = JdPoolCore.UpdateCookies(cookie, user_agent);
  305. bool success = accountId > 0;
  306. if (success)
  307. {
  308. //string desc = ObjectComparer.PrintCompareToString(old, form).Trim();
  309. OperationLogCore.LogOperation(token.AccessKey, clientIp, $"jd_pool:{accountId}:cookie", string.Empty, cookie);
  310. }
  311. return new APIResult(new
  312. {
  313. data = new { success, msg = success ? "更新成功" : "更新失败,请检查输入的cookie" },
  314. });
  315. }
  316. }
  317. }