Skip to content

Commit b4edbcb

Browse files
committed
add uuid Model
1 parent 008af4b commit b4edbcb

File tree

5 files changed

+48
-30
lines changed

5 files changed

+48
-30
lines changed

WeChatBot.Net.Tests/ClientTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public async Task RunTest_Normal_ShouldOuputQRCode()
3333
public async Task GetUuidTest_Normal_ShouldReturnTrue()
3434
{
3535
var q = await new Client().GetUuid();
36-
Assert.IsTrue(q.Item1);
36+
Assert.IsTrue(q.HasValue);
3737
}
3838

3939
[Test]

WeChatBot.Net/Client.cs

+28-26
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,16 @@ public Client(Settings settings)
145145
public async Task Run()
146146
{
147147
var getUuidSuccess = await GetUuid();
148-
if (!getUuidSuccess.Item1)
148+
if (!getUuidSuccess.HasValue)
149149
{
150-
Logger.Error("Failed to get login token. ");
150+
Logger.Error("Failed to get login token.");
151151
}
152152

153-
UUID = getUuidSuccess.Item2;
153+
UUID = getUuidSuccess.UUID;
154154

155155
await QRCodeHelper.ShowQRCode(UUID, _settings.QRCodeOutputType);
156156

157-
Logger.Info("Please use WeChat to scan the QR code .");
157+
Logger.Info("Please use WeChat to scan the QR code.");
158158

159159
var actions = new List<Func<Task<bool>>>()
160160
{
@@ -170,7 +170,7 @@ public async Task Run()
170170
await ThrowIfFailed(Log(action));
171171
}
172172

173-
Logger.Info(@"Start to process messages .");
173+
Logger.Info(@"Start to process messages.");
174174

175175
await Task.WhenAll(CreatePollingTimerAsync(() => true,
176176
TimeSpan.FromSeconds(30),
@@ -187,7 +187,7 @@ await Task.WhenAll(CreatePollingTimerAsync(() => true,
187187
/// <remarks>
188188
/// If failed, uuid will be empty string
189189
/// </remarks>
190-
public async Task<Tuple<bool, string>> GetUuid()
190+
public async Task<Uuid> GetUuid()
191191
{
192192
var url = @"https://login.weixin.qq.com/jslogin";
193193
var @params = new
@@ -204,15 +204,17 @@ public async Task<Tuple<bool, string>> GetUuid()
204204
.GetStringAsync();
205205

206206
var match = Regex.Match(data, @"window.QRLogin.code = (?<code>\d+); window.QRLogin.uuid = ""(?<uuid>\S+?)""");
207-
if (!match.Success)
207+
var code = match.Groups["code"].Value;
208+
209+
if (!match.Success || code != "200")
208210
{
209-
return new Tuple<bool, string>(false, "");
211+
return Uuid.Null;
210212
}
211213

212-
var code = match.Groups["code"].Value;
214+
213215
var uuid = match.Groups["uuid"].Value;
214216

215-
return new Tuple<bool, string>(code == "200", uuid);
217+
return new Uuid(uuid);
216218
}
217219

218220
/// <summary>
@@ -576,7 +578,7 @@ private async Task HandleMessage(SyncResponse response)
576578
foreach (dynamic msg in response.AddMsgList)
577579
{
578580
var msg_type_id = 99;
579-
var user = new Message.User() {id = msg.FromUserName, name = "unknown"};
581+
var user = new Message.User() { id = msg.FromUserName, name = "unknown" };
580582

581583
if (msg.MsgType == 51) //init message
582584
{
@@ -612,7 +614,7 @@ private async Task HandleMessage(SyncResponse response)
612614
{
613615
var contactName = this.GetContactName(user.id);
614616

615-
var contactPreferName = contactName.HasValue? contactName.Value : "unknown";
617+
var contactPreferName = contactName.HasValue ? contactName.Value : "unknown";
616618

617619
if (msg.FromUserName.StartWith("@@")) //# Group
618620
{
@@ -650,32 +652,32 @@ private async Task HandleMessage(SyncResponse response)
650652

651653
var content = this.extract_msg_content(msg_type_id, msg);
652654
var message = new Message()
653-
{
654-
msg_type_id = msg_type_id,
655-
msg_id = msg["MsgId"],
656-
content = content,
657-
to_user_id = msg["ToUserName"],
658-
user = user
659-
};
655+
{
656+
msg_type_id = msg_type_id,
657+
msg_id = msg["MsgId"],
658+
content = content,
659+
to_user_id = msg["ToUserName"],
660+
user = user
661+
};
660662
handle_msg_all(message);
661663
}
662664
}
663665

664666
private ContactName GetContactName(string uid)
665667
{
666-
var normalMember = AccountInfo.NormalMembers.FirstOrDefault(x=>x.Info.UserName == uid);
668+
var normalMember = AccountInfo.NormalMembers.FirstOrDefault(x => x.Info.UserName == uid);
667669
if (normalMember == null)
668670
{
669671
return null;
670672
}
671673

672674
var info = normalMember.Info;
673675
var contactName = new ContactName()
674-
{
675-
RemarkName = info?.RemarkName,
676-
Nickname = info?.NickName,
677-
DisplayName = info?.DisplayName
678-
};
676+
{
677+
RemarkName = info?.RemarkName,
678+
Nickname = info?.NickName,
679+
DisplayName = info?.DisplayName
680+
};
679681
return contactName;
680682
}
681683

@@ -743,7 +745,7 @@ private async Task<Tuple<int, int>> SyncCheck()
743745
synckey = SyncKeyAsString,
744746
_ = NowUnix(),
745747
};
746-
748+
747749
var url = $@"https://{sync_host}/cgi-bin/mmwebwx-bin/synccheck?".SetQueryParams(@params);
748750
try
749751
{

WeChatBot.Net/Helper/HttpClientContainer.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
using System;
2-
using System.Net;
1+
using System.Net;
32
using System.Net.Http;
4-
using Flurl;
53
using Flurl.Http;
64
using Flurl.Http.Configuration;
75

WeChatBot.Net/Model/Uuid.cs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
3+
namespace WeChatBot.Net.Model
4+
{
5+
public class Uuid
6+
{
7+
public bool HasValue => !string.IsNullOrWhiteSpace(UUID);
8+
public string UUID { get; set; }
9+
10+
public Uuid(string uuid = null)
11+
{
12+
UUID = uuid;
13+
}
14+
15+
public static Uuid Null = new Uuid();
16+
}
17+
}

WeChatBot.Net/WeChatBot.Net.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
<Compile Include="Model\MemberType.cs" />
106106
<Compile Include="Model\Synckey.cs" />
107107
<Compile Include="Model\User.cs" />
108+
<Compile Include="Model\Uuid.cs" />
108109
<Compile Include="Properties\AssemblyInfo.cs" />
109110
<Compile Include="Settings.cs" />
110111
<Compile Include="Util\ConsoleWriter.cs" />

0 commit comments

Comments
 (0)