-
Notifications
You must be signed in to change notification settings - Fork 143
/
IMTService.cs
423 lines (391 loc) · 19.1 KB
/
IMTService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
using Flurl.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using hygge_imaotai.Domain;
using hygge_imaotai.Entity;
using hygge_imaotai.Repository;
using hygge_imaotai.UserInterface.Component;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NLog;
using System.Threading;
namespace hygge_imaotai
{
public class IMTService
{
#region Fields
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
#endregion
private const string Salt = "2af72f100c356273d46284f6fd1dfc08";
private static string _mtVersion = "";
private const string AesKey = "qbhajinldepmucsonaaaccgypwuvcjaa";
private const string AesIv = "2018534749963515";
private static string Signature(string content, long timestamp)
{
var text = Salt + content + timestamp;
using var md = MD5.Create();
var hashBytes = md.ComputeHash(Encoding.UTF8.GetBytes(text));
var sb = new StringBuilder();
foreach (var b in hashBytes)
{
sb.Append($"{b:X2}");
}
return sb.ToString().ToLower();
}
/// <summary>
/// 获得i茅台版本号
/// </summary>
/// <returns></returns>
private static async Task<string> GetMtVersion()
{
if (!string.IsNullOrEmpty(_mtVersion)) return _mtVersion;
var htmlSource = await "https://apps.apple.com/cn/app/i%E8%8C%85%E5%8F%B0/id1600482450"
.GetStringAsync();
var pattern = new Regex(@"new__latest__version\"">(.*?)<\/p>", RegexOptions.Singleline);
var matcher = pattern.Match(htmlSource);
var replace = "";
if (!matcher.Success) return replace;
var mtVersion = matcher.Groups[1].Value;
replace = mtVersion.Replace("版本 ", "");
_mtVersion = replace;
return replace;
}
/// <summary>
/// 向手机号发送验证码
/// </summary>
/// <param name="phone"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public async Task<bool> SendCode(string phone)
{
var client = new HttpClient();
var timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
var values = new Dictionary<string, string>
{
{ "mobile", phone },
{ "md5", Signature(phone,timestamp) },
{"timestamp",timestamp+""}
};
var content = new StringContent(JsonConvert.SerializeObject(values), Encoding.UTF8, "application/json");
client.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "*/*");
client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "iOS;16.3;Apple;?unrecognized?");
client.DefaultRequestHeaders.Add("MT-Lat", "28.499562");
client.DefaultRequestHeaders.Add("MT-K", "1675213490331");
client.DefaultRequestHeaders.Add("MT-Lng", "102.182324");
client.DefaultRequestHeaders.Add("Host", "app.moutai519.com.cn");
client.DefaultRequestHeaders.Add("MT-User-Tag", "0");
client.DefaultRequestHeaders.Add("MT-Network-Type", "WIFI");
client.DefaultRequestHeaders.TryAddWithoutValidation("MT-Team-ID", "");
client.DefaultRequestHeaders.Add("MT-Info", "028e7f96f6369cafe1d105579c5b9377");
client.DefaultRequestHeaders.Add("MT-Device-ID", "2F2075D0-B66C-4287-A903-DBFF6358342A");
client.DefaultRequestHeaders.Add("MT-Bundle-ID", "com.moutai.mall");
client.DefaultRequestHeaders.Add("Accept-Language", "en-CN;q=1, zh-Hans-CN;q=0.9");
client.DefaultRequestHeaders.Add("MT-Request-ID", "167560018873318465");
client.DefaultRequestHeaders.Add("MT-APP-Version", await GetMtVersion());
client.DefaultRequestHeaders.Add("MT-R", "clips_OlU6TmFRag5rCXwbNAQ/Tz1SKlN8THcecBp/HGhHdw==");
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Length", "93");
client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate, br");
client.DefaultRequestHeaders.Add("Connection", "keep-alive");
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
var response = await client
.PostAsync("https://app.moutai519.com.cn/xhr/front/user/register/vcode", content);
var responseString = await response.Content.ReadAsStringAsync();
var responseJson = JObject.Parse(responseString);
var code = (string)responseJson["code"];
if (code == "2000") return true;
throw new Exception(responseString);
}
/// <summary>
/// 发送用户登录请求
/// </summary>
/// <param name="phone"></param>
/// <param name="code"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public async Task<bool> Login(string phone, string code)
{
var client = new HttpClient();
var timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
var values = new Dictionary<string, string>
{
{ "mobile", phone },
{ "ydToken", "" },
{"vCode",code},
{"ydLogId",""},
{"md5",Signature(phone + code + "" + "",timestamp)},
{"timestamp",timestamp+""},
{"MT-APP-Version",await GetMtVersion()}
};
var content = new StringContent(JsonConvert.SerializeObject(values), Encoding.UTF8, "application/json");
client.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "*/*");
client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "iOS;16.3;Apple;?unrecognized?");
client.DefaultRequestHeaders.Add("MT-Lat", "28.499562");
client.DefaultRequestHeaders.Add("MT-K", "1675213490331");
client.DefaultRequestHeaders.Add("MT-Lng", "102.182324");
client.DefaultRequestHeaders.Add("Host", "app.moutai519.com.cn");
client.DefaultRequestHeaders.Add("MT-User-Tag", "0");
client.DefaultRequestHeaders.Add("MT-Network-Type", "WIFI");
client.DefaultRequestHeaders.TryAddWithoutValidation("MT-Team-ID", "");
client.DefaultRequestHeaders.Add("MT-Info", "028e7f96f6369cafe1d105579c5b9377");
client.DefaultRequestHeaders.Add("MT-Device-ID", "2F2075D0-B66C-4287-A903-DBFF6358342A");
client.DefaultRequestHeaders.Add("MT-Bundle-ID", "com.moutai.mall");
client.DefaultRequestHeaders.Add("Accept-Language", "en-CN;q=1, zh-Hans-CN;q=0.9");
client.DefaultRequestHeaders.Add("MT-Request-ID", "167560018873318465");
client.DefaultRequestHeaders.Add("MT-APP-Version", await GetMtVersion());
client.DefaultRequestHeaders.Add("MT-R", "clips_OlU6TmFRag5rCXwbNAQ/Tz1SKlN8THcecBp/HGhHdw==");
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Length", "93");
client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate, br");
client.DefaultRequestHeaders.Add("Connection", "keep-alive");
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
var response = await client
.PostAsync("https://app.moutai519.com.cn/xhr/front/user/register/login", content);
var responseString = await response.Content.ReadAsStringAsync();
var responseJson = JObject.Parse(responseString);
var responseCode = (string)responseJson["code"];
if (responseCode != "2000") throw new Exception(responseJson.TryGetValue("message", out var value) ? value.Value<string>() : responseString);
// 存储一下数据
var foundUserEntity = UserManageViewModel.UserList.FirstOrDefault(user => user.Mobile == phone);
if (foundUserEntity != null)
{
await DB.Sqlite.Update<UserEntity>().Set(i => i.UserId, foundUserEntity.UserId)
.Set(i => i.Token, foundUserEntity.Token)
.Set(i => i.ItemCode, foundUserEntity.ItemCode)
.Set(i => i.ProvinceName, foundUserEntity.ProvinceName)
.Set(i => i.CityName, foundUserEntity.CityName)
.Set(i => i.Address, foundUserEntity.Address)
.Set(i => i.Lat, foundUserEntity.Lat)
.Set(i => i.Lng, foundUserEntity.Lng)
.Set(i => i.ShopType, foundUserEntity.ShopType)
.Set(i => i.PushPlusToken, foundUserEntity.PushPlusToken)
.Set(i => i.JsonResult, foundUserEntity.JsonResult)
.Set(i => i.CreateTime, foundUserEntity.CreateTime)
.Set(i => i.ExpireTime, foundUserEntity.ExpireTime)
.Where(i => i.Mobile == foundUserEntity.Mobile).ExecuteAffrowsAsync();
}
else
{
await DB.Sqlite.Insert(new UserEntity(phone, responseJson)).ExecuteAffrowsAsync();
}
return true;
}
/// <summary>
/// 预约方法
/// </summary>
/// <param name="userEntity"></param>
public static async Task Reservation(UserEntity userEntity)
{
var items = userEntity.ItemCode.Split("@");
try
{
foreach (var item in items)
{
var shopId = await ShopRepository.GetShopId(userEntity.ShopType, item, userEntity.ProvinceName, userEntity.CityName, userEntity.Lat, userEntity.Lng);
if (!string.IsNullOrEmpty(shopId))
{
await Reservation(userEntity, item, shopId);
}
}
}
catch (Exception ex)
{
await DB.Sqlite.Insert<LogEntity>(new LogEntity()
{
CreateTime = DateTime.Now,
MobilePhone = userEntity.Mobile,
Content = $"[userId]:{userEntity.UserId}",
Response = ex.Message,
Status = "异常"
}).ExecuteAffrowsAsync();
}
}
public static async Task Reservation(UserEntity user, string itemId, string shopId)
{
var info = new Dictionary<string, object>
{
{ "itemId", itemId },
{ "count", 1 }
};
var values = new Dictionary<string, object>
{
{ "itemInfoList", new List<Dictionary<string, object>>() { info } },
{ "sessionId",await GetCurrentSessionId() },
{"shopId",shopId},
{"userId",user.UserId + ""}
};
values.Add("actParam", EncryptAES_CBC(JsonConvert.SerializeObject(values).Replace("\\\"", "\"")));
var requestBody = JsonConvert.SerializeObject(values);
var response = await "https://app.moutai519.com.cn/xhr/front/mall/reservation/add"
.WithHeader("userId", user.UserId + "")
.WithHeader("Content-Type", "application/json")
.WithHeader("Connection", "keep-alive")
.WithHeader("MT-Bundle-ID", "com.moutai.mall")
.WithHeader("MT-R", "clips_OlU6TmFRag5rCXwbNAQ/Tz1SKlN8THcecBp/HGhHdw==")
.WithHeader("Accept-Encoding", "gzip, deflate, br")
.WithHeader("Accept-Language", "en-CN;q=1, zh-Hans-CN;q=0.9")
.WithHeader("MT-Lat", user.Lat)
.WithHeader("MT-K", "1675213490331")
.WithHeader("MT-Lng", user.Lng)
.WithHeader("Host", "app.moutai519.com.cn")
.WithHeader("MT-User-Tag", "0")
.WithHeader("Accept", "*/*")
.WithHeader("MT-Network-Type", "WIFI")
.WithHeader("MT-Token", user.Token)
.WithHeader("MT-Team-ID", "")
.WithHeader("MT-Info", "028e7f96f6369cafe1d105579c5b9377")
.WithHeader("MT-Device-ID", "2F2075D0-B66C-4287-A903-DBFF6358342A")
.WithHeader("MT-Request-ID", "167560018873318465")
.WithHeader("MT-APP-Version", await GetMtVersion())
.WithHeader("User-Agent", "iOS;16.3;Apple;?unrecognized?")
.AllowAnyHttpStatus()
.PostStringAsync(requestBody);
if (response.StatusCode is 200 or 480)
{
var responseString = await response.GetStringAsync();
var responseJson = JObject.Parse(responseString);
await DB.Sqlite.Insert<LogEntity>(new LogEntity()
{
CreateTime = DateTime.Now,
MobilePhone = user.Mobile,
Content = $"[userId]:{user.UserId} [shopId]:{itemId}",
Response = responseString,
Status = responseJson["code"].Value<int>() == 2000 ? "预约成功" : "预约失败"
}).ExecuteAffrowsAsync();
}
else
{
var logEntity = new LogEntity()
{
CreateTime = DateTime.Now,
MobilePhone = user.Mobile,
Content = $"[userId]:{user.UserId} [shopId]:{itemId}",
Response = $"本次抢购会话已过期,请手动刷新一下商品列表和店铺列表后重试",
Status = "预约失败"
};
if (response.StatusCode != 404)
{
logEntity.Response = await response.GetStringAsync();
}
await DB.Sqlite.Insert<LogEntity>(logEntity).ExecuteAffrowsAsync();
}
}
/// <summary>
/// AES CBC加密函数
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
private static string EncryptAES_CBC(string input)
{
var key = Encoding.UTF8.GetBytes(AesKey);
var iv = Encoding.UTF8.GetBytes(AesIv);
using var aes = Aes.Create();
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
aes.Key = key;
aes.IV = iv;
var encryptor = aes.CreateEncryptor();
var inputBytes = Encoding.UTF8.GetBytes(input);
var encryptedBytes = encryptor.TransformFinalBlock(inputBytes, 0, inputBytes.Length);
var base64String = Convert.ToBase64String(encryptedBytes);
return base64String;
}
/// <summary>
/// 获取当前会话的专属ID,并更新商品列表数据
/// </summary>
/// <returns></returns>
public static async Task<string> GetCurrentSessionId()
{
var mtSessionId = App.MtSessionId;
if (!string.IsNullOrEmpty(mtSessionId)) return mtSessionId;
var midNight = DateTime.Now.Date;
var epochStart = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.FromHours(8));
var timeSpan = midNight.AddHours(-8) - epochStart;
var milliseconds = (long)timeSpan.TotalMilliseconds;
var responseStr = await ("https://static.moutai519.com.cn/mt-backend/xhr/front/mall/index/session/get/" +
milliseconds)
.GetStringAsync();
var jObject = JObject.Parse(responseStr);
if (jObject.GetValue("code").Value<int>() == 2000)
{
var dataJObject = jObject["data"];
App.MtSessionId = dataJObject["sessionId"].Value<string>();
var itemList = (JArray)dataJObject["itemList"];
AppointProjectViewModel.ProductList.Clear();
foreach (var itemElement in itemList)
{
AppointProjectViewModel.ProductList.Add(new ProductEntity(itemElement["itemCode"].Value<string>(),
itemElement["title"].Value<string>(),
itemElement["content"].Value<string>(),
itemElement["picture"].Value<string>(), DateTime.Now));
}
App.WriteCache("productList.json", JsonConvert.SerializeObject(AppointProjectViewModel.ProductList));
App.WriteCache("mtSessionId.txt", App.MtSessionId);
}
return mtSessionId;
}
/// <summary>
/// 批量预约
/// </summary>
public static void ReservationBatch()
{
var users = DB.Sqlite.Select<UserEntity>().Where(i =>
i.ExpireTime > DateTime.Now && !string.IsNullOrEmpty(i.Lat) && !string.IsNullOrEmpty(i.Lng) &&
!string.IsNullOrEmpty(i.ShopType + "") && !string.IsNullOrEmpty(i.ItemCode))
.ToList();
foreach (var userEntity in users)
{
try
{
Logger.Info($"「开始预约用户」{userEntity.Mobile}");
Reservation(userEntity);
}
catch (Exception e)
{
Logger.Error($"用户{userEntity.Mobile}预约产生异常,错误原因:{e.Message}");
DB.Sqlite.Insert(new LogEntity()
{
CreateTime = DateTime.Now,
MobilePhone = userEntity.Mobile,
Content = $"[userId]:{userEntity.UserId}",
Response = e.Message,
Status = "异常"
}).ExecuteAffrows();
}
}
}
public static async void RefreshAll()
{
_mtVersion = string.Empty;
await GetMtVersion();
await RefreshShop();
App.MtSessionId = string.Empty;
await GetCurrentSessionId();
}
public static async Task RefreshShop()
{
ShopListViewModel.StoreList.Clear();
await DB.Sqlite.Delete<ShopEntity>().ExecuteAffrowsAsync();
var responseStr = await "https://static.moutai519.com.cn/mt-backend/xhr/front/mall/resource/get"
.GetStringAsync();
var jObject = JObject.Parse(responseStr);
var shopsUrl = jObject.GetValue("data").Value<JObject>().GetValue("mtshops_pc").Value<JObject>().GetValue("url").Value<string>();
var shopInnerJson = await shopsUrl.GetStringAsync();
var shopInnerJObject = JObject.Parse(shopInnerJson);
var task = Task.Run(() =>
{
foreach (var property in shopInnerJObject.Properties())
{
var shopId = property.Name;
var nestedObject = (JObject)property.Value;
DB.Sqlite.Insert(new ShopEntity(shopId, nestedObject)).ExecuteAffrows();
}
});
await task;
}
}
}