CNAS取数仪器端升级
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

239 行
8.2KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  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. using System.Data.SQLite;
  18. namespace CNAS_Authorize
  19. {
  20. /// <summary>
  21. /// MainWindow.xaml 的交互逻辑
  22. /// </summary>
  23. public partial class MainWindow : Window
  24. {
  25. private MachineAuth m_machines;
  26. public MainWindow()
  27. {
  28. InitializeComponent();
  29. }
  30. private void Window_Loaded(object sender, RoutedEventArgs e)
  31. {
  32. this.LoadData();
  33. }
  34. private void LoadData()
  35. {
  36. m_machines = new MachineAuth();
  37. m_machines.Load();
  38. dataGrid.ItemsSource = m_machines.Page;
  39. this.DataContext = m_machines;
  40. }
  41. private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
  42. {
  43. if (this.Width < 500) return;
  44. dataGrid.Width = this.Width - 48;
  45. dataGrid.Height = centerPanel.Height;
  46. //设置列宽
  47. double colWidth = this.Width - 60;// centerPanel.Width;
  48. for (int i = 1; i < dataGrid.Columns.Count; i++)
  49. {
  50. colWidth = colWidth - dataGrid.Columns[i].Width.Value;
  51. }
  52. if (colWidth > 0)
  53. dataGrid.Columns[0].Width = colWidth;
  54. edtMachine.Width = this.Width - lbMachine.Width - btnQuery.Width - 48;
  55. edtFactory.Width = edtMachine.Width;
  56. }
  57. private void refreshBind()
  58. {
  59. this.DataContext = null;
  60. this.DataContext = m_machines;
  61. }
  62. private void PrevPage_Click(object sender, RoutedEventArgs e)
  63. {
  64. bool ok;
  65. List<Machine> page = m_machines.PrevPage(out ok);
  66. if (ok)
  67. {
  68. dataGrid.ItemsSource = page;
  69. refreshBind();
  70. }
  71. }
  72. private void NextPage_Click(object sender, RoutedEventArgs e)
  73. {
  74. bool ok;
  75. List<Machine> page = m_machines.NextPage(out ok);
  76. if (ok)
  77. {
  78. dataGrid.ItemsSource = page;
  79. refreshBind();
  80. }
  81. }
  82. private void Query_Click(object sender, RoutedEventArgs e)
  83. {
  84. m_machines.ChangeFilter(edtMachine.Text.Trim(), edtFactory.Text.Trim());
  85. dataGrid.ItemsSource = m_machines.Page;
  86. refreshBind();
  87. }
  88. private void ConfirmImport_Click(object sender, RoutedEventArgs e)
  89. {
  90. string macAddress = edtMachine.Text.Trim();
  91. string plantCode = edtFactory.Text.Trim();
  92. if (string.IsNullOrEmpty(macAddress) || string.IsNullOrEmpty(plantCode))
  93. {
  94. MessageBox.Show("MAC地址和工厂代码不能为空!", "提示");
  95. return;
  96. }
  97. string connectionString = "Data Source=|DataDirectory|\\CNAS.db;Version=3;";
  98. using (var connection = new SQLiteConnection(connectionString))
  99. {
  100. try
  101. {
  102. connection.Open();
  103. // 检查是否已存在
  104. using (var checkCommand = new SQLiteCommand(connection))
  105. {
  106. checkCommand.CommandText = "SELECT COUNT(*) FROM macaddress WHERE MAC_ADDRESS = @mac";
  107. checkCommand.Parameters.AddWithValue("@mac", macAddress);
  108. int count = Convert.ToInt32(checkCommand.ExecuteScalar());
  109. if (count > 0)
  110. {
  111. MessageBox.Show("已经导入!", "提示");
  112. return;
  113. }
  114. }
  115. // 插入新记录
  116. using (var insertCommand = new SQLiteCommand(connection))
  117. {
  118. insertCommand.CommandText = @"
  119. INSERT INTO macaddress (
  120. MAC_ADDRESS,
  121. PLANTCODE,
  122. CREATETIME,
  123. FACILITY_STYLE
  124. ) VALUES (
  125. @mac,
  126. @plant,
  127. @time,
  128. @style
  129. )";
  130. insertCommand.Parameters.AddWithValue("@mac", macAddress);
  131. insertCommand.Parameters.AddWithValue("@plant", plantCode);
  132. insertCommand.Parameters.AddWithValue("@time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  133. insertCommand.Parameters.AddWithValue("@style", "999");
  134. insertCommand.ExecuteNonQuery();
  135. //AppLog.ServiceInfo("导入成功");
  136. MessageBox.Show("导入成功...", "提示");
  137. this.LoadData();
  138. }
  139. }
  140. catch (Exception ex)
  141. {
  142. MessageBox.Show($"导入失败: {ex.Message}", "提示");
  143. //AppLog.ServiceError($"导入失败: {ex.Message}");
  144. }
  145. }
  146. }
  147. private void DownloadActivationCode_Click(object sender, RoutedEventArgs e)
  148. {
  149. var button = sender as Button;
  150. var machine = button.DataContext as Machine;
  151. if (machine != null)
  152. {
  153. try
  154. {
  155. // 生成激活码
  156. string activationCode = GenerateActivationCode(machine.MachineCode);
  157. // 创建保存文件对话框
  158. var saveFileDialog = new Microsoft.Win32.SaveFileDialog
  159. {
  160. FileName = $"激活码_{machine.MachineCode}_{DateTime.Now:yyyyMMdd}",
  161. DefaultExt = ".txt",
  162. Filter = "文本文件|*.txt"
  163. };
  164. if (saveFileDialog.ShowDialog() == true)
  165. {
  166. // 将激活码写入文件
  167. System.IO.File.WriteAllText(saveFileDialog.FileName, activationCode);
  168. MessageBox.Show("激活码已成功保存!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
  169. // 更新状态
  170. machine.IsGenerated = true;
  171. refreshBind();
  172. }
  173. }
  174. catch (Exception ex)
  175. {
  176. MessageBox.Show($"下载激活码失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  177. }
  178. }
  179. }
  180. private string GenerateActivationCode(string machineCode)
  181. {
  182. // 这里实现激活码生成算法
  183. // 示例:简单的加密算法,实际应用中应该使用更安全的加密方式
  184. using (var md5 = System.Security.Cryptography.MD5.Create())
  185. {
  186. // 加入时间戳使得生成的激活码具有时效性
  187. string input = machineCode + DateTime.Now.ToString("yyyyMMdd");
  188. byte[] inputBytes = Encoding.UTF8.GetBytes(input);
  189. byte[] hashBytes = md5.ComputeHash(inputBytes);
  190. // 转换为16进制字符串
  191. StringBuilder sb = new StringBuilder();
  192. for (int i = 0; i < hashBytes.Length; i++)
  193. {
  194. sb.Append(hashBytes[i].ToString("X2"));
  195. // 每4个字符添加一个分隔符,提高可读性
  196. if ((i + 1) % 4 == 0 && i != hashBytes.Length - 1)
  197. {
  198. sb.Append("-");
  199. }
  200. }
  201. return sb.ToString();
  202. }
  203. }
  204. }
  205. }