TkController.cs 22 KB

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