account.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using dodohold.core;
  2. using System.Net;
  3. namespace molilian.core
  4. {
  5. public partial class AlimamaPlus
  6. {
  7. /// <summary>
  8. /// 获取可提现余额
  9. /// </summary>
  10. public void GetDrawBalance()
  11. {
  12. DateTime date = DateTime.Now.AddDays(-1);
  13. TkPoolDTO result = new TkPoolDTO();
  14. try
  15. {
  16. string t = $"{date.Convert2UnixTimestamp(true)}";
  17. string dt = date.ToString("yyyy-MM-dd");
  18. string url = "https://pub.alimama.com/openapi/param2/1/gateway.unionpub/settle.account.balance.get.json?" +
  19. $"t={t}&_tb_token_={_tb_token}";
  20. var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
  21. .AddHeaders("X-Requested-With", "XMLHttpRequest")
  22. .AddHeaders("Cookie", _cookies);
  23. client.Proxy = _proxy;
  24. if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
  25. var response = client.Request(url);
  26. var body = response.Body();
  27. var root = body.Convert2JsonElement();
  28. int resultCode = root.Read<int>("resultCode", 0);
  29. bool success = root.Read("success", false);
  30. if (!success)
  31. {
  32. _ = new LoggerLibrary("account", "error")
  33. .Info(url, body)
  34. .SaveAsync();
  35. throw new Exception(body);
  36. }
  37. var drawBalance = root.PathRead<decimal>("data.totalBalance", 0);
  38. TkPoolCore.UpdateDrawBalance(_accountName, drawBalance);
  39. }
  40. catch (Exception ex) { }
  41. }
  42. public (bool, string) RenewCookie()
  43. {
  44. string cache_key = $"cache:tk_pool:{_accountName}:RenewCookie";
  45. long count = RedisHelper.IncrBy(cache_key);
  46. RedisHelper.Expire(cache_key, 30);
  47. if (count > 1) return (true, "前一个cookie续签正在执行中");
  48. try
  49. {
  50. {
  51. string url = "https://pub.alimama.com/common/getUnionPubContextInfo.json";
  52. var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
  53. .AddHeaders("X-Requested-With", "XMLHttpRequest")
  54. .AddHeaders("Cookie", _cookies);
  55. #if DEBUG
  56. #else
  57. client.Proxy = _proxy;
  58. #endif
  59. if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
  60. var response = client.Request(url);
  61. var body = response.Body();
  62. }
  63. /*
  64. {"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} */
  65. {
  66. string url = "https://pub.alimama.com/openapi/param2/1/gateway.unionpub/union.userLoginInfo.get";
  67. var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
  68. .AddHeaders("X-Requested-With", "XMLHttpRequest")
  69. .AddHeaders("Cookie", _cookies);
  70. #if DEBUG
  71. #else
  72. client.Proxy = _proxy;
  73. #endif
  74. if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
  75. var response = client.Request(url);
  76. var body = response.Body();
  77. var body2 = body;
  78. }
  79. {
  80. string url = "https://pub.alimama.com/openapi/param2/1/gateway.unionpub/union.login";
  81. var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
  82. .AddHeaders("X-Requested-With", "XMLHttpRequest")
  83. .AddHeaders("Cookie", _cookies);
  84. #if DEBUG
  85. #else
  86. client.Proxy = _proxy;
  87. #endif
  88. if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
  89. var response = client.Request(url);
  90. var body = response.Body();
  91. var root = body.Convert2JsonElement();
  92. /*
  93. {"success":true,"model":{}}
  94. */
  95. bool success = root.PathRead<bool>("success", true);
  96. string msgCode = root.PathRead("msgCode", string.Empty);
  97. string msgInfo = root.PathRead("msgInfo", string.Empty);
  98. if (!success)
  99. {
  100. _ = new LoggerLibrary("account", "error")
  101. .Info(url, body)
  102. .SaveAsync();
  103. return (false, body);
  104. }
  105. //{"success":false,"msgInfo":"用户淘宝未登录","msgCode":"USER_TB_NOT_LOGIN"}
  106. }
  107. string aso_url;
  108. {
  109. string url = "https://pub.alimama.com/openapi/param2/1/gateway.unionpub/union.auto.signIn.mama.url.get";
  110. var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
  111. .AddHeaders("X-Requested-With", "XMLHttpRequest")
  112. .AddHeaders("Cookie", _cookies);
  113. #if DEBUG
  114. #else
  115. client.Proxy = _proxy;
  116. #endif
  117. if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
  118. var response = client.Request(url);
  119. var body = response.Body();
  120. /*
  121. {"success":false,"msgInfo":"用户淘宝未登录","msgCode":"USER_TB_NOT_LOGIN"}
  122. {"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}
  123. */
  124. var root = body.Convert2JsonElement();
  125. aso_url = root.PathRead("model.url", string.Empty);
  126. bool success = root.Read("success", false);
  127. if (!success)
  128. {
  129. _ = new LoggerLibrary("account", "error")
  130. .Info(url, body)
  131. .SaveAsync();
  132. return (false, body);
  133. }
  134. }
  135. {
  136. var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
  137. .AddHeaders("X-Requested-With", "XMLHttpRequest")
  138. .AddHeaders("Cookie", _cookies);
  139. #if DEBUG
  140. #else
  141. client.Proxy = _proxy;
  142. #endif
  143. //client.AllowAutoRedirect = false;
  144. if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
  145. var response = client.Request(aso_url);
  146. List<Cookie> cookies_list = response.ResponseMessage.GetCookies();
  147. var body = response.Body();
  148. }
  149. {
  150. string url = "https://pub.alimama.com/common/getUnionPubContextInfo.json";
  151. var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
  152. .AddHeaders("X-Requested-With", "XMLHttpRequest")
  153. .AddHeaders("Cookie", _cookies);
  154. #if DEBUG
  155. #else
  156. client.Proxy = _proxy;
  157. #endif
  158. if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
  159. var response = client.Request(url);
  160. var body = response.Body();
  161. /*
  162. {"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}}
  163. */
  164. }
  165. new DBContext.Table("tk_pool")
  166. .Add("status", 1)
  167. .Add("last_time", DateTime.Now)
  168. .Where("name=@name", new { name = _accountName })
  169. .Update();
  170. _ = TkPoolCore.ListAsync(true);
  171. NotifyCore.Notify(new NifyMessage
  172. {
  173. message = $"【淘客{_accountId}:{_accountName}】cookie 续期",
  174. tags = ["green_circle"]
  175. });
  176. EndPointCore.NotifyReload(true);
  177. //NotifyCore.AnPushNotify("Cookie更新", $"【淘宝联盟:{_accountName}】cookie 续期");
  178. return (true, string.Empty);
  179. }
  180. catch (Exception ex)
  181. {
  182. _ = new LoggerLibrary("account", "error")
  183. .Info(ex.Message, ex.StackTrace)
  184. .SaveAsync();
  185. string message = $"【cookie续期异常】{_accountName}\n\n{ex.Message}\n{ex.StackTrace}";
  186. NotifyCore.Notify(new NifyMessage
  187. {
  188. message = message,
  189. priority = NifyMessagePriority.high,
  190. tags = ["red_circle"]
  191. });
  192. return (false, "cookie续期异常");
  193. }
  194. }
  195. }
  196. }