CNAS取数仪器端升级
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

205 Zeilen
6.8KB

  1. using CnasSynchronousCommon;
  2. using CnasSynchronusClient;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Security.Cryptography;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Navigation;
  18. using System.Windows.Shapes;
  19. namespace CNAS_Authorize
  20. {
  21. /// <summary>
  22. /// MainWindow.xaml 的交互逻辑
  23. /// </summary>
  24. public partial class MainWindow : Window
  25. {
  26. private MachineAuth m_machines;
  27. public MainWindow()
  28. {
  29. InitializeComponent();
  30. }
  31. private void Window_Loaded(object sender, RoutedEventArgs e)
  32. {
  33. this.LoadData();
  34. }
  35. private void LoadData()
  36. {
  37. m_machines = new MachineAuth();
  38. m_machines.Load();
  39. dataGrid.ItemsSource = m_machines.Page;
  40. this.DataContext = m_machines;
  41. }
  42. private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
  43. {
  44. if (this.Width < 500) return;
  45. dataGrid.Width = this.Width - 48;
  46. dataGrid.Height = centerPanel.Height;
  47. //设置列宽
  48. double colWidth = this.Width - 60;// centerPanel.Width;
  49. for (int i = 1; i < dataGrid.Columns.Count; i++)
  50. {
  51. colWidth = colWidth - dataGrid.Columns[i].Width.Value;
  52. }
  53. if (colWidth > 0)
  54. dataGrid.Columns[0].Width = colWidth;
  55. edtMachine.Width = this.Width - lbMachine.Width - btnQuery.Width - 48;
  56. edtFactory.Width = edtMachine.Width;
  57. }
  58. private void refreshBind()
  59. {
  60. this.DataContext = null;
  61. this.DataContext = m_machines;
  62. }
  63. private void PrevPage_Click(object sender, RoutedEventArgs e)
  64. {
  65. bool ok;
  66. List<Machine> page = m_machines.PrevPage(out ok);
  67. if (ok)
  68. {
  69. dataGrid.ItemsSource = page;
  70. refreshBind();
  71. }
  72. }
  73. private void NextPage_Click(object sender, RoutedEventArgs e)
  74. {
  75. bool ok;
  76. List<Machine> page = m_machines.NextPage(out ok);
  77. if (ok)
  78. {
  79. dataGrid.ItemsSource = page;
  80. refreshBind();
  81. }
  82. }
  83. private void Query_Click(object sender, RoutedEventArgs e)
  84. {
  85. m_machines.ChangeFilter(edtMachine.Text.Trim(), edtFactory.Text.Trim());
  86. dataGrid.ItemsSource = m_machines.Page;
  87. refreshBind();
  88. }
  89. private void ConfirmImport_Click(object sender, RoutedEventArgs e)
  90. {
  91. //加载CNAS数据库配置
  92. var targetDataBase = FileOperation.GetLocalCnasDB();
  93. if (!CnasDataOperationFact.CnasDataOperation().TestConnect(targetDataBase))
  94. {
  95. MessageBox.Show("CNAS数据库无法正常连接...", "提示");
  96. return;
  97. }
  98. var existData = CnasDataOperationFact.CnasDataOperation().GetTableData(targetDataBase, $"select * from macaddress where MAC_ADDRESS='{edtMachine.Text.Trim()}'");
  99. if(existData!=null&& existData.Rows.Count > 0)
  100. {
  101. MessageBox.Show("已经导入!");
  102. return;
  103. }
  104. StringBuilder sb = new StringBuilder();
  105. sb.AppendFormat($"insert into macaddress(MAC_ADDRESS,PLANTCODE,CREATETIME,FACILITY_STYLE) VALUES('{edtMachine.Text.Trim()}','{edtFactory.Text.Trim()}',now(),'999')");
  106. //1.加载数据
  107. var flag = CnasDataOperationFact.CnasDataOperation().InsertTableData(targetDataBase, sb.ToString());
  108. if (flag)
  109. {
  110. AppLog.ServiceInfo("导入成功");
  111. MessageBox.Show("导入成功...", "提示");
  112. this.LoadData();
  113. }
  114. else
  115. {
  116. MessageBox.Show("导入失败...", "提示");
  117. }
  118. }
  119. private void DownloadActivationCode_Click(object sender, RoutedEventArgs e)
  120. {
  121. var button = sender as Button;
  122. var machine = button.DataContext as Machine;
  123. if (machine != null)
  124. {
  125. try
  126. {
  127. // 生成激活码
  128. string activationCode = GenerateActivationCode(machine.MachineCode);
  129. // 创建保存文件对话框
  130. var saveFileDialog = new Microsoft.Win32.SaveFileDialog
  131. {
  132. FileName = $"激活码_{machine.MachineCode}_{DateTime.Now:yyyyMMdd}",
  133. DefaultExt = ".txt",
  134. Filter = "文本文件|*.txt"
  135. };
  136. if (saveFileDialog.ShowDialog() == true)
  137. {
  138. // 将激活码写入文件
  139. System.IO.File.WriteAllText(saveFileDialog.FileName, activationCode);
  140. MessageBox.Show("激活码已成功保存!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
  141. // 更新状态
  142. machine.IsGenerated = true;
  143. refreshBind();
  144. }
  145. }
  146. catch (Exception ex)
  147. {
  148. MessageBox.Show($"下载激活码失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  149. }
  150. }
  151. }
  152. private string GenerateActivationCode(string machineCode)
  153. {
  154. // 这里实现激活码生成算法
  155. // 示例:简单的加密算法,实际应用中应该使用更安全的加密方式
  156. using (var md5 = System.Security.Cryptography.MD5.Create())
  157. {
  158. // 加入时间戳使得生成的激活码具有时效性
  159. string input = machineCode + DateTime.Now.ToString("yyyyMMdd");
  160. byte[] inputBytes = Encoding.UTF8.GetBytes(input);
  161. byte[] hashBytes = md5.ComputeHash(inputBytes);
  162. // 转换为16进制字符串
  163. StringBuilder sb = new StringBuilder();
  164. for (int i = 0; i < hashBytes.Length; i++)
  165. {
  166. sb.Append(hashBytes[i].ToString("X2"));
  167. // 每4个字符添加一个分隔符,提高可读性
  168. if ((i + 1) % 4 == 0 && i != hashBytes.Length - 1)
  169. {
  170. sb.Append("-");
  171. }
  172. }
  173. return sb.ToString();
  174. }
  175. }
  176. }
  177. }