CNAS取数仪器端升级
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

190 lines
6.6KB

  1. using CnasSynchronusClient;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. namespace CNAS_Authorize
  18. {
  19. /// <summary>
  20. /// MainWindow.xaml 的交互逻辑
  21. /// </summary>
  22. public partial class MainWindow : Window
  23. {
  24. private MachineAuth m_machines;
  25. public MainWindow()
  26. {
  27. InitializeComponent();
  28. }
  29. private void Window_Loaded(object sender, RoutedEventArgs e)
  30. {
  31. m_machines = new MachineAuth();
  32. m_machines.Load();
  33. dataGrid.ItemsSource = m_machines.Page;
  34. this.DataContext = m_machines;
  35. }
  36. private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
  37. {
  38. if (this.Width < 500) return;
  39. dataGrid.Width = this.Width - 48;
  40. dataGrid.Height = centerPanel.Height;
  41. //设置列宽
  42. double colWidth = this.Width - 60;// centerPanel.Width;
  43. for (int i = 1; i < dataGrid.Columns.Count; i++)
  44. {
  45. colWidth = colWidth - dataGrid.Columns[i].Width.Value;
  46. }
  47. if (colWidth > 0)
  48. dataGrid.Columns[0].Width = colWidth;
  49. edtMachine.Width = this.Width - lbMachine.Width - btnQuery.Width - 48;
  50. edtFactory.Width = edtMachine.Width;
  51. }
  52. private void refreshBind()
  53. {
  54. this.DataContext = null;
  55. this.DataContext = m_machines;
  56. }
  57. private void PrevPage_Click(object sender, RoutedEventArgs e)
  58. {
  59. bool ok;
  60. List<Machine> page = m_machines.PrevPage(out ok);
  61. if (ok)
  62. {
  63. dataGrid.ItemsSource = page;
  64. refreshBind();
  65. }
  66. }
  67. private void NextPage_Click(object sender, RoutedEventArgs e)
  68. {
  69. bool ok;
  70. List<Machine> page = m_machines.NextPage(out ok);
  71. if (ok)
  72. {
  73. dataGrid.ItemsSource = page;
  74. refreshBind();
  75. }
  76. }
  77. private void Query_Click(object sender, RoutedEventArgs e)
  78. {
  79. m_machines.ChangeFilter(edtMachine.Text.Trim(), edtFactory.Text.Trim());
  80. dataGrid.ItemsSource = m_machines.Page;
  81. refreshBind();
  82. }
  83. private void ConfirmImport_Click(object sender, RoutedEventArgs e)
  84. {
  85. //MySQLHelper.ExecuteNonQuery(strInsertSql, parameters);
  86. //int iReturn = CnasDataOperationFact.CnasDataOperation().InsertDataToCNASTable(ConvertDataRowToTable(drNewTarget), targetdataBase, banlanceitem.syncParamasInfos, banlanceitem.CnasInstrumentColumn, banlanceitem.lstFixedValue);
  87. //if (iReturn <= 0) //此时出现问题
  88. //{
  89. // if (iReturn == -1)
  90. // {
  91. // AppLog.ServiceInfo("数据库连接中断,终止本次上传。");
  92. // break; //此时数据库连接中断,直接跳出循环,结束本次数据同步传输
  93. // }
  94. // else if (iReturn == -2) //等于-2表示插入准备更新时发现数据一致,不再执行
  95. // {
  96. // OtherCount++;
  97. // }
  98. // else
  99. // {
  100. // ErrorCount++;
  101. // /*需要转换一下,否则会将整个dt全部写入日志*/
  102. // lstError.Add(GlobalCommonOperation.ConvertDataRowToTable(drNewTarget).Rows[0]);
  103. // }
  104. // bSuccess = false;
  105. //}
  106. }
  107. private void DownloadActivationCode_Click(object sender, RoutedEventArgs e)
  108. {
  109. var button = sender as Button;
  110. var machine = button.DataContext as Machine;
  111. if (machine != null)
  112. {
  113. try
  114. {
  115. // 生成激活码
  116. string activationCode = GenerateActivationCode(machine.MachineCode);
  117. // 创建保存文件对话框
  118. var saveFileDialog = new Microsoft.Win32.SaveFileDialog
  119. {
  120. FileName = $"激活码_{machine.MachineCode}_{DateTime.Now:yyyyMMdd}",
  121. DefaultExt = ".txt",
  122. Filter = "文本文件|*.txt"
  123. };
  124. if (saveFileDialog.ShowDialog() == true)
  125. {
  126. // 将激活码写入文件
  127. System.IO.File.WriteAllText(saveFileDialog.FileName, activationCode);
  128. MessageBox.Show("激活码已成功保存!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
  129. // 更新状态
  130. machine.IsGenerated = true;
  131. refreshBind();
  132. }
  133. }
  134. catch (Exception ex)
  135. {
  136. MessageBox.Show($"下载激活码失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  137. }
  138. }
  139. }
  140. private string GenerateActivationCode(string machineCode)
  141. {
  142. // 这里实现激活码生成算法
  143. // 示例:简单的加密算法,实际应用中应该使用更安全的加密方式
  144. using (var md5 = System.Security.Cryptography.MD5.Create())
  145. {
  146. // 加入时间戳使得生成的激活码具有时效性
  147. string input = machineCode + DateTime.Now.ToString("yyyyMMdd");
  148. byte[] inputBytes = Encoding.UTF8.GetBytes(input);
  149. byte[] hashBytes = md5.ComputeHash(inputBytes);
  150. // 转换为16进制字符串
  151. StringBuilder sb = new StringBuilder();
  152. for (int i = 0; i < hashBytes.Length; i++)
  153. {
  154. sb.Append(hashBytes[i].ToString("X2"));
  155. // 每4个字符添加一个分隔符,提高可读性
  156. if ((i + 1) % 4 == 0 && i != hashBytes.Length - 1)
  157. {
  158. sb.Append("-");
  159. }
  160. }
  161. return sb.ToString();
  162. }
  163. }
  164. }
  165. }