PddUnionController.cs 10 KB

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