TkController.cs 22 KB

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