PddUnionController.cs 10 KB

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