Skip to content

Commit e76fd6d

Browse files
LinwenxuanLinwenxuan
authored andcommitted
[Core] Minor refactor for WtExchangeLogic.cs
1 parent e5e0a67 commit e76fd6d

File tree

1 file changed

+46
-88
lines changed

1 file changed

+46
-88
lines changed

Lagrange.Core/Internal/Context/Logic/Implementation/WtExchangeLogic.cs

Lines changed: 46 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ internal class WtExchangeLogic : LogicBase
2323
{
2424
private const string Tag = nameof(WtExchangeLogic);
2525

26-
private readonly TaskCompletionSource<bool> _qrCodeTask;
27-
private readonly TaskCompletionSource<bool> _unusualTask;
26+
private readonly TaskCompletionSource<bool> _transEmpTask;
2827
private TaskCompletionSource<(string, string)>? _captchaTask;
2928

3029
private const string Interface = "https://ntlogin.qq.com/qr/getFace";
@@ -33,8 +32,7 @@ internal class WtExchangeLogic : LogicBase
3332

3433
internal WtExchangeLogic(ContextCollection collection) : base(collection)
3534
{
36-
_qrCodeTask = new TaskCompletionSource<bool>();
37-
_unusualTask = new TaskCompletionSource<bool>();
35+
_transEmpTask = new TaskCompletionSource<bool>();
3836
}
3937

4038
public override async Task Incoming(ProtocolEvent e)
@@ -80,8 +78,19 @@ public override async Task Incoming(ProtocolEvent e)
8078

8179
public Task LoginByQrCode()
8280
{
83-
Collection.Scheduler.Interval(QueryEvent, 2 * 1000, async () => await QueryQrCodeState());
84-
return _qrCodeTask.Task;
81+
Collection.Scheduler.Interval(QueryEvent, 2 * 1000, async () => await QueryTransEmpState(async @event =>
82+
{
83+
if (@event.TgtgtKey != null)
84+
{
85+
Collection.Keystore.Stub.TgtgtKey = @event.TgtgtKey;
86+
Collection.Keystore.Session.TempPassword = @event.TempPassword;
87+
Collection.Keystore.Session.NoPicSig = @event.NoPicSig;
88+
}
89+
90+
return await DoWtLogin();
91+
}));
92+
93+
return _transEmpTask.Task;
8594
}
8695

8796
public async Task<bool> LoginByPassword()
@@ -120,22 +129,31 @@ public async Task<bool> LoginByPassword()
120129
}
121130
case LoginCommon.Error.UnusualVerify:
122131
{
123-
Collection.Log.LogInfo(Tag, "Login Success, but need to verify");
132+
Collection.Log.LogInfo(Tag, "Verification needed");
124133

125134
if (!await FetchUnusual())
126135
{
127136
Collection.Log.LogInfo(Tag, "Fetch unusual state failed");
128137
return false;
129138
}
130139

131-
Collection.Scheduler.Interval(QueryEvent, 2 * 1000, async () => await QueryUnusualState());
132-
bool result = await _unusualTask.Task;
140+
Collection.Scheduler.Interval(QueryEvent, 2 * 1000, async () => await QueryTransEmpState(async e =>
141+
{
142+
if (e.TempPassword != null)
143+
{
144+
Collection.Keystore.Session.TempPassword = e.TempPassword;
145+
return await DoUnusualEasyLogin();
146+
}
147+
148+
return false;
149+
}));
150+
bool result = await _transEmpTask.Task;
133151
if (result) await BotOnline();
134152
return result;
135153
}
136154
default:
137155
{
138-
Collection.Log.LogWarning(Tag, "Fast Login Failed, trying to Login by Password...");
156+
Collection.Log.LogWarning(Tag, $"Fast Login Failed with code {easyLoginResult[0].ResultCode}, trying to Login by Password...");
139157

140158
Collection.Keystore.Session.TempPassword = null; // clear temp password
141159
return await LoginByPassword(); // try password login
@@ -163,10 +181,7 @@ public async Task<bool> LoginByPassword()
163181
}
164182
case LoginCommon.Error.UnusualVerify:
165183
{
166-
Collection.Log.LogInfo(Tag, "Login Success, but need to verify");
167-
168-
await FetchUnusual();
169-
Collection.Scheduler.Interval(QueryEvent, 2 * 1000, async () => await QueryUnusualState());
184+
Collection.Log.LogInfo(Tag, "Unusual Verify is not currently supported for PasswordLogin");
170185
return true;
171186
}
172187
case LoginCommon.Error.CaptchaVerify:
@@ -246,26 +261,23 @@ private async Task<bool> DoWtLogin()
246261
return false;
247262
}
248263

249-
private async Task QueryQrCodeState()
264+
private async Task QueryTransEmpState(Func<TransEmpEvent, Task<bool>> callback)
250265
{
251-
if (Collection.Keystore.Session.QrString == null)
266+
if (Collection.Keystore.Session.QrString != null)
252267
{
253-
Collection.Log.LogFatal(Tag, "QrString is null, Please Fetch QrCode First");
254-
_qrCodeTask.SetResult(false);
255-
return;
268+
var request = new NTLoginHttpRequest
269+
{
270+
Appid = Collection.AppInfo.AppId,
271+
Qrsig = Collection.Keystore.Session.QrString,
272+
FaceUpdateTime = 0
273+
};
274+
275+
var payload = JsonSerializer.SerializeToUtf8Bytes(request);
276+
var response = await Http.PostAsync(Interface, payload, "application/json");
277+
var info = JsonSerializer.Deserialize<NTLoginHttpResponse>(response);
278+
if (info != null) Collection.Keystore.Uin = info.Uin;
256279
}
257280

258-
var request = new NTLoginHttpRequest
259-
{
260-
Appid = Collection.AppInfo.AppId,
261-
Qrsig = Collection.Keystore.Session.QrString,
262-
FaceUpdateTime = 0
263-
};
264-
var payload = JsonSerializer.SerializeToUtf8Bytes(request);
265-
var response = await Http.PostAsync(Interface, payload, "application/json");
266-
var info = JsonSerializer.Deserialize<NTLoginHttpResponse>(response);
267-
if (info != null) Collection.Keystore.Uin = info.Uin;
268-
269281
var transEmp = TransEmpEvent.Create(TransEmpEvent.State.QueryResult);
270282
var result = await Collection.Business.SendEvent(transEmp);
271283

@@ -281,15 +293,7 @@ private async Task QueryQrCodeState()
281293
{
282294
Collection.Log.LogInfo(Tag, "QrCode Confirmed, Logging in with A1 sig...");
283295
Collection.Scheduler.Cancel(QueryEvent); // cancel query task
284-
285-
if (@event.TgtgtKey != null)
286-
{
287-
Collection.Keystore.Stub.TgtgtKey = @event.TgtgtKey;
288-
Collection.Keystore.Session.TempPassword = @event.TempPassword;
289-
Collection.Keystore.Session.NoPicSig = @event.NoPicSig;
290-
291-
_qrCodeTask.SetResult(await DoWtLogin());
292-
}
296+
_transEmpTask.SetResult(await callback.Invoke(@event));
293297
break;
294298
}
295299
case TransEmp12.State.CodeExpired:
@@ -298,7 +302,7 @@ private async Task QueryQrCodeState()
298302
Collection.Scheduler.Cancel(QueryEvent);
299303
Collection.Scheduler.Dispose();
300304

301-
_qrCodeTask.SetResult(false);
305+
_transEmpTask.SetResult(false);
302306
return;
303307
}
304308
case TransEmp12.State.Canceled:
@@ -307,7 +311,7 @@ private async Task QueryQrCodeState()
307311
Collection.Scheduler.Cancel(QueryEvent);
308312
Collection.Scheduler.Dispose();
309313

310-
_qrCodeTask.SetResult(false);
314+
_transEmpTask.SetResult(false);
311315
return;
312316
}
313317
case TransEmp12.State.WaitingForConfirm:
@@ -347,59 +351,13 @@ private async Task<bool> FetchUnusual()
347351
return false;
348352
}
349353

350-
private async Task QueryUnusualState()
351-
{
352-
var transEmp = TransEmpEvent.Create(TransEmpEvent.State.QueryResult);
353-
var result = await Collection.Business.SendEvent(transEmp);
354-
355-
if (result.Count != 0)
356-
{
357-
var @event = (TransEmpEvent)result[0];
358-
var state = (TransEmp12.State)@event.ResultCode;
359-
Collection.Log.LogInfo(Tag, $"Confirmation State Queried: {state}");
360-
361-
switch (state)
362-
{
363-
case TransEmp12.State.Confirmed:
364-
{
365-
Collection.Log.LogInfo(Tag, "Verification Confirmed, Logging in Unusual Login Service...");
366-
Collection.Scheduler.Cancel(QueryEvent); // cancel query task
367-
368-
if (@event.TempPassword != null) Collection.Keystore.Session.TempPassword = @event.TempPassword;
369-
_unusualTask.SetResult(await DoUnusualEasyLogin());
370-
break;
371-
}
372-
case TransEmp12.State.CodeExpired:
373-
{
374-
Collection.Log.LogWarning(Tag, "Verification Expired, Please Login Again");
375-
Collection.Scheduler.Cancel(QueryEvent);
376-
Collection.Scheduler.Dispose();
377-
378-
_unusualTask.SetResult(false);
379-
break;
380-
}
381-
case TransEmp12.State.Canceled:
382-
{
383-
Collection.Log.LogWarning(Tag, "Verification Canceled, Please Login Again");
384-
Collection.Scheduler.Cancel(QueryEvent);
385-
Collection.Scheduler.Dispose();
386-
387-
_unusualTask.SetResult(false);
388-
break;
389-
}
390-
case TransEmp12.State.WaitingForConfirm:
391-
default:
392-
break;
393-
}
394-
}
395-
}
396-
397354
private async Task<bool> DoUnusualEasyLogin()
398355
{
399356
Collection.Log.LogInfo(Tag, "Trying to Login by EasyLogin...");
400357
var unusualEvent = UnusualEasyLoginEvent.Create();
401358
var result = await Collection.Business.SendEvent(unusualEvent);
402359
return result.Count != 0 && ((UnusualEasyLoginEvent)result[0]).Success;
360+
}
403361

404362
}
405363

0 commit comments

Comments
 (0)