mirror of
https://git.oceanpay.cc/danial/kami_itunes_june.git
synced 2025-12-18 21:21:34 +00:00
247 lines
8.6 KiB
C#
247 lines
8.6 KiB
C#
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using AppleBatch_June.Model;
|
|
using AppleBatch_June.StartTaskModel;
|
|
using AppleBatch_June.Utils;
|
|
|
|
namespace AppleBatch_June.ExecuteTasks
|
|
{
|
|
public class ReddemTask : BaseTask
|
|
{
|
|
private readonly Dictionary<string, ItunesUtlis> ReddemAccountUtlis = new Dictionary<string, ItunesUtlis>();
|
|
|
|
private readonly Dictionary<string, object> objReddemLockS = new Dictionary<string, object>();
|
|
|
|
private readonly object objReddemLock = new object();
|
|
|
|
public static ReddemTask Instance { get; private set; }
|
|
|
|
static ReddemTask()
|
|
{
|
|
Instance = new ReddemTask("卡片兑换");
|
|
}
|
|
|
|
public ReddemTask(string _taskName)
|
|
: base(_taskName)
|
|
{
|
|
}
|
|
|
|
public void AppleReddemTask(int TaskSize, List<AppleAcount> lvItems, bool _OpenVpn, bool remoteReddem, bool singleProcessRedeem, int reddemDelay, List<string> freezeAppleids, Action startAct, Action endTakAct, Action<string, string> sucessAct, Action<string, DisplyType, string> _action, Action<string, DisplyType, string> _applyAtion, Func<string, string, int, string> actGetCode = null)
|
|
{
|
|
TaskSize = Tools.getTaskSize(_OpenVpn, lvItems.Count);
|
|
if (!base.isRun)
|
|
{
|
|
AppleExecuteTask.TaskRunAppleIdCount = 0L;
|
|
AppleExecuteTask.TaskRunModels.Clear();
|
|
startAct?.Invoke();
|
|
base.isRun = true;
|
|
Task.Run(delegate
|
|
{
|
|
ApiNetReq.startNowFun = 8;
|
|
ReddemAccountUtlis.Clear();
|
|
base.baseEndTakAct = endTakAct;
|
|
base.TaskIsRun = true;
|
|
Task[] array = new Task[TaskSize];
|
|
ConcurrentDictionary<string, string> pauseRedeemAppleIds = new ConcurrentDictionary<string, string>();
|
|
foreach (string item in freezeAppleids)
|
|
{
|
|
pauseRedeemAppleIds.AddOrUpdate(item, "111", (string c, string d) => item);
|
|
}
|
|
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 item2 in lvItems.Where((AppleAcount c) => c.appleId == listView.appleId).ToList())
|
|
{
|
|
item2.reddeemLockTaskGuid = guid;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
appleAcount.isAct = true;
|
|
listView = appleAcount;
|
|
}
|
|
}
|
|
if (BaseTask.startRuning(taskRunModel, _action, listView.reddemCode))
|
|
{
|
|
if (pauseRedeemAppleIds.ContainsKey(listView.appleId))
|
|
{
|
|
break;
|
|
}
|
|
ItunesUtlis itunesUtlis = null;
|
|
AppleItunesLogin appleItunesLogin = null;
|
|
lock (objReddemLock)
|
|
{
|
|
if (!objReddemLockS.ContainsKey(listView.appleId))
|
|
{
|
|
objReddemLockS.Add(listView.appleId, new object());
|
|
}
|
|
if (!ReddemAccountUtlis.ContainsKey(listView.appleId.Trim()))
|
|
{
|
|
itunesUtlis = new ItunesUtlis(this, listView.guidTag, _action, _OpenVpn);
|
|
ReddemAccountUtlis.Add(listView.appleId.Trim(), itunesUtlis);
|
|
}
|
|
else
|
|
{
|
|
itunesUtlis = ReddemAccountUtlis[listView.appleId.Trim()];
|
|
}
|
|
}
|
|
lock (objReddemLockS[listView.appleId])
|
|
{
|
|
appleItunesLogin = getItunesLogin(listView, 8, _action, itunesUtlis, 1, listView.guidTag);
|
|
}
|
|
if (appleItunesLogin != null)
|
|
{
|
|
string msg = "";
|
|
ReddemeState reddemeState = ReddemeState.Unknown;
|
|
if (AppSysConfig.getConfig("ReddemCHNCheckRealName") == "1" && appleItunesLogin.AreaCode == "CHN" && !itunesUtlis.checkAccountRealNameState(listView.appleId))
|
|
{
|
|
_action?.Invoke(listView.guidTag, DisplyType.xinxi, "查询中国账号是否已实名..");
|
|
switch (itunesUtlis.CheckNeedRealName(appleItunesLogin))
|
|
{
|
|
case CheckRealNameType.Need:
|
|
_action?.Invoke(listView.guidTag, DisplyType.xinxi, "ID账号未实名认证");
|
|
goto end_IL_0055;
|
|
case CheckRealNameType.Unknown:
|
|
_action?.Invoke(listView.guidTag, DisplyType.xinxi, "实名检查失败,请重试");
|
|
goto end_IL_0055;
|
|
}
|
|
}
|
|
_action?.Invoke(listView.guidTag, DisplyType.xinxi, "正在兑换");
|
|
appleItunesLogin.reddemQts = listView.reddemQts;
|
|
string errorMessageKey = "";
|
|
if (remoteReddem && AppSysConfig.iTunesisRemoteLogin)
|
|
{
|
|
reddemeState = itunesUtlis.serverReddeme(appleItunesLogin, listView.reddemCode, ref msg, ref errorMessageKey, appleItunesLogin.serverIndex);
|
|
}
|
|
else
|
|
{
|
|
reddemeState = itunesUtlis.reddeme(appleItunesLogin, _OpenVpn, listView.reddemCode, listView.guidTag, ref msg, ref errorMessageKey, accepted: false, actGetCode);
|
|
if (reddemeState == ReddemeState.doLogin)
|
|
{
|
|
_action?.Invoke(listView.guidTag, DisplyType.xinxi, "正在重新登录..");
|
|
appleItunesLogin = getItunesLogin(listView, 8, _action, itunesUtlis, 2);
|
|
if (appleItunesLogin != null)
|
|
{
|
|
iTunesAccountLoginCache.addReddemUserInfo(listView.appleId, appleItunesLogin);
|
|
_action?.Invoke(listView.guidTag, DisplyType.xinxi, "正在兑换");
|
|
reddemeState = itunesUtlis.reddeme(appleItunesLogin, _OpenVpn, listView.reddemCode, listView.guidTag, ref msg, ref errorMessageKey);
|
|
}
|
|
}
|
|
}
|
|
_action?.Invoke(listView.guidTag, DisplyType.xinxi, msg);
|
|
if (msg.Contains("兑换暂不可用") && AppSysConfig.ckShowTime)
|
|
{
|
|
_action?.Invoke(listView.guidTag, DisplyType.xiaofeiriqiNow, "-");
|
|
pauseRedeemAppleIds.AddOrUpdate(listView.appleId, listView.reddemCode, (string c, string d) => listView.reddemCode);
|
|
break;
|
|
}
|
|
if (reddemeState != ReddemeState.Unknown)
|
|
{
|
|
if (reddemeState == ReddemeState.lose)
|
|
{
|
|
_applyAtion?.Invoke(listView.guidTag, DisplyType.reddemLose, "无效");
|
|
}
|
|
else
|
|
{
|
|
_applyAtion?.Invoke(listView.guidTag, DisplyType.reddemValid, "有效");
|
|
}
|
|
if (msg.Contains("安全原因"))
|
|
{
|
|
iTunesAccountLoginCache.removeReddemUserInfo(appleItunesLogin.account, appleItunesLogin.StdndbyKeyGuid);
|
|
iCloudAccountLoginCache.removeUserInfoByAppleId(appleItunesLogin.account, appleItunesLogin.pwd);
|
|
}
|
|
if (!msg.Contains("本商城无法获得您的账户信息"))
|
|
{
|
|
APIUtlis.ApiApplyAct(8, "Apple兑换");
|
|
}
|
|
if (singleProcessRedeem)
|
|
{
|
|
Thread.Sleep(reddemDelay);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
_applyAtion?.Invoke(listView.guidTag, DisplyType.reddemUNlock, "未知");
|
|
}
|
|
sucessAct?.Invoke(listView.guidTag, listView.appleId);
|
|
}
|
|
goto IL_07d2;
|
|
}
|
|
end_IL_0055:;
|
|
}
|
|
catch (Exception ex2)
|
|
{
|
|
APIUtlis.ApiSeriveError(ex2);
|
|
if (listView != null)
|
|
{
|
|
_action?.Invoke(listView.guidTag, DisplyType.xinxi, "请求失败:" + ex2.Message);
|
|
}
|
|
goto IL_07d2;
|
|
}
|
|
continue;
|
|
IL_07d2:
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
}
|