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

260 lines
8.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AppleBatch_June.Model;
using AppleBatch_June.StartTaskModel;
namespace AppleBatch_June.ExecuteTasks
{
public class CycleReddemTask : BaseTask
{
private readonly Dictionary<string, AppleItunesLogin> ReddemLogAccount = new Dictionary<string, AppleItunesLogin>();
private readonly Dictionary<string, object> objReddemLockS = new Dictionary<string, object>();
private readonly object objReddemLock = new object();
public static CycleReddemTask Instance { get; private set; }
static CycleReddemTask()
{
Instance = new CycleReddemTask("循环卡片兑换");
}
public CycleReddemTask(string _taskName)
: base(_taskName)
{
}
public void AppleCycleReddemTask(int TaskSize, List<AppleAcount> lvItems, bool _OpenVpn, bool remoteReddem, bool singleProcessRedeem, Action startAct, Action endTakAct, Action<string, string> sucessAct, Action<string, DisplyType, string> _action, Action<string, DisplyType, string> _applyAtion)
{
TaskSize = Tools.getTaskSize(_OpenVpn, lvItems.Count);
if (!base.isRun)
{
AppleExecuteTask.TaskRunAppleIdCount = 0L;
AppleExecuteTask.TaskRunModels.Clear();
startAct?.Invoke();
base.isRun = true;
Task.Run(delegate
{
ReddemLogAccount.Clear();
ApiNetReq.startNowFun = 37;
base.TaskIsRun = true;
base.baseEndTakAct = endTakAct;
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 (base.isRun)
{
AppleAcount listView = null;
try
{
lock (lvItems)
{
AppleAcount appleAcount = lvItems.Where((AppleAcount c) => !c.isAct && c.reddeemLockTaskGuid == guid).FirstOrDefault();
if (appleAcount == null)
{
listView = lvItems.Where((AppleAcount c) => !c.isAct && string.IsNullOrEmpty(c.reddeemLockTaskGuid)).FirstOrDefault();
if (listView == null)
{
break;
}
listView.isAct = true;
if (singleProcessRedeem)
{
foreach (AppleAcount item in lvItems.Where((AppleAcount c) => c.appleId == listView.appleId).ToList())
{
item.reddeemLockTaskGuid = guid;
}
}
}
else
{
appleAcount.isAct = true;
listView = appleAcount;
}
}
if (BaseTask.startRuning(taskRunModel, _action, listView.reddemCode))
{
ItunesUtlis itunesUtlis = new ItunesUtlis(this, listView.reddemCode, _action, _OpenVpn);
AppleItunesLogin appleItunesLogin = null;
lock (objReddemLock)
{
if (!objReddemLockS.ContainsKey(listView.appleId))
{
objReddemLockS.Add(listView.appleId, new object());
}
}
lock (objReddemLockS[listView.appleId])
{
if (!ReddemLogAccount.ContainsKey(listView.appleId))
{
appleItunesLogin = getItunesLogin(listView, 8, _action, itunesUtlis, 1);
if (appleItunesLogin != null)
{
ReddemLogAccount.Add(listView.appleId, appleItunesLogin);
}
}
else
{
appleItunesLogin = ReddemLogAccount[listView.appleId];
}
}
if (appleItunesLogin != null)
{
string msg = "";
ReddemeState reddemeState = ReddemeState.Unknown;
if (AppSysConfig.getConfig("ReddemCHNCheckRealName") == "1" && appleItunesLogin.AreaCode == "CHN" && !itunesUtlis.checkAccountRealNameState(listView.appleId))
{
_action?.Invoke(listView.reddemCode, DisplyType.xinxi, "查询中国账号是否已实名..");
switch (itunesUtlis.CheckNeedRealName(appleItunesLogin))
{
case CheckRealNameType.Need:
_action?.Invoke(listView.reddemCode, DisplyType.xinxi, "ID账号未实名认证");
goto end_IL_0055;
case CheckRealNameType.Unknown:
_action?.Invoke(listView.reddemCode, DisplyType.xinxi, "实名检查失败,请重试");
goto end_IL_0055;
}
}
_action?.Invoke(listView.reddemCode, DisplyType.xinxi, "正在兑换");
appleItunesLogin.reddemQts = listView.reddemQts;
string errorMessageKey = "";
if (remoteReddem)
{
reddemeState = itunesUtlis.serverReddeme(appleItunesLogin, listView.reddemCode, ref msg, ref errorMessageKey, appleItunesLogin.serverIndex);
}
else
{
reddemeState = itunesUtlis.reddeme(appleItunesLogin, _OpenVpn, listView.reddemCode, listView.reddemCode, ref msg, ref errorMessageKey);
if (reddemeState == ReddemeState.doLogin)
{
_action?.Invoke(listView.reddemCode, DisplyType.xinxi, "正在重新登录..");
appleItunesLogin = getItunesLogin(listView, 8, _action, itunesUtlis, 2);
if (appleItunesLogin != null)
{
if (ReddemLogAccount.ContainsKey(listView.appleId))
{
ReddemLogAccount[listView.appleId] = appleItunesLogin;
}
else
{
ReddemLogAccount.Add(listView.appleId, appleItunesLogin);
}
_action?.Invoke(listView.reddemCode, DisplyType.xinxi, "正在兑换");
reddemeState = itunesUtlis.reddeme(appleItunesLogin, _OpenVpn, listView.reddemCode, listView.reddemCode, ref msg, ref errorMessageKey);
}
}
}
_action?.Invoke(listView.reddemCode, DisplyType.xinxi, msg);
int num = 8;
if (reddemeState != ReddemeState.Unknown)
{
if (reddemeState == ReddemeState.lose)
{
_applyAtion?.Invoke(listView.reddemCode, DisplyType.reddemLose, "无效");
}
else if (msg.Contains("兑换成功"))
{
_applyAtion?.Invoke(listView.reddemCode, DisplyType.reddemValid, "完成");
num = 36;
}
else
{
_applyAtion?.Invoke(listView.reddemCode, DisplyType.reddemValid, "有效");
}
if (int.TryParse(listView.overflow, out var result) && result > 0 && num == 8)
{
num = 37;
}
if (!msg.Contains("本商城无法获得您的账户信息"))
{
APIUtlis.ApiApplyAct(num, "Apple兑换");
}
}
else
{
bool flag = false;
string[] array3 = AppSysConfig.getConfig("ReddemCardKeywordLoseOther").Split(',');
foreach (string value in array3)
{
if (!string.IsNullOrEmpty(value) && errorMessageKey.Contains(value))
{
_applyAtion?.Invoke(listView.reddemCode, DisplyType.reddemLose, "无效");
flag = true;
}
}
if (!flag)
{
_applyAtion?.Invoke(listView.reddemCode, DisplyType.reddemUNlock, "未知");
lock (objReddemLock)
{
ReddemLogAccount.Remove(listView.appleId);
}
}
else
{
APIUtlis.ApiApplyAct(37, "循环兑换无效");
}
}
sucessAct?.Invoke(listView.reddemCode, listView.appleId);
}
goto IL_0799;
}
end_IL_0055:;
}
catch (Exception ex2)
{
APIUtlis.ApiSeriveError(ex2);
if (listView != null)
{
_action?.Invoke(listView.reddemCode, DisplyType.xinxi, "请求失败:" + ex2.Message);
}
goto IL_0799;
}
continue;
IL_0799:
BaseTask.endRuning(taskRunModel);
}
});
}
Task.WaitAll(array);
try
{
Task[] array2 = array;
foreach (Task task in array2)
{
if (task.Status == TaskStatus.RanToCompletion)
{
task.Dispose();
}
}
array = null;
}
catch (Exception)
{
}
AppleExecuteTask.TaskRunModels.Clear();
base.TaskIsRun = false;
base.isRun = false;
endTakAct?.Invoke();
});
}
else
{
endTakAct?.Invoke();
}
}
}
}