account.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc.Controllers;
  3. using Microsoft.AspNetCore.Mvc.Filters;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using dodohold.core;
  9. using Dataoke;
  10. using System.Text.RegularExpressions;
  11. using Org.BouncyCastle.Ocsp;
  12. using System.Diagnostics;
  13. using TencentCloud.Fmu.V20191213.Models;
  14. using Microsoft.VisualBasic;
  15. using System.Text.Json;
  16. using static System.Runtime.InteropServices.JavaScript.JSType;
  17. using TencentCloud.Cdn.V20180606.Models;
  18. using System.Net;
  19. using TencentCloud.Teo.V20220901.Models;
  20. using MySqlX.XDevAPI;
  21. using Spire.Pdf.Exporting.XPS.Schema;
  22. using COSXML.Network;
  23. using TencentCloud.Wedata.V20210820.Models;
  24. using System.Xml.Linq;
  25. namespace molilian.core
  26. {
  27. public partial class AlimamaPlus
  28. {
  29. /// <summary>
  30. /// 获取可提现余额
  31. /// </summary>
  32. public void GetDrawBalance()
  33. {
  34. DateTime date = DateTime.Now.AddDays(-1);
  35. TkPoolDTO result = new TkPoolDTO();
  36. try
  37. {
  38. string t = $"{date.Convert2UnixTimestamp(true)}";
  39. string dt = date.ToString("yyyy-MM-dd");
  40. string url = "https://pub.alimama.com/openapi/param2/1/gateway.unionpub/settle.account.balance.get.json?" +
  41. $"t={t}&_tb_token_={_tb_token}";
  42. var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
  43. .AddHeaders("X-Requested-With", "XMLHttpRequest")
  44. .AddHeaders("Cookie", _cookies);
  45. client.Proxy = _proxy;
  46. if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
  47. var response = client.Request(url);
  48. var body = response.Body();
  49. var root = body.Convert2JsonElement();
  50. int resultCode = root.Read<int>("resultCode", 0);
  51. bool success = root.Read("success", false);
  52. if (!success)
  53. {
  54. _ = new LoggerLibrary("account", "error")
  55. .Info(url, body)
  56. .SaveAsync();
  57. throw new Exception(body);
  58. }
  59. var drawBalance = root.PathRead<decimal>("data.totalBalance", 0);
  60. TkPoolCore.UpdateDrawBalance(_accountName, drawBalance);
  61. }
  62. catch (Exception ex) { }
  63. }
  64. public (bool, string) RenewCookie()
  65. {
  66. string cache_key = $"cache:tk_pool:{_accountName}:RenewCookie";
  67. long count = RedisHelper.IncrBy(cache_key);
  68. RedisHelper.Expire(cache_key, 30);
  69. if (count > 1) return (true, "前一个cookie续签正在执行中");
  70. try
  71. {
  72. {
  73. string url = "https://pub.alimama.com/common/getUnionPubContextInfo.json";
  74. var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
  75. .AddHeaders("X-Requested-With", "XMLHttpRequest")
  76. .AddHeaders("Cookie", _cookies);
  77. #if DEBUG
  78. #else
  79. client.Proxy = _proxy;
  80. #endif
  81. if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
  82. var response = client.Request(url);
  83. var body = response.Body();
  84. }
  85. /*
  86. {"invalidKey":null,"info":{"ok":true,"message":null},"data":{"noLogin":true,"ip":"121.206.54.236","loginUrlPrefix":"http://www.alimama.com/member/login.htm?forward=","env":"product"},"ok":true} */
  87. {
  88. string url = "https://pub.alimama.com/openapi/param2/1/gateway.unionpub/union.userLoginInfo.get";
  89. var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
  90. .AddHeaders("X-Requested-With", "XMLHttpRequest")
  91. .AddHeaders("Cookie", _cookies);
  92. #if DEBUG
  93. #else
  94. client.Proxy = _proxy;
  95. #endif
  96. if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
  97. var response = client.Request(url);
  98. var body = response.Body();
  99. var body2 = body;
  100. }
  101. {
  102. string url = "https://pub.alimama.com/openapi/param2/1/gateway.unionpub/union.login";
  103. var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
  104. .AddHeaders("X-Requested-With", "XMLHttpRequest")
  105. .AddHeaders("Cookie", _cookies);
  106. #if DEBUG
  107. #else
  108. client.Proxy = _proxy;
  109. #endif
  110. if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
  111. var response = client.Request(url);
  112. var body = response.Body();
  113. var root = body.Convert2JsonElement();
  114. /*
  115. {"success":true,"model":{}}
  116. */
  117. bool success = root.PathRead<bool>("success", true);
  118. string msgCode = root.PathRead("msgCode", string.Empty);
  119. string msgInfo = root.PathRead("msgInfo", string.Empty);
  120. if (!success)
  121. {
  122. _ = new LoggerLibrary("account", "error")
  123. .Info(url, body)
  124. .SaveAsync();
  125. return (false, body);
  126. }
  127. //{"success":false,"msgInfo":"用户淘宝未登录","msgCode":"USER_TB_NOT_LOGIN"}
  128. }
  129. string aso_url;
  130. {
  131. string url = "https://pub.alimama.com/openapi/param2/1/gateway.unionpub/union.auto.signIn.mama.url.get";
  132. var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
  133. .AddHeaders("X-Requested-With", "XMLHttpRequest")
  134. .AddHeaders("Cookie", _cookies);
  135. #if DEBUG
  136. #else
  137. client.Proxy = _proxy;
  138. #endif
  139. if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
  140. var response = client.Request(url);
  141. var body = response.Body();
  142. /*
  143. {"success":false,"msgInfo":"用户淘宝未登录","msgCode":"USER_TB_NOT_LOGIN"}
  144. {"model":{"url":"https://www.alimama.com/aso/tvs?domain=taobao&sign_account=670430e31dbb7459635d7db887fbc39b&sign_time=1717670916&token=64616532303JDaxNfPy83okP3kLScwCkGiuMcyC4Pcyn92UtF465646233393431&target=6874747073JDaxNfPy83okP3kLScwCkGiuMcyC4PcynF6D&defaultLoginURL=https%3A%2F%2Flogin.taobao.com%2Fmember%2Flogin.jhtml%3Ffrom%3Dalimama"},"success":true}
  145. */
  146. var root = body.Convert2JsonElement();
  147. aso_url = root.PathRead("model.url", string.Empty);
  148. bool success = root.Read("success", false);
  149. if (!success)
  150. {
  151. _ = new LoggerLibrary("account", "error")
  152. .Info(url, body)
  153. .SaveAsync();
  154. return (false, body);
  155. }
  156. }
  157. {
  158. var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
  159. .AddHeaders("X-Requested-With", "XMLHttpRequest")
  160. .AddHeaders("Cookie", _cookies);
  161. #if DEBUG
  162. #else
  163. client.Proxy = _proxy;
  164. #endif
  165. //client.AllowAutoRedirect = false;
  166. if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
  167. var response = client.Request(aso_url);
  168. List<Cookie> cookies_list = response.ResponseMessage.GetCookies();
  169. var body = response.Body();
  170. }
  171. {
  172. string url = "https://pub.alimama.com/common/getUnionPubContextInfo.json";
  173. var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
  174. .AddHeaders("X-Requested-With", "XMLHttpRequest")
  175. .AddHeaders("Cookie", _cookies);
  176. #if DEBUG
  177. #else
  178. client.Proxy = _proxy;
  179. #endif
  180. if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
  181. var response = client.Request(url);
  182. var body = response.Body();
  183. /*
  184. {"invalidKey":null,"info":{"ok":true,"message":null},"ok":true,"data":{"nnormalGradeLevel":3,"mmNick":"tb628735153169@alimama","hasHighCommPerm":1,"ip":"121.206.54.236","avatar":"//img.alicdn.com/bao/uploaded/i3/6000000006221/O1CN01S9dQzd1vpGrjjldrF_!!6000000006221-0-userheaderimgshow.jpg","userType":1,"env":"product","imgUrlPrefix":"//img.alicdn.com/tfscom","memberid":6172912691,"hasPubPermission":"1","newVersion":1}}
  185. */
  186. }
  187. new DBContext.Table("tk_pool")
  188. .Add("status", 1)
  189. .Add("last_time", DateTime.Now)
  190. .Where("name=@name", new { name = _accountName })
  191. .Update();
  192. _ = TkPoolCore.List(true);
  193. NotifyCore.Notify(new NifyMessage
  194. {
  195. message = $"【淘客{_accountId}:{_accountName}】cookie 续期",
  196. tags = ["green_circle"]
  197. });
  198. EndPointCore.NotifyReload(true);
  199. //NotifyCore.AnPushNotify("Cookie更新", $"【淘宝联盟:{_accountName}】cookie 续期");
  200. return (true, string.Empty);
  201. }
  202. catch (Exception ex)
  203. {
  204. _ = new LoggerLibrary("account", "error")
  205. .Info(ex.Message, ex.StackTrace)
  206. .SaveAsync();
  207. string message = $"【cookie续期异常】{_accountName}\n\n{ex.Message}\n{ex.StackTrace}";
  208. NotifyCore.Notify(new NifyMessage
  209. {
  210. message = message,
  211. priority = NifyMessagePriority.high,
  212. tags = ["red_circle"]
  213. });
  214. return (false, "cookie续期异常");
  215. }
  216. }
  217. }
  218. }