TkController.cs 22 KB

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