PddUnionController.cs 10.0 KB

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