chr
2024-08-21 9ee7d3bd9c58a204b1efe38e6be61155bbb15c16
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
using PdmAlert.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Reflection;
 
namespace PdmAlert
{
    public class LoginUser
    {
        public static LoginUser CurrentUser { get; private set; }
 
        private static readonly Encryptor encryptor = new Encryptor("12345678123456781234567812345678",
            "1234567812345678");
 
        public string id { get; set; }
        public string realName { get; set; }
        public string username { get; set; }
        public string password { get; set; }
        public string appId { get; set; }
        public string pluginVersion { get; set; }
        public bool rememberMe { get; set; }
 
        public static bool TryAutoLogin()
        {
            string fileContent = null;
            if (LoadStrFromBin(ref fileContent))
            {
                try
                {
                    string jsonStr = encryptor.Decrypt(fileContent);
                    Dictionary<string, string> user = JsonUtil.Deserialize<Dictionary<string, string>>(jsonStr);
                    return Login(user["username"], user["password"], true);
                }
                catch (Exception ex)
                {
                    RemoveCacheBin();
                    return false;
                }
 
            }
            return false;
        }
 
        public static bool Login(string username, string password, bool rememberMe)
        {
 
            using (HttpClient client = new HttpClient
            {
                BaseAddress = new Uri("http://localhost:8888/pdm-web/"),
            })
            {
                try
                {
                    Result<LoginUser> res = client.PostSyncAction<LoginUser>(new LoginUser
                    {
                        username = username,
                        password = password,
                        appId = "sockettool",
                        pluginVersion = "0.0.0.1"
 
                    }, "openApi/wpf/login");
                    CurrentUser = res.HandleResult();
                    CurrentUser.password = password;
                    CurrentUser.rememberMe = rememberMe;
                    if (rememberMe)
                    {
                        string userInfo = JsonUtil.Stringfiy(CurrentUser);
                        SaveStrToBin(encryptor.Encrypt(userInfo));
                    }
                    return true;
                }
                catch (Exception ex)
                {
                    RemoveCacheBin();
                }
            }
            return false;
        }
 
 
        public static void SaveStrToBin(string content)
        {
            string filePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().FullName), "bin");
            byte[] bytes = Convert.FromBase64String(content);
            using (Stream stream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
            using (BinaryWriter writer = new BinaryWriter(stream))
            {
                writer.Write(bytes);
            }
        }
 
        public static bool LoadStrFromBin(ref string content)
        {
            string filePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().FullName), "bin");
            if (!File.Exists(filePath))
            {
                return false;
            }
            using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            using (BinaryReader reader = new BinaryReader(stream))
            {
                byte[] bytes = new byte[stream.Length];
                reader.Read(bytes, 0, bytes.Length);
                content = Convert.ToBase64String(bytes);
                return true;
            }
        }
 
        public static void RemoveCacheBin()
        {
            string filePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().FullName), "bin");
            File.Delete(filePath);
        }
    }
}