|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- using CnasSynchronousCommon;
- using CnasSynchronusClient;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Security.Cryptography;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
-
- namespace CNAS_Authorize
- {
- /// <summary>
- /// MainWindow.xaml 的交互逻辑
- /// </summary>
- public partial class MainWindow : Window
- {
-
- private MachineAuth m_machines;
-
- public MainWindow()
- {
- InitializeComponent();
-
- }
-
- private void Window_Loaded(object sender, RoutedEventArgs e)
- {
- this.LoadData();
- }
-
- private void LoadData()
- {
- m_machines = new MachineAuth();
- m_machines.Load();
- dataGrid.ItemsSource = m_machines.Page;
- this.DataContext = m_machines;
- }
-
- private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
- {
- if (this.Width < 500) return;
- dataGrid.Width = this.Width - 48;
- dataGrid.Height = centerPanel.Height;
-
- //设置列宽
- double colWidth = this.Width - 60;// centerPanel.Width;
- for (int i = 1; i < dataGrid.Columns.Count; i++)
- {
- colWidth = colWidth - dataGrid.Columns[i].Width.Value;
- }
- if (colWidth > 0)
- dataGrid.Columns[0].Width = colWidth;
-
- edtMachine.Width = this.Width - lbMachine.Width - btnQuery.Width - 48;
- edtFactory.Width = edtMachine.Width;
-
- }
-
- private void refreshBind()
- {
- this.DataContext = null;
- this.DataContext = m_machines;
- }
-
- private void PrevPage_Click(object sender, RoutedEventArgs e)
- {
- bool ok;
- List<Machine> page = m_machines.PrevPage(out ok);
- if (ok)
- {
- dataGrid.ItemsSource = page;
- refreshBind();
- }
-
-
- }
-
- private void NextPage_Click(object sender, RoutedEventArgs e)
- {
- bool ok;
- List<Machine> page = m_machines.NextPage(out ok);
- if (ok)
- {
- dataGrid.ItemsSource = page;
- refreshBind();
- }
-
- }
-
- private void Query_Click(object sender, RoutedEventArgs e)
- {
- m_machines.ChangeFilter(edtMachine.Text.Trim(), edtFactory.Text.Trim());
- dataGrid.ItemsSource = m_machines.Page;
- refreshBind();
- }
-
- private void ConfirmImport_Click(object sender, RoutedEventArgs e)
- {
-
- //加载CNAS数据库配置
- var targetDataBase = FileOperation.GetLocalCnasDB();
- if (!CnasDataOperationFact.CnasDataOperation().TestConnect(targetDataBase))
- {
- MessageBox.Show("CNAS数据库无法正常连接...", "提示");
- return;
- }
-
- var existData = CnasDataOperationFact.CnasDataOperation().GetTableData(targetDataBase, $"select * from macaddress where MAC_ADDRESS='{edtMachine.Text.Trim()}'");
- if(existData!=null&& existData.Rows.Count > 0)
- {
- MessageBox.Show("已经导入!");
- return;
- }
- StringBuilder sb = new StringBuilder();
- sb.AppendFormat($"insert into macaddress(MAC_ADDRESS,PLANTCODE,CREATETIME,FACILITY_STYLE) VALUES('{edtMachine.Text.Trim()}','{edtFactory.Text.Trim()}',now(),'999')");
-
- //1.加载数据
- var flag = CnasDataOperationFact.CnasDataOperation().InsertTableData(targetDataBase, sb.ToString());
- if (flag)
- {
- AppLog.ServiceInfo("导入成功");
- MessageBox.Show("导入成功...", "提示");
- this.LoadData();
- }
- else
- {
- MessageBox.Show("导入失败...", "提示");
- }
- }
-
- private void DownloadActivationCode_Click(object sender, RoutedEventArgs e)
- {
- var button = sender as Button;
- var machine = button.DataContext as Machine;
- if (machine != null)
- {
- try
- {
- // 生成激活码
- string activationCode = GenerateActivationCode(machine.MachineCode);
-
- // 创建保存文件对话框
- var saveFileDialog = new Microsoft.Win32.SaveFileDialog
- {
- FileName = $"激活码_{machine.MachineCode}_{DateTime.Now:yyyyMMdd}",
- DefaultExt = ".txt",
- Filter = "文本文件|*.txt"
- };
-
- if (saveFileDialog.ShowDialog() == true)
- {
- // 将激活码写入文件
- System.IO.File.WriteAllText(saveFileDialog.FileName, activationCode);
- MessageBox.Show("激活码已成功保存!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
-
- // 更新状态
- machine.IsGenerated = true;
- refreshBind();
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show($"下载激活码失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
- }
- }
- }
-
- private string GenerateActivationCode(string machineCode)
- {
- // 这里实现激活码生成算法
- // 示例:简单的加密算法,实际应用中应该使用更安全的加密方式
- using (var md5 = System.Security.Cryptography.MD5.Create())
- {
- // 加入时间戳使得生成的激活码具有时效性
- string input = machineCode + DateTime.Now.ToString("yyyyMMdd");
- byte[] inputBytes = Encoding.UTF8.GetBytes(input);
- byte[] hashBytes = md5.ComputeHash(inputBytes);
-
- // 转换为16进制字符串
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < hashBytes.Length; i++)
- {
- sb.Append(hashBytes[i].ToString("X2"));
- // 每4个字符添加一个分隔符,提高可读性
- if ((i + 1) % 4 == 0 && i != hashBytes.Length - 1)
- {
- sb.Append("-");
- }
- }
- return sb.ToString();
- }
- }
- }
- }
|