-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScanQRCode.xaml.cs
79 lines (65 loc) · 2.59 KB
/
ScanQRCode.xaml.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
using System;
using System.Collections.Generic;
using System.Timers;
using System.Windows;
namespace LiveBot
{
/// <summary>
/// ScanQRCode.xaml 的交互逻辑
/// </summary>
public partial class ScanQRCode : Window
{
private string authKey;
private string cookie;
private Timer listen;
private UserManager userManager = UserManager.Instance;
public ScanQRCode()
{
WindowStartupLocation = WindowStartupLocation.CenterScreen;
InitializeComponent();
GenQRcode();
}
private void GenQRcode()
{
if (userManager.LoginInfo.ContainsKey("SESSDATA"))
{
new LiveToast().ToastText("你已经登陆过了哦");
Close();
}
try
{
Newtonsoft.Json.Linq.JObject data = Utilities.ConvertJsonString(Utilities.HttpGet("http://passport.bilibili.com/qrcode/getLoginUrl"));
QrCode.Source = Utilities.Qrencode(data["data"]["url"].ToString());
authKey = data["data"]["oauthKey"].ToString();
listen = new Timer();
listen.Elapsed += WaitLogin;
listen.AutoReset = true;
listen.Interval = 500;
listen.Start();
}
catch { new LiveToast().ToastText("获取二维码失败"); }
}
private void WaitLogin(object sender, ElapsedEventArgs e)
{
Dictionary<string, string> postData = new Dictionary<string, string>();
postData.Add("oauthKey", authKey);
Newtonsoft.Json.Linq.JObject res = Utilities.ConvertJsonString(Utilities.HttpPost("http://passport.bilibili.com/qrcode/getLoginInfo", postData, new Dictionary<string, string>()));
if (res["status"].ToString() == "True")
{
SaveLoginInfo(res);
}
}
private void SaveLoginInfo(Newtonsoft.Json.Linq.JObject res)
{
cookie = res["data"]["url"].ToString();
string queryStr = new Uri(cookie).Query.Substring(1);
userManager.LoginInfo = Utilities.QueryToDic(queryStr);
userManager.SaveSetting("Config", "Cookie", queryStr);
//保存时间戳
//userManager.SaveSetting("Config", "ExpiredTime", (Utilities.DataTimeToStamp(DateTime.Now) + long.Parse(userManager.LoginInfo["Expires"])).ToString());
new LiveToast().ToastText("登录成功~");
listen.Stop();
window.Dispatcher.Invoke(() => { Close(); });
}
}
}