TkController.cs 23 KB

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