TkController.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. using molilian.core;
  2. using dodohold.core;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Org.BouncyCastle.Ocsp;
  5. using System.Text.Json;
  6. using System.Runtime.InteropServices;
  7. using System.Net;
  8. using System.Security.Cryptography;
  9. using Microsoft.AspNetCore.Authentication;
  10. using TencentCloud.Ecm.V20190719.Models;
  11. using COSXML.Network;
  12. using System.Linq;
  13. using static dodohold.core.ZTOExpress.CreateOrderArgs;
  14. using MySqlX.XDevAPI;
  15. using TencentCloud.Ame.V20190916.Models;
  16. using YunhuiKit;
  17. namespace molilian.api.Controllers
  18. {
  19. [ApiController]
  20. [Route("[controller]/[action]")]
  21. public class TkController : ControllerBase
  22. {
  23. protected IHttpContextAccessor _accessor;
  24. public TkController(IHttpContextAccessor accessor)
  25. {
  26. _accessor = accessor;
  27. }
  28. /// <summary>
  29. /// oppo调用
  30. /// </summary>
  31. /// <param name="s">正文</param>
  32. /// <param name="c">渠道。tb/jd</param>
  33. /// <param name="a">分配的客户端账号</param>
  34. /// <param name="t">11位时间戳</param>
  35. /// <param name="sign">签名</param>
  36. /// <param name="ip">客户的IP</param>
  37. /// <param name="oaid">客户的唯一ID</param>
  38. /// <returns></returns>
  39. [HttpPost]
  40. public async Task<ActionResult> unionParse([FromBody] JsonElement form, [FromQuery] string a = "", [FromQuery] int t = 0, [FromQuery] string sign = "")
  41. {
  42. //正文
  43. var content = form.Read("s", string.Empty);
  44. var channel = form.Read("c", string.Empty);
  45. var ip = form.Read("ip", string.Empty);
  46. var oaid = form.Read("oaid", string.Empty);
  47. var clickId = form.Read("clickId", string.Empty);
  48. var commerceType = form.Read<int>("commerceType", 0);
  49. var riskStrategy = form.Read("riskStrategy", string.Empty);
  50. //验证签名
  51. if (string.IsNullOrEmpty(a) || string.IsNullOrEmpty(sign))
  52. {
  53. return new APIResult(new { success = false, message = "验证错误" }, APIResultCodeEnum.Unauthorized);
  54. }
  55. DateTime time1 = t.Convert2Datetime();
  56. if (time1 < DateTime.Now.AddMinutes(-10))
  57. {
  58. return new APIResult(new { success = false, message = "验证错误2" }, APIResultCodeEnum.Unauthorized);
  59. }
  60. var account = await ApiAccountCore.GetOneAsync(a);
  61. if (account == null)
  62. {
  63. return new APIResult(new { success = false, message = "验证错误3" }, APIResultCodeEnum.Unauthorized);
  64. }
  65. string app_sign = $"{account.api_secret}@{t}".MD5();
  66. if (sign != app_sign)
  67. {
  68. return new APIResult(new { success = false, message = "验证错误4" }, APIResultCodeEnum.Unauthorized);
  69. }
  70. return await UnionParseCore.UnionParseAsync(content, channel, commerceType, ip, oaid, riskStrategy, 0, clickId);
  71. }
  72. [HttpPost]
  73. public async Task<ActionResult> unsafeParse([FromBody] JsonElement form)
  74. {
  75. var content = form.Read("s", string.Empty);
  76. var channel = form.Read("c", string.Empty);
  77. var ip = form.Read("ip", string.Empty);
  78. var oaid = form.Read("oaid", string.Empty);
  79. var clickId = form.Read("clickId", string.Empty);
  80. int accountid = form.Read("aid", 0);
  81. var commerceType = form.Read<int>("commerceType", 0);
  82. var riskStrategy = form.Read("riskStrategy", string.Empty);
  83. #if DEBUG
  84. //ip = "127.0.0.1";
  85. //oaid = "test-oaid";
  86. #endif
  87. return await UnionParseCore.UnionParseAsync(content, channel, commerceType, ip, oaid, riskStrategy, accountid, clickId);
  88. }
  89. [HttpPost]
  90. public async Task<ActionResult> unsafeBatchParse([FromBody] JsonElement form)
  91. {
  92. var content = form.PathReadArray("s[]", string.Empty);
  93. var channel = form.Read("c", string.Empty);
  94. var ip = form.Read("ip", string.Empty);
  95. var oaid = form.Read("oaid", string.Empty);
  96. var clickId = form.Read("clickId", string.Empty);
  97. int accountid = form.Read("aid", 0);
  98. int count = form.Read("count", 0);
  99. var commerceType = form.Read<int>("commerceType", 0);
  100. //入参参数。brw-浏览器,qapp-快应用
  101. var riskStrategy = form.Read("riskStrategy", string.Empty);
  102. #if DEBUG
  103. //ip = "127.0.0.1";
  104. //oaid = "test-oaid";
  105. #endif
  106. for (int i = 0; i < count; i++)
  107. {
  108. _ = await UnionParseCore.UnionParseAsync(content[i], channel, commerceType, ip, oaid, riskStrategy, accountid, clickId);
  109. }
  110. return new APIResult(new { msg = "ok" });
  111. }
  112. [HttpPost]
  113. public async Task<ActionResult> unionCoupon([FromBody] JsonElement form, [FromQuery] string a = "", [FromQuery] int t = 0, [FromQuery] string sign = "")
  114. {
  115. //正文
  116. var content = form.Read("s", string.Empty);
  117. var dplink = form.Read("dp", string.Empty);
  118. var channel = form.Read("c", string.Empty);
  119. var ip = form.Read("ip", string.Empty);
  120. var oaid = form.Read("oaid", string.Empty);
  121. var clickId = form.Read("clickId", string.Empty);
  122. //验证签名
  123. if (string.IsNullOrEmpty(a) || string.IsNullOrEmpty(sign))
  124. {
  125. return new APIResult(new { success = false, message = "验证错误" }, APIResultCodeEnum.Unauthorized);
  126. }
  127. DateTime time1 = t.Convert2Datetime();
  128. if (time1 < DateTime.Now.AddMinutes(-10))
  129. {
  130. return new APIResult(new { success = false, message = "验证错误2" }, APIResultCodeEnum.Unauthorized);
  131. }
  132. var account = await ApiAccountCore.GetOneAsync(a);
  133. if (account == null)
  134. {
  135. return new APIResult(new { success = false, message = "验证错误3" }, APIResultCodeEnum.Unauthorized);
  136. }
  137. string app_sign = $"{account.api_secret}@{t}".MD5();
  138. if (sign != app_sign)
  139. {
  140. return new APIResult(new { success = false, message = "验证错误4" }, APIResultCodeEnum.Unauthorized);
  141. }
  142. return await UnionCouponCore.UnionCouponAsync(content, dplink, channel, ip, oaid, clickId);
  143. }
  144. [HttpPost]
  145. public async Task<ActionResult> unsafeCoupon([FromBody] JsonElement form)
  146. {
  147. var content = form.Read("s", string.Empty);
  148. var dplink = form.Read("dp", string.Empty);
  149. var channel = form.Read("c", string.Empty);
  150. var ip = form.Read("ip", string.Empty);
  151. var oaid = form.Read("oaid", string.Empty);
  152. var clickId = form.Read("clickId", string.Empty);
  153. #if DEBUG
  154. ip = "127.0.0.1";
  155. oaid = "test-oaid";
  156. #endif
  157. return await UnionCouponCore.UnionCouponAsync(content, dplink, channel, ip, oaid, clickId);
  158. }
  159. [HttpGet]
  160. public async Task<ActionResult> jd_parse(string s, string s2 = "", string ip = "", string oaid = "")
  161. {
  162. var result = JdUnionPlus.GetFormattedObject(s, ip, oaid);
  163. string body = "";
  164. string content = s.UrlDecode();
  165. string content2 = s2.UrlDecode();
  166. try
  167. {
  168. if (AlimamaPlus.ShouldIgnoreRequest(ip, oaid, string.Empty, out string reason))
  169. {
  170. result.success = false;
  171. result.message = "放弃转链";
  172. result.reason = reason;
  173. result.rawContent = content;
  174. result.rawContent2 = content2;
  175. _ = TkLogCore.LogAsync(result);
  176. return new APIResult(result);
  177. }
  178. var api = DataokeCore.GetOne();
  179. if (api == null)
  180. {
  181. result.success = false;
  182. result.message = "放弃转链";
  183. result.reason = "没有匹配账号";
  184. result.rawContent = content;
  185. result.rawContent2 = content2;
  186. _ = TkLogCore.LogAsync(result);
  187. return new APIResult(result);
  188. }
  189. var account = await JdPoolCore.GetOneAsync();
  190. if (account == null)
  191. {
  192. result.success = false;
  193. result.message = "放弃转链";
  194. result.reason = "没有匹配账号";
  195. result.rawContent = content;
  196. result.rawContent2 = content2;
  197. _ = TkLogCore.LogAsync(result);
  198. return new APIResult(result);
  199. }
  200. result = DataokeCore.JdParse(result, api, account, content);
  201. }
  202. catch (Exception ex)
  203. {
  204. _ = new LoggerLibrary("api_error")
  205. .Info(s, s2)
  206. .Info("【jd】", body)
  207. .Info(ex.Message, ex.StackTrace)
  208. .SaveAsync();
  209. NotifyCore.Notify(new NifyMessage
  210. {
  211. message = $"【转链接口异常】\n{content}\n{content2}\n{body}\n\n{ex.Message}\n{ex.StackTrace}",
  212. priority = NifyMessagePriority.high,
  213. tags = ["red_circle"]
  214. });
  215. result.success = false;
  216. result.message = "放弃转链";
  217. result.reason = "转链接口异常";
  218. }
  219. result.rawContent = content;
  220. result.rawContent2 = content2;
  221. _ = TkLogCore.LogAsync(result);
  222. return new APIResult(new
  223. {
  224. result.success,
  225. result.message,
  226. result.shortLinkurl,
  227. result.itemId,
  228. result.itemName,
  229. result.deeplink_url,
  230. });
  231. }
  232. [HttpGet]
  233. public async Task<ActionResult> Reload()
  234. {
  235. await EndPointCore.NotifyReload();
  236. return new APIResult(new
  237. {
  238. success = true,
  239. message = "ok",
  240. });
  241. }
  242. [HttpGet]
  243. public async Task<ActionResult> dplist([FromQuery] string query = "", [FromQuery] string a = "", [FromQuery] int t = 0, [FromQuery] string sign = "")
  244. {
  245. var clientIp = _accessor.HttpContext.GetUserIp();
  246. //验证签名
  247. if (string.IsNullOrEmpty(a) || string.IsNullOrEmpty(sign))
  248. {
  249. _ = new LoggerLibrary("api", "dplist_error")
  250. .Info($"{clientIp}\t{query}")
  251. .Info($"{a}\t{sign}\t{t}")
  252. .Info("验证错误")
  253. .SaveAsync();
  254. return new APIResult(new { success = false, message = "验证错误" }, APIResultCodeEnum.Unauthorized);
  255. }
  256. DateTime time1 = t.Convert2Datetime();
  257. if (time1 < DateTime.Now.AddMinutes(-10))
  258. {
  259. _ = new LoggerLibrary("api", "dplist_error")
  260. .Info($"{clientIp}\t{query}")
  261. .Info($"{a}\t{sign}\t{t}")
  262. .Info("验证错误2")
  263. .SaveAsync();
  264. return new APIResult(new { success = false, message = "验证错误2" }, APIResultCodeEnum.Unauthorized);
  265. }
  266. var account = await ApiAccountCore.GetOneAsync(a);
  267. if (account == null)
  268. {
  269. _ = new LoggerLibrary("api", "dplist_error")
  270. .Info($"{clientIp}\t{query}")
  271. .Info($"{a}\t{sign}\t{t}")
  272. .Info("验证错误3")
  273. .SaveAsync();
  274. return new APIResult(new { success = false, message = "验证错误3" }, APIResultCodeEnum.Unauthorized);
  275. }
  276. string app_sign = $"{account.api_secret}@{t}".MD5();
  277. if (sign != app_sign)
  278. {
  279. _ = new LoggerLibrary("api", "dplist_error")
  280. .Info($"{clientIp}\t{query}")
  281. .Info($"{a}\t{sign}\t{t}")
  282. .Info("验证错误4")
  283. .SaveAsync();
  284. return new APIResult(new { success = false, message = "验证错误4" }, APIResultCodeEnum.Unauthorized);
  285. }
  286. if (string.IsNullOrEmpty(query))
  287. {
  288. _ = new LoggerLibrary("api", "dplist_error")
  289. .Info($"{clientIp}\t{query}")
  290. .Info($"{a}\t{sign}\t{t}")
  291. .Info("查询条件不能为空")
  292. .SaveAsync();
  293. return new APIResult(new { success = false, message = "查询条件不能为空" });
  294. }
  295. int[] report_account_ids = account.report_account_ids.TryToArray<int>(',');
  296. int[] report_exclude_account_ids = account.report_exclude_account_ids.TryToArray<int>(',');
  297. string filter = $"batchId=@query";
  298. var account_list = await TkPoolCore.ListAsync();
  299. List<int> account_ids = [];
  300. if (account_list.Any())
  301. {
  302. account_ids = account_list.Where(e => e.enable_fake_click).Select(e => e.id).ToList();
  303. if (report_account_ids.Length > 0)
  304. {
  305. account_ids = account_ids.Intersect(report_account_ids).ToList();
  306. }
  307. if (report_exclude_account_ids.Length > 0)
  308. {
  309. account_ids = account_ids.Except(report_exclude_account_ids).ToList();
  310. }
  311. filter += $" AND accountId IN @account_ids";
  312. }
  313. string orderBy = "paid_time ASC";
  314. var data = new DBContext.Table("tk_order_tracking")
  315. .Where(filter, new { query, account_ids })
  316. .Order(orderBy)
  317. .Select<TkOrderTrackingDTO>();
  318. //fake_click_link_type
  319. List<object> list = [];
  320. foreach (var item in data)
  321. {
  322. list.Add(new { pid = item.accountId, url = item.deeplinkUrl, item.shortLinkUrl, count = item.click_num, time = item.exp_time });
  323. }
  324. var ids = data.Select(e => e.Id).ToArray();
  325. _ = new LoggerLibrary("api", "dplist")
  326. .Info($"{clientIp}\t{query}\tcount:{list.Count}")
  327. .Info(string.Join(',', ids))
  328. .SaveAsync();
  329. var update = new DBContext.Table("tk_order_tracking")
  330. .Add("last_pull_time", DateTime.Now)
  331. .Add("last_pull_account", account.name)
  332. .Add("clicked_num:", "clicked_num+1")
  333. .Where("id IN @ids", new { ids })
  334. .Update();
  335. return new APIResult(new
  336. {
  337. success = true,
  338. message = list.Count == 0 ? "none" : "ok",
  339. count = list.Count,
  340. data = list,
  341. });
  342. }
  343. [HttpGet]
  344. public async Task<ActionResult> unsafeParseDpList([FromQuery] string a = "", [FromQuery] string query = "")
  345. {
  346. if (string.IsNullOrEmpty(a))
  347. {
  348. return new APIResult(new { success = false, message = "验证错误" }, APIResultCodeEnum.Unauthorized);
  349. }
  350. var account = await ApiAccountCore.GetOneAsync(a);
  351. if (account == null || !account.enable_report)
  352. {
  353. return new APIResult(new { success = false, message = "验证错误3" }, APIResultCodeEnum.Unauthorized);
  354. }
  355. if (string.IsNullOrEmpty(query))
  356. {
  357. return new APIResult(new { success = false, message = "查询条件不能为空" });
  358. }
  359. int[] report_account_ids = account.report_account_ids.TryToArray<int>(',');
  360. int[] report_exclude_account_ids = account.report_exclude_account_ids.TryToArray<int>(',');
  361. string filter = $"batchId=@query";
  362. var account_list = await TkPoolCore.ListAsync();
  363. List<int> account_ids = [];
  364. if (account_list.Any())
  365. {
  366. account_ids = account_list.Where(e => e.enable_fake_click).Select(e => e.id).ToList();
  367. if (report_account_ids.Length > 0)
  368. {
  369. account_ids = account_ids.Intersect(report_account_ids).ToList();
  370. }
  371. if (report_exclude_account_ids.Length > 0)
  372. {
  373. account_ids = account_ids.Except(report_exclude_account_ids).ToList();
  374. }
  375. filter += $" AND accountId IN @account_ids";
  376. }
  377. string orderBy = "paid_time ASC";
  378. var data = new DBContext.Table("tk_order_tracking")
  379. .Where(filter, new { query, account_ids })
  380. .Order(orderBy)
  381. .Select<TkOrderTrackingDTO>();
  382. List<object> list = [];
  383. foreach (var item in data)
  384. {
  385. list.Add(new { pid = item.accountId, url = item.deeplinkUrl, item.shortLinkUrl, count = item.click_num, time = item.exp_time });
  386. }
  387. var clientIp = _accessor.HttpContext.GetUserIp();
  388. LoggerLibrary log = new LoggerLibrary("api", "unsafeParseDpList");
  389. log.Info($"{clientIp}\t{query}\tcount:{list.Count}");
  390. var ids = data.Select(e => e.Id).ToArray();
  391. log.Info(string.Join(',', ids));
  392. _ = log.SaveAsync();
  393. return new APIResult(new
  394. {
  395. success = true,
  396. message = list.Count == 0 ? "none" : "ok",
  397. count = list.Count,
  398. data = list,
  399. });
  400. }
  401. [HttpPost]
  402. public async Task<ActionResult> dataReport([FromBody] JsonElement form, [FromQuery] string a = "", [FromQuery] int t = 0, [FromQuery] string sign = "")
  403. {
  404. bool debug = form.Read("debug", false);
  405. var clientIp = _accessor.HttpContext.GetUserIp();
  406. var log = new LoggerLibrary("ApiReportCore", "dataReport");
  407. log.Info(clientIp);
  408. //验证签名
  409. if (string.IsNullOrEmpty(a) || string.IsNullOrEmpty(sign))
  410. {
  411. _ = log.Info($"a:{a}\tt:{t}\tsign:{sign}\t", $"message:验证错误")
  412. .Info(form.GetRawText())
  413. .SaveAsync();
  414. return new APIResult(new { success = false, message = "验证错误" }, APIResultCodeEnum.Unauthorized);
  415. }
  416. DateTime time1 = t.Convert2Datetime();
  417. if (time1 < DateTime.Now.AddMinutes(-10))
  418. {
  419. _ = log.Info($"a:{a}\tt:{t}\tsign:{sign}\t", $"message:验证错误2")
  420. .Info(form.GetRawText())
  421. .SaveAsync();
  422. return new APIResult(new { success = false, message = "验证错误2" }, APIResultCodeEnum.Unauthorized);
  423. }
  424. var account = await ApiAccountCore.GetOneAsync(a);
  425. if (account == null)
  426. {
  427. _ = log.Info($"a:{a}\tt:{t}\tsign:{sign}\t", $"message:验证错误3")
  428. .Info(form.GetRawText())
  429. .SaveAsync();
  430. return new APIResult(new { success = false, message = "验证错误3" }, APIResultCodeEnum.Unauthorized);
  431. }
  432. string app_sign = $"{account.api_secret}@{t}".MD5();
  433. if (sign != app_sign)
  434. {
  435. _ = log.Info($"a:{a}\tt:{t}\tsign:{sign}\tapp_sign:{app_sign}", $"message:验证错误4")
  436. .Info(form.GetRawText())
  437. .SaveAsync();
  438. return new APIResult(new { success = false, message = "验证错误4" }, APIResultCodeEnum.Unauthorized);
  439. }
  440. try
  441. {
  442. //0:全分销类型;-1:各分销类型,1:goods 2: live 3:video 4: profile 5: unknown
  443. int distributiontype = form.Read("distributiontype", 0);
  444. //0:全平台;-1:各平台;1:淘宝 2:京东 2:多多
  445. int platform = form.Read("platform", 0);
  446. var data = await ApiReportCore.ReportDailysAsync(form, clientIp, debug);
  447. _ = log.Info(form.GetRawText(), data.Convert2Json()).SaveAsync();
  448. return new APIResult(new { success = true, platform, distributiontype, data });
  449. }
  450. catch (Exception ex)
  451. {
  452. _ = log.Info(form.GetRawText()).Info(ex.Message, ex.StackTrace).SaveAsync();
  453. return new APIResult(new { success = false, message = "内部错误" });
  454. }
  455. }
  456. [HttpPost]
  457. public async Task<ActionResult> unsafeDataReport([FromBody] JsonElement form)
  458. {
  459. bool debug = form.Read("debug", false);
  460. var clientIp = _accessor.HttpContext.GetUserIp();
  461. var log = new LoggerLibrary("ApiReportCore", "unsafeDataReport");
  462. log.Info(clientIp);
  463. try
  464. {
  465. //0:全分销类型;-1:各分销类型,1:goods 2: live 3:video 4: profile 5: unknown
  466. int distributiontype = form.Read("distributiontype", 0);
  467. //0:全平台;-1:各平台;1:淘宝 2:京东
  468. int platform = form.Read("platform", 0);
  469. var data = await ApiReportCore.ReportDailysAsync(form, clientIp, debug);
  470. _ = log.Info(form.GetRawText(), data.Convert2Json()).SaveAsync();
  471. return new APIResult(new { success = true, platform, distributiontype, data });
  472. }
  473. catch (Exception ex)
  474. {
  475. _ = log.Info(form.GetRawText()).Info(ex.Message, ex.StackTrace).SaveAsync();
  476. return new APIResult(new { success = false, message = "内部错误" });
  477. }
  478. }
  479. }
  480. }