PddUnionController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. foreach (var item in result.List)
  109. {
  110. item.app_secret = string.Empty;
  111. item.cookies = string.Empty;
  112. item.riskCookie = RiskControlCore.GetRiskCookie($"pdd:{item.id}");
  113. }
  114. return new APIResult(new { data = result });
  115. }
  116. [HttpPost]
  117. public async Task<ActionResult> update([FromBody] JsonElement form)
  118. {
  119. var clientIp = _accessor.HttpContext.GetUserIp();
  120. var token = provider.Get(_accessor.HttpContext);
  121. int id = form.Read<int>("id", 0);
  122. string name = form.Read<string>("name", string.Empty);
  123. string val = form.Read<string>("val", string.Empty);
  124. if (id == 0)
  125. {
  126. return new APIResult(new { data = new { success = false, msg = "更新失败,请检查输入的cookie" } });
  127. }
  128. int result;
  129. switch (name)
  130. {
  131. case "enable_parse":
  132. case "enable_sync_revenue":
  133. case "enable_sync_order":
  134. case "enable_coupon":
  135. case "cookie_status":
  136. case "status":
  137. bool bVal = val.Equals("True");
  138. result = new DBContext.Table("pdd_pool")
  139. .Add(name, bVal)
  140. .Add("last_time", DateTime.Now)
  141. .Where("id=@id", new { id })
  142. .Update();
  143. if (bVal && ("status".Equals(name) || "enable_parse".Equals(name)))
  144. {
  145. PddPoolCore.InitializeNewAccountUsage(id);
  146. }
  147. break;
  148. case "time_range":
  149. int.TryParse(val, out int iVal);
  150. result = new DBContext.Table("pdd_pool")
  151. .Add(name, iVal)
  152. .Add("last_time", DateTime.Now)
  153. .Where("id=@id", new { id })
  154. .Update();
  155. break;
  156. case "balance":
  157. // todo 更新账户余额
  158. var account = new DBContext.Table("pdd_pool").Get<PddPoolDTO>("id=@id", new { id });
  159. var plus = new PddUnionPlus(account);
  160. result = await plus.queryMallBalance();
  161. break;
  162. default:
  163. return new APIResult(new { data = new { success = false, msg = "更新失败,未授权操作" } });
  164. }
  165. bool success = result > 0;
  166. if (success)
  167. {
  168. OperationLogCore.LogOperation(token.AccessKey, clientIp, $"pdd_pool:{id}:{name}", string.Empty, val);
  169. await EndPointCore.NotifyReload(true);
  170. }
  171. return new APIResult(new
  172. {
  173. data = new { success, msg = success ? "更新成功" : "更新失败,请检查输入信息" },
  174. });
  175. }
  176. [HttpPost]
  177. public ActionResult estimate_revenue_total([FromBody] JsonElement form)
  178. {
  179. int page = form.Read("current", 1);
  180. int size = form.Read("pageSize", 10);
  181. bool getTotal = form.Read("getTotal", true);
  182. string sort = form.Read<string>("sort");
  183. string order = form.Read<string>("order");
  184. var report_date = form.PathReadArray<string>("query_date[]");
  185. string filter = string.Empty;
  186. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  187. if (report_date.Count == 2)
  188. {
  189. if (DateTime.TryParse(report_date[0], out stime) && DateTime.TryParse(report_date[1], out etime))
  190. {
  191. etime = etime.AddDays(1).AddSeconds(-1);
  192. filter += $" AND report_date BETWEEN @stime AND @etime";
  193. }
  194. }
  195. filter = filter.StringTrimStart(" AND ");
  196. //排序
  197. string orderBy = "report_date DESC";
  198. if (!string.IsNullOrEmpty(order))
  199. {
  200. order = "descending".Equals(order) ? "DESC" : "ASC";
  201. orderBy = sort switch
  202. {
  203. _ => $"{sort} {order}",
  204. };
  205. }
  206. var result = new DBContext.Table("v_pdd_estimate_revenue")
  207. .Where(filter, new { stime, etime })
  208. .Page(size, page)
  209. .Order(orderBy)
  210. .PageList<PddEstimateRevenueDTO>(getTotal);
  211. return new APIResult(new { data = result });
  212. }
  213. [HttpGet]
  214. public ActionResult RechargeAllOnlineAccountUsage()
  215. {
  216. int count = PddPoolCore.RechargeAllOnlineAccountUsage();
  217. return new APIResult(new { msg = "ok", count });
  218. }
  219. [HttpPost]
  220. public ActionResult estimate_revenue([FromBody] JsonElement form)
  221. {
  222. int page = form.Read("current", 1);
  223. int size = form.Read("pageSize", 10);
  224. bool getTotal = form.Read("getTotal", true);
  225. string sort = form.Read<string>("sort");
  226. string order = form.Read<string>("order");
  227. string name = form.Read<string>("keyword");
  228. var report_date = form.PathReadArray<string>("query_date[]");
  229. string filter = string.Empty;
  230. if (!string.IsNullOrEmpty(name))
  231. {
  232. filter += $" AND accountId IN (SELECT ID FROM pdd_pool WHERE company=@name AND is_hide=0)";
  233. }
  234. else
  235. {
  236. filter += $" AND accountId IN (SELECT ID FROM pdd_pool WHERE is_hide=0)";
  237. }
  238. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  239. if (report_date.Count == 2)
  240. {
  241. if (DateTime.TryParse(report_date[0], out stime) && DateTime.TryParse(report_date[1], out etime))
  242. {
  243. etime = etime.AddDays(1).AddSeconds(-1);
  244. filter += $" AND report_date BETWEEN @stime AND @etime";
  245. }
  246. }
  247. filter = filter.StringTrimStart(" AND ");
  248. //排序
  249. string orderBy = "report_date DESC";
  250. if (!string.IsNullOrEmpty(order))
  251. {
  252. order = "descending".Equals(order) ? "DESC" : "ASC";
  253. orderBy = sort switch
  254. {
  255. _ => $"{sort} {order}",
  256. };
  257. }
  258. var result = new DBContext.Table("pdd_estimate_revenue")
  259. .Where(filter, new { name, stime, etime })
  260. .Page(size, page)
  261. .Order(orderBy)
  262. .PageList<PddEstimateRevenueDTO>(getTotal);
  263. return new APIResult(new { data = result });
  264. }
  265. }
  266. }