Files
kami_itunes_june/AppleBatch_June.ExecuteTasks/GiftcardBalanceAotuQueryTask.cs
2024-07-22 00:43:14 +08:00

449 lines
16 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using AppleBatch_June.Domain;
using AppleBatch_June.Model;
using AppleBatch_June.StartTaskModel;
using AppleBatch_June.Utils;
using DotNet.Utilities;
namespace AppleBatch_June.ExecuteTasks
{
public class GiftcardBalanceAotuQueryTask
{
private GiftcardBalanceWeb giftcardBalance;
public BaseFormTaskRunState taskState = new BaseFormTaskRunState("礼品卡余额定时查询");
private Action<string, DisplyType, string> action;
public static object loginLockObj = new object();
public DateTime loginTime = DateTime.Now.AddHours(-1.0);
public DateTime loginCheckTime = DateTime.Now.AddHours(-1.0);
public bool expirationExecuted { get; set; }
public string storeAppleHostLists
{
get
{
return _storeAppleHostLists;
}
set
{
if (value != _storeAppleHostLists)
{
if (giftcardBalance != null)
{
giftcardBalance = null;
}
giftcardBalance = new GiftcardBalanceWeb(value, action, action, taskState);
}
_storeAppleHostLists = value;
}
}
private string _storeAppleHostLists { get; set; }
public GiftcardBalanceAotuQueryTask(Action<string, DisplyType, string> _action, Action<string, DisplyType, string> _applyAtion)
{
action = _action;
giftcardBalance = new GiftcardBalanceWeb(_storeAppleHostLists, _action, _applyAtion, taskState);
}
public void QuertAppleBlance(List<AppleAcount> acounts, Action<string, DisplyType, string> reddemAction)
{
Task.Run(delegate
{
foreach (AppleAcount acount in acounts)
{
string appleId = acount.appleId;
string applePwd = acount.applePwd;
ItunesUtlis itunesUtlis = new ItunesUtlis(taskState, appleId, reddemAction);
reddemAction?.Invoke(appleId, DisplyType.xinxi, "正在查询..");
string msgReust = "";
AppleItunesLogin appleItunesLogin = itunesUtlis.remoteAppleLogin(appleId.Trim(), applePwd, ref msgReust, 2);
if (appleItunesLogin != null)
{
APIUtlis.ApiApplyAct(2, "余额查询");
string text = ((appleItunesLogin.isDisabledAccount.ToLower() == "false") ? "正常" : "禁用");
reddemAction?.Invoke(appleId, DisplyType.xinxi, "登录完成,余额:" + appleItunesLogin.Balance + ",状态:" + text + ",国家:" + appleItunesLogin.Area);
}
else
{
reddemAction?.Invoke(appleId, DisplyType.error, "失败");
reddemAction?.Invoke(appleId, DisplyType.xinxi, msgReust);
}
}
});
}
public void Reddem(string reddemCode, List<AppleAcount> acounts, Action<string, DisplyType, string> reddemAction)
{
ItunesUtlis itunesUtlis = new ItunesUtlis(taskState, reddemCode, reddemAction);
action?.Invoke(reddemCode, DisplyType.xinxi, "正在兑换..");
foreach (AppleAcount acount in acounts)
{
bool flag = false;
string appleId = acount.appleId;
string applePwd = acount.applePwd;
reddemAction?.Invoke(appleId, DisplyType.xinxi, "正在登录");
string msgReust = "";
AppleItunesLogin appleItunesLogin = itunesUtlis.remoteAppleLogin(appleId.Trim(), applePwd, ref msgReust, 1);
if (appleItunesLogin != null)
{
if (appleItunesLogin.isDisabledAccount.ToLower() == "false")
{
reddemAction?.Invoke(appleId, DisplyType.xinxi, "登录完成,国家:" + appleItunesLogin.Area);
string msg = "";
string errorMessageKey = "";
if (itunesUtlis.serverReddeme(appleItunesLogin, reddemCode.Trim(), ref msg, ref errorMessageKey, appleItunesLogin.serverIndex) == ReddemeState.valid && string.IsNullOrEmpty(errorMessageKey))
{
string text = appleId + msg;
action?.Invoke(reddemCode, DisplyType.reddemValid, text);
string value = new Regex("加载金额:([\\s\\S]*?),").Match(msg).Groups[1].Value;
decimal num = Tools.ConverCurrency(value);
reddemAction?.Invoke(appleId, DisplyType.reddemValid, num.ToString());
APIUtlis.ApiApplyAct(33, "礼品卡余额自动兑换");
action?.Invoke(reddemCode, DisplyType.xinxi, "兑换成功,金额:" + value.ToString());
ininserToDb(reddemCode, value, text);
break;
}
string[] array = AppSysConfig.getConfig("AotoReddemCardKeywordAccountError").Split(',');
foreach (string value2 in array)
{
if (!string.IsNullOrEmpty(value2) && errorMessageKey.Contains(value2))
{
reddemAction?.Invoke(appleId, DisplyType.error, "失败");
reddemAction?.Invoke(appleId, DisplyType.xinxi, msg);
flag = true;
break;
}
}
if (!flag)
{
action?.Invoke(reddemCode, DisplyType.xinxi, "卡状态未知");
action?.Invoke(reddemCode, DisplyType.reddemLose, appleId + msg);
ininserToDb(reddemCode, "", appleId + msg);
break;
}
}
else
{
reddemAction?.Invoke(appleId, DisplyType.error, "失败");
reddemAction?.Invoke(appleId, DisplyType.xinxi, "账户已被禁用");
}
}
else
{
reddemAction?.Invoke(appleId, DisplyType.error, "失败");
reddemAction?.Invoke(appleId, DisplyType.xinxi, msgReust);
}
}
}
public void Query(List<AppleAcount> lvItems, bool startVpn, string LoginAppleId, string LoginApwd, Action endAct, bool isAgain = false)
{
Task.Run(delegate
{
expirationExecuted = true;
int taskSize = Tools.getTaskSize(startVpn, lvItems.Count);
taskState.isRun = true;
taskState.TaskIsRun = true;
AppleExecuteTask.TaskRunAppleIdCount = 0L;
AppleExecuteTask.TaskRunModels.Clear();
string text = "";
if (lvItems.Count > 0)
{
text = lvItems[0].reddemCode;
}
giftcardBalance.appleId = text;
int num = 0;
DateTime now = DateTime.Now;
while (giftcardBalance.state == GiftcardBalanceWeb.GiftcardBalancState.busy && DateTime.Now.AddMinutes(-1.5) < now)
{
action(text, DisplyType.xinxi, "ID登录中" + getIngStr(num, 5));
Thread.Sleep(300);
num++;
}
if (giftcardBalance.state == GiftcardBalanceWeb.GiftcardBalancState.success && !(giftcardBalance.LoginAppleId != LoginAppleId))
{
action(text, DisplyType.xinxi, "ID登陆完成");
}
else
{
action(text, DisplyType.xinxi, "正在登陆ID" + getIngStr(3, 4));
if (!giftcardBalance.shopSignIn(LoginAppleId, LoginApwd, startVpn, out var error))
{
giftcardBalance.Reset();
action(text, DisplyType.error, error);
action(text, DisplyType.xinxi, error);
endAct?.Invoke();
return;
}
string arg = giftcardBalance.storeAppleHost.Replace(".store.apple.com", "");
GiftcardBalanceStandby standby = GiftcardBalanceManage.GetStandby(_storeAppleHostLists, LoginAppleId);
if (standby == null)
{
GiftcardBalanceManage.AddGifStandby(_storeAppleHostLists, LoginAppleId, LoginApwd);
standby = GiftcardBalanceManage.GetStandby(_storeAppleHostLists, LoginAppleId);
}
if (standby != null && giftcardBalance.storeAppleHostLists == _storeAppleHostLists)
{
standby.AddGiftcardBalance(giftcardBalance);
}
ProxyAccountCache.addProxyIp(LoginAppleId.Trim() + "_Aoto", giftcardBalance.ProxyIp);
action(text, DisplyType.newAccount, arg);
action(text, DisplyType.error, "ID登陆完成");
action(text, DisplyType.xinxi, "ID登陆完成");
}
Task[] array = new Task[taskSize];
for (int i = 0; i < taskSize; i++)
{
array[i] = Task.Run(delegate
{
string guid = Guid.NewGuid().ToString("n");
TaskRunModel taskRunModel = new TaskRunModel
{
guid = guid,
isRuning = false
};
AppleExecuteTask.TaskRunModels.Add(taskRunModel);
while (taskState.isRun)
{
AppleAcount appleAcount = null;
try
{
lock (lvItems)
{
appleAcount = lvItems.Where((AppleAcount c) => !c.isAct).FirstOrDefault();
if (appleAcount == null)
{
break;
}
appleAcount.isAct = true;
}
if (BaseTask.startRuning(taskRunModel, action, appleAcount.reddemCode))
{
GiftcardBalanceStandby standby2 = GiftcardBalanceManage.GetStandby(_storeAppleHostLists, LoginAppleId);
if (standby2 == null)
{
GiftcardBalanceManage.AddGifStandby(_storeAppleHostLists, LoginAppleId, LoginApwd);
standby2 = GiftcardBalanceManage.GetStandby(_storeAppleHostLists, LoginAppleId);
standby2.GetRandomGiftcard(startVpn);
}
lock (loginLockObj)
{
if (DateTime.Now.AddSeconds(-10.0) > loginCheckTime)
{
loginCheckTime = DateTime.Now;
standby2.check(startVpn, action);
}
}
action(appleAcount.reddemCode, DisplyType.xinxi, "正在处理");
int id = 23;
HttpResult isLogin = APIUtlis.getIsLogin(23);
if (isLogin.StatusCode == HttpStatusCode.OK)
{
object obj = Tools.Todejosn<object>(isLogin.Html);
if (!((((dynamic)obj)["Code"] == "0000") ? true : false))
{
action(appleAcount.reddemCode, DisplyType.xinxi, ((dynamic)obj)["Message"]);
}
else if (((dynamic)obj)["Data"]["integral"] >= AppSysConfig.getTypeById(id).consNum)
{
action(appleAcount.appleId, DisplyType.chongzhi, "");
bool flag = false;
int num2 = 0;
int num3 = 10;
do
{
flag = false;
GiftcardBalanceWeb randomGiftcard = standby2.GetRandomGiftcard(startVpn);
if (randomGiftcard == null)
{
flag = true;
action(appleAcount.reddemCode, DisplyType.xinxi, "等待重新登录中..." + (num2 + 1));
}
else
{
randomGiftcard.ProxyIp = ProxyAccountCache.getProxyAccountIP(LoginAppleId + "_Aoto", 18);
randomGiftcard.isUpProxyAccountCache = true;
randomGiftcard.action = action;
randomGiftcard.taskState = taskState;
GiftcardBalanceModel giftcardBalanceModel = randomGiftcard.giftCardBalanceCheck(appleAcount.reddemCode, startVpn);
if (giftcardBalanceModel != null)
{
if (giftcardBalanceModel.head.status == 200 && !giftcardBalanceModel.body.giftCardBalanceCheck.d.maxAttemptReached)
{
Dictionary<string, string> dictionary = new Dictionary<string, string>
{
{ "type", "giftcardBalance" },
{
"addTime",
Tools.GenerateTimeStamp().ToString()
},
{ "value1", appleAcount.reddemCode }
};
if (giftcardBalanceModel.body.giftCardBalanceCheck.d.balance != null)
{
string balance = giftcardBalanceModel.body.giftCardBalanceCheck.d.balance;
dictionary.Add("value2", balance);
dictionary.Add("value3", "查询完成");
action(appleAcount.reddemCode, DisplyType.xinxi, "查询完成,余额:" + balance);
action(appleAcount.reddemCode, DisplyType.balance, balance);
string arg2 = HttpUtility.HtmlDecode(giftcardBalanceModel.body.giftCardBalanceCheck.d.giftCardNumber);
action(appleAcount.reddemCode, DisplyType.lostBillNo, arg2);
APIUtlis.ApiApplyAct(34, "自动礼品卡余额有效");
SqliteHelper.ExecuteInsert("itunes_db", dictionary);
}
else if (giftcardBalanceModel.body.giftCardBalanceCheck.d.alertMessages != null)
{
string text2 = "错误:" + giftcardBalanceModel.body.giftCardBalanceCheck.d.alertMessages[0];
dictionary.Add("value2", "无");
dictionary.Add("value3", text2);
action(appleAcount.reddemCode, DisplyType.balance, "无");
action(appleAcount.reddemCode, DisplyType.xinxi, text2);
if (int.TryParse(appleAcount.overflow, out var result) && result > 0)
{
APIUtlis.ApiApplyAct(35, "自动礼品卡余额无效");
}
else
{
APIUtlis.ApiApplyAct(23, "自动礼品卡余额无效");
}
SqliteHelper.ExecuteInsert("itunes_db", dictionary);
}
else
{
string text3 = "错误:不是有效的礼品卡";
dictionary.Add("value2", "无");
dictionary.Add("value3", text3);
if (int.TryParse(appleAcount.overflow, out var result2) && result2 > 0)
{
APIUtlis.ApiApplyAct(35, "自动礼品卡余额无效");
}
else
{
APIUtlis.ApiApplyAct(23, "自动礼品卡余额无效");
SqliteHelper.ExecuteInsert("itunes_db", dictionary);
}
action(appleAcount.reddemCode, DisplyType.balance, "无");
action(appleAcount.reddemCode, DisplyType.xinxi, text3);
}
action(appleAcount.reddemCode, DisplyType.gameCount, "+1");
}
else
{
bool? flag2 = giftcardBalanceModel?.body?.giftCardBalanceCheck?.d?.maxAttemptReached;
if (giftcardBalanceModel.head.status != 302 && (!flag2.HasValue || !flag2.Value))
{
randomGiftcard.timeCheck = DateTime.Now.AddMinutes(10.5).ToString("yyMMddHHmmss");
flag = true;
action(appleAcount.reddemCode, DisplyType.xinxi, "未知,等待重新查询.." + (num2 + 1));
}
else
{
flag = true;
standby2.removeGiftcardBalance(randomGiftcard.StdndbyKey);
action(appleAcount.reddemCode, DisplyType.xinxi, "等待重新查询中..." + (num2 + 1));
}
}
}
else
{
action(appleAcount.reddemCode, DisplyType.xinxi, "请求错误,请重新查询");
}
}
if (flag)
{
if (num2 + 1 < num3)
{
Thread.Sleep(1000 * (2 * num2));
}
else
{
action(appleAcount.reddemCode, DisplyType.xinxi, "查询失败,请重新登录");
}
}
if (!taskState.isRun)
{
action(appleAcount.reddemCode, DisplyType.xinxi, "停止执行");
}
num2++;
}
while (flag && num2 < num3 && taskState.isRun);
}
else
{
action(appleAcount.reddemCode, DisplyType.xinxi, "点数不足,请联系客服充值");
}
}
else
{
action(appleAcount.reddemCode, DisplyType.xinxi, "网络请求失败");
}
goto IL_0b3d;
}
}
catch (Exception ex)
{
APIUtlis.ApiSeriveError(ex);
if (appleAcount != null)
{
action(appleAcount.reddemCode, DisplyType.xinxi, "请求失败:" + ex.Message);
}
goto IL_0b3d;
}
continue;
IL_0b3d:
BaseTask.endRuning(taskRunModel);
}
});
}
Task.WaitAll(array);
AppleExecuteTask.TaskRunModels.Clear();
taskState.isRun = false;
taskState.TaskIsRun = false;
expirationExecuted = false;
endAct?.Invoke();
});
}
public void ininserToDb(string reddemCode, string balance, string errmsg)
{
try
{
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.Add("type", "giftcardBalance");
dictionary.Add("addTime", Tools.GenerateTimeStamp().ToString());
dictionary.Add("value1", reddemCode);
dictionary.Add("value2", balance);
dictionary.Add("value3", errmsg);
SqliteHelper.ExecuteInsert("itunes_db", dictionary);
}
catch (Exception)
{
}
}
public void Stop()
{
taskState.Stop();
}
private string getIngStr(int cont, int maxCount)
{
int totalWidth = cont % maxCount;
return ".".PadRight(totalWidth, '.');
}
}
}