PddUnionController.cs 11 KB

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