TkController.cs 23 KB

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