JdUnionController.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. string filter = "is_hide=0";
  209. if (!string.IsNullOrEmpty(keyword))
  210. {
  211. filter += $" AND (`name` LIKE @keyword OR `description` LIKE @keyword)";
  212. keyword = $"%{keyword}%";
  213. }
  214. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  215. if (lastTime.Count == 2)
  216. {
  217. if (DateTime.TryParse(lastTime[0], out stime) && DateTime.TryParse(lastTime[1], out etime))
  218. {
  219. etime = etime.AddDays(1).AddSeconds(-1);
  220. filter += $" AND last_time BETWEEN @stime AND @etime";
  221. }
  222. }
  223. if (status != -1)
  224. {
  225. filter += $" AND status=@status";
  226. }
  227. filter = filter.StringTrimStart(" AND ");
  228. //排序
  229. string orderBy = "id DESC";
  230. if (!string.IsNullOrEmpty(order))
  231. {
  232. order = "descending".Equals(order) ? "DESC" : "ASC";
  233. orderBy = sort switch
  234. {
  235. //"num" => $"num {order}",
  236. _ => $"{sort} {order}",
  237. };
  238. }
  239. using var conn = DBContext.GetOpenConnection();
  240. var result = new DBContext.Table(conn, "jd_pool")
  241. .Where(filter, new { keyword, status, stime, etime })
  242. .Page(size, page)
  243. .Order(orderBy)
  244. .PageList<JdPoolDTO>(getTotal);
  245. foreach (var item in result.List)
  246. {
  247. item.app_secret = string.Empty;
  248. item.cookies = string.Empty;
  249. }
  250. return new APIResult(new { data = result });
  251. }
  252. [HttpPost]
  253. public async Task<ActionResult> update([FromBody] JsonElement form)
  254. {
  255. var clientIp = _accessor.HttpContext.GetUserIp();
  256. var token = provider.Get(_accessor.HttpContext);
  257. int id = form.Read<int>("id", 0);
  258. string name = form.Read<string>("name", string.Empty);
  259. string val = form.Read<string>("val", string.Empty);
  260. if (id == 0)
  261. {
  262. return new APIResult(new { data = new { success = false, msg = "更新失败,请检查输入的cookie" } });
  263. }
  264. int result;
  265. switch (name)
  266. {
  267. case "enable_parse":
  268. case "enable_coupon":
  269. case "status":
  270. bool bVal = val.Equals("True");
  271. result = new DBContext.Table("jd_pool")
  272. .Add(name, bVal)
  273. .Add("last_time", DateTime.Now)
  274. .Where("id=@id", new { id })
  275. .Update();
  276. break;
  277. case "time_range":
  278. int.TryParse(val, out int iVal);
  279. result = new DBContext.Table("jd_pool")
  280. .Add(name, iVal)
  281. .Add("last_time", DateTime.Now)
  282. .Where("id=@id", new { id })
  283. .Update();
  284. break;
  285. default:
  286. return new APIResult(new { data = new { success = false, msg = "更新失败,未授权操作" } });
  287. }
  288. bool success = result > 0;
  289. if (success)
  290. {
  291. OperationLogCore.LogOperation(token.AccessKey, clientIp, $"jd_pool:{id}:{name}", string.Empty, val);
  292. await EndPointCore.NotifyReload(true);
  293. }
  294. return new APIResult(new
  295. {
  296. data = new { success, msg = success ? "更新成功" : "更新失败,请检查输入信息" },
  297. });
  298. }
  299. [HttpPost]
  300. public async Task<ActionResult> updateCookies([FromBody] JsonElement form)
  301. {
  302. var clientIp = _accessor.HttpContext.GetUserIp();
  303. var token = provider.Get(_accessor.HttpContext);
  304. string user_agent = _accessor.HttpContext.Request.UserAgent();
  305. string cookie = form.Read<string>("cookie", string.Empty);
  306. cookie = cookie.Replace("\r", "").Replace("\n", "").Trim();
  307. if (!cookie.EndsWith(";")) cookie += ";";
  308. var accountId = JdPoolCore.UpdateCookies(cookie, user_agent);
  309. bool success = accountId > 0;
  310. if (success)
  311. {
  312. //string desc = ObjectComparer.PrintCompareToString(old, form).Trim();
  313. OperationLogCore.LogOperation(token.AccessKey, clientIp, $"jd_pool:{accountId}:cookie", string.Empty, cookie);
  314. }
  315. return new APIResult(new
  316. {
  317. data = new { success, msg = success ? "更新成功" : "更新失败,请检查输入的cookie" },
  318. });
  319. }
  320. }
  321. }