TkController.cs 23 KB

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