PddUnionController.cs 12 KB

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