TkController.cs 22 KB

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