PddUnionController.cs 9.9 KB

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