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
|
}
|
}
|