智云3.0授权文件生成器新版
liusuyi
2026-08-07 0ae10765f86b83cd6d9f7db818266c62521800cd
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
using Authorization.Tools;
using log4cxx;
using Microsoft.Win32;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Media;
using System.Windows.Threading;
using ZXing;
 
namespace Authorization
{
    public partial class MainWindow : Window
    {
        private DispatcherTimer _clockTimer;
 
        public MainWindow()
        {
            InitializeComponent();
            StartClock();
        }
 
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            dateTimePicker.SelectedDate = DateTime.Now.AddDays(30);
            timeTextBox.Text = "00:00:00";
 
            HTTPAdress.Text = Properties.Settings.Default.ServerIP;
            HTTPPort.Text = Properties.Settings.Default.ServerPort;
        }
 
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Properties.Settings.Default.ServerIP = HTTPAdress.Text;
            Properties.Settings.Default.ServerPort = HTTPPort.Text;
            Properties.Settings.Default.Save();
        }
 
        private void StartClock()
        {
            _clockTimer = new DispatcherTimer();
            _clockTimer.Interval = TimeSpan.FromSeconds(1);
            _clockTimer.Tick += (s, e) =>
            {
                nowTimeLb.Text = DateTime.Now.ToString("yyyy-MM-dd   HH:mm:ss");
            };
            _clockTimer.Start();
        }
 
        #region DateTime helpers
 
        private DateTime GetSelectedDateTime()
        {
            DateTime date = dateTimePicker.SelectedDate ?? DateTime.Now;
            string timeStr = (timeTextBox.Text ?? "00:00:00").Trim();
            TimeSpan ts;
            if (TimeSpan.TryParse(timeStr, out ts))
            {
                date = date.Date + ts;
            }
            return date;
        }
 
        private void SetSelectedDateTime(DateTime value)
        {
            dateTimePicker.SelectedDate = value.Date;
            timeTextBox.Text = value.ToString("HH:mm:ss");
        }
 
        #endregion
 
        #region Generate
 
        private void GenerateBtn_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtAdress.Text))
            {
                MessageBox.Show("现场地点不能为空!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Stop);
                txtAdress.Focus();
                return;
            }
 
            if (string.IsNullOrWhiteSpace(HTTPAdress.Text))
            {
                MessageBox.Show("HTTP请求地址不能为空!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Stop);
                HTTPAdress.Focus();
                return;
            }
 
            if (!IsIP(HTTPAdress.Text))
            {
                MessageBox.Show("HTTP填写不正确,非法IP!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Stop);
                HTTPAdress.Focus();
                return;
            }
 
            if (string.IsNullOrWhiteSpace(HTTPPort.Text))
            {
                MessageBox.Show("HTTP请求端口不能为空!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Stop);
                HTTPPort.Focus();
                return;
            }
 
            if (string.IsNullOrWhiteSpace(txtJQM.Text))
            {
                MessageBox.Show("机器码不能为空!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Stop);
                txtJQM.Focus();
                return;
            }
 
            try
            {
                Root root = new Root();
                root.systemAuthorizeDate = GetSelectedDateTime().ToString("yyyyMMddHHmmss");
                root.machineCode = txtJQM.Text;
                int droneNum;
                int.TryParse(txtDroneNum.Text, out droneNum);
                root.droneNum = droneNum;
                int dockNum;
                int.TryParse(txtDockNum.Text, out dockNum);
                root.dockNum = dockNum;
 
                string json = JsonConvert.SerializeObject(root);
                string postUrl = "http://" + HTTPAdress.Text + ":" + HTTPPort.Text + "/Authorization/getLicense";
                string result = HttpPost(postUrl, json);
 
                if (!string.IsNullOrEmpty(result))
                {
                    LogHelper.Info("生成授权:" + result);
                    Common.SaveToLicense(result, txtAdress.Text, "License");
 
                    var saveDialog = new SaveFileDialog();
                    saveDialog.Title = "保存授权文件";
                    saveDialog.Filter = "cpy(*.cpy)|*.cpy";
                    saveDialog.FileName = "license.cpy";
                    saveDialog.DefaultExt = "cpy";
                    if (saveDialog.ShowDialog() == true)
                    {
                        // 必须用无 BOM 的 UTF-8,否则服务端 FileReader 会把 BOM 读成 \uFEFF,
                        // 导致 base64 解码失败,验证时返回 "License mismatch"
                        File.WriteAllText(saveDialog.FileName, result, new UTF8Encoding(false));
                        MessageBox.Show("授权文件已保存到:" + saveDialog.FileName, "系统提示",
                            MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                    else
                    {
                        MessageBox.Show("生成成功!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                }
                else
                {
                    MessageBox.Show("生成失败,请重试!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("生成授权异常:" + ex.Message, "系统提示", MessageBoxButton.OK, MessageBoxImage.Error);
                LogHelper.Info("生成授权异常:" + ex.Message);
            }
        }
 
        #endregion
 
        #region Decrypt
 
        private void ParsLicenseBtn_Click(object sender, RoutedEventArgs e)
        {
            string filePath = txtPath.Text;
            if (string.IsNullOrWhiteSpace(filePath))
            {
                MessageBox.Show("License目录不能为空!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                txtPath.Focus();
                return;
            }
 
            if (string.IsNullOrWhiteSpace(JQMTb.Text))
            {
                MessageBox.Show("机器码不能为空!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                JQMTb.Focus();
                return;
            }
 
            if (string.IsNullOrWhiteSpace(HTTPAdress.Text) || string.IsNullOrWhiteSpace(HTTPPort.Text))
            {
                MessageBox.Show("请先填写服务器配置!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
 
            string postUrl = "http://" + HTTPAdress.Text + ":" + HTTPPort.Text + "/Authorization/parsLicense";
            try
            {
                ParsLicenseInfo info = new ParsLicenseInfo();
                info.machineCode = JQMTb.Text;
                info.EncryptedStr = FileToString(filePath);
 
                string json = JsonConvert.SerializeObject(info);
                string result = HttpPost(postUrl, json);
 
                if (!string.IsNullOrEmpty(result))
                {
                    Root root = (Root)JsonConvert.DeserializeObject(result, typeof(Root));
                    string authorizeDate = root.systemAuthorizeDate.Substring(0, 4) + "/"
                        + root.systemAuthorizeDate.Substring(4, 2) + "/"
                        + root.systemAuthorizeDate.Substring(6, 2) + " "
                        + root.systemAuthorizeDate.Substring(8, 2) + ":"
                        + root.systemAuthorizeDate.Substring(10, 2) + ":"
                        + root.systemAuthorizeDate.Substring(12, 2);
 
                    DateTime re;
                    if (DateTime.TryParse(authorizeDate, out re))
                    {
                        dateTimePicker.SelectedDate = re.Date;
                        timeTextBox.Text = re.ToString("HH:mm:ss");
                    }
                    dateTimePicker.Foreground = new SolidColorBrush(Colors.Red);
                    timeTextBox.Foreground = new SolidColorBrush(Colors.Red);
 
                    txtDroneNum.Text = root.dockNum.ToString();
                    txtDroneNum.Foreground = new SolidColorBrush(Colors.Red);
 
                    txtDockNum.Text = root.droneNum.ToString();
                    txtDockNum.Foreground = new SolidColorBrush(Colors.Red);
 
                    MessageBox.Show("解密成功!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Information);
                }
                else
                {
                    MessageBox.Show("解密失败,请检查机器码或者授权文件!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("解密异常:" + ex.Message);
                MessageBox.Show("解密异常:" + ex.Message, "系统提示", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
 
        #endregion
 
        #region File Dialogs
 
        private void ViewPathBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.Title = "请选择文件";
                openFileDialog.Filter = "cpy(*.cpy)|*.cpy";
                openFileDialog.FilterIndex = 1;
                openFileDialog.RestoreDirectory = true;
                openFileDialog.Multiselect = false;
 
                if (openFileDialog.ShowDialog() == true)
                {
                    txtPath.Text = openFileDialog.FileName;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("加载授权文件异常:" + ex.Message);
            }
        }
 
        private void OpenCodeBtn_Click(object sender, RoutedEventArgs e)
        {
            DecodeQRCode(txtJQM);
        }
 
        private void OpenCode1Btn_Click(object sender, RoutedEventArgs e)
        {
            DecodeQRCode(JQMTb);
        }
 
        private void DecodeQRCode(System.Windows.Controls.TextBox targetBox)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "请选择二维码图片";
            openFileDialog.Filter = "图片文件(*.jpg;*.png)|*.jpg;*.png";
            openFileDialog.FilterIndex = 1;
            openFileDialog.RestoreDirectory = true;
            openFileDialog.Multiselect = false;
 
            if (openFileDialog.ShowDialog() == true)
            {
                try
                {
                    string localFilePath = openFileDialog.FileName;
                    using (var image = (System.Drawing.Bitmap)System.Drawing.Image.FromFile(localFilePath))
                    {
                        BarcodeReader reader = new BarcodeReader();
                        Result result = reader.Decode(image);
                        if (result == null)
                        {
                            MessageBox.Show("无法识别二维码", "系统提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                            return;
                        }
                        targetBox.Text = result.Text;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("读取二维码异常:" + ex.Message, "系统提示", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }
 
        #endregion
 
        #region Utility Methods
 
        public static bool IsIP(string ip)
        {
            return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
        }
 
        public string HttpPost(string url, string values)
        {
            byte[] postData = Encoding.UTF8.GetBytes(values);
            WebClient webClient = new WebClient();
            webClient.Headers.Add("Content-Type", "text/xml");
            byte[] responseData = webClient.UploadData(url, "POST", postData);
            return Encoding.UTF8.GetString(responseData);
        }
 
        public string HttpGet(string url)
        {
            string result = string.Empty;
            try
            {
                HttpWebRequest wbRequest = (HttpWebRequest)WebRequest.Create(url);
                wbRequest.Method = "GET";
                HttpWebResponse wbResponse = (HttpWebResponse)wbRequest.GetResponse();
                using (Stream responseStream = wbResponse.GetResponseStream())
                {
                    using (StreamReader sReader = new StreamReader(responseStream))
                    {
                        result = sReader.ReadToEnd();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return result;
        }
 
        public static string FileToString(string filePath)
        {
            string strData = "";
            try
            {
                string line;
                using (StreamReader sr = new StreamReader(filePath))
                {
                    while ((line = sr.ReadLine()) != null)
                    {
                        strData = line;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
            return strData;
        }
 
        public static string SerializerJson<T>(T obj)
        {
            DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(T));
            MemoryStream ms = new MemoryStream();
            jsonSerializer.WriteObject(ms, obj);
            return Encoding.UTF8.GetString(ms.ToArray());
        }
 
        #endregion
    }
}