CNAS取数仪器端升级
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MainWindow.xaml.cs 8.2KB

4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. 
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Security.Cryptography;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using System.Windows.Shapes;
  18. using System.Data.SQLite;
  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. string macAddress = edtMachine.Text.Trim();
  92. string plantCode = edtFactory.Text.Trim();
  93. if (string.IsNullOrEmpty(macAddress) || string.IsNullOrEmpty(plantCode))
  94. {
  95. MessageBox.Show("MAC地址和工厂代码不能为空!", "提示");
  96. return;
  97. }
  98. string connectionString = "Data Source=|DataDirectory|\\CNAS.db;Version=3;";
  99. using (var connection = new SQLiteConnection(connectionString))
  100. {
  101. try
  102. {
  103. connection.Open();
  104. // 检查是否已存在
  105. using (var checkCommand = new SQLiteCommand(connection))
  106. {
  107. checkCommand.CommandText = "SELECT COUNT(*) FROM macaddress WHERE MAC_ADDRESS = @mac";
  108. checkCommand.Parameters.AddWithValue("@mac", macAddress);
  109. int count = Convert.ToInt32(checkCommand.ExecuteScalar());
  110. if (count > 0)
  111. {
  112. MessageBox.Show("已经导入!", "提示");
  113. return;
  114. }
  115. }
  116. // 插入新记录
  117. using (var insertCommand = new SQLiteCommand(connection))
  118. {
  119. insertCommand.CommandText = @"
  120. INSERT INTO macaddress (
  121. MAC_ADDRESS,
  122. PLANTCODE,
  123. CREATETIME,
  124. FACILITY_STYLE
  125. ) VALUES (
  126. @mac,
  127. @plant,
  128. @time,
  129. @style
  130. )";
  131. insertCommand.Parameters.AddWithValue("@mac", macAddress);
  132. insertCommand.Parameters.AddWithValue("@plant", plantCode);
  133. insertCommand.Parameters.AddWithValue("@time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  134. insertCommand.Parameters.AddWithValue("@style", "999");
  135. insertCommand.ExecuteNonQuery();
  136. //AppLog.ServiceInfo("导入成功");
  137. MessageBox.Show("导入成功...", "提示");
  138. this.LoadData();
  139. }
  140. }
  141. catch (Exception ex)
  142. {
  143. MessageBox.Show($"导入失败: {ex.Message}", "提示");
  144. //AppLog.ServiceError($"导入失败: {ex.Message}");
  145. }
  146. }
  147. }
  148. private void DownloadActivationCode_Click(object sender, RoutedEventArgs e)
  149. {
  150. var button = sender as Button;
  151. var machine = button.DataContext as Machine;
  152. if (machine != null)
  153. {
  154. try
  155. {
  156. // 生成激活码
  157. string activationCode = GenerateActivationCode(machine.MachineCode);
  158. // 创建保存文件对话框
  159. var saveFileDialog = new Microsoft.Win32.SaveFileDialog
  160. {
  161. FileName = $"激活码_{machine.MachineCode}_{DateTime.Now:yyyyMMdd}",
  162. DefaultExt = ".txt",
  163. Filter = "文本文件|*.txt"
  164. };
  165. if (saveFileDialog.ShowDialog() == true)
  166. {
  167. // 将激活码写入文件
  168. System.IO.File.WriteAllText(saveFileDialog.FileName, activationCode);
  169. MessageBox.Show("激活码已成功保存!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
  170. // 更新状态
  171. machine.IsGenerated = true;
  172. refreshBind();
  173. }
  174. }
  175. catch (Exception ex)
  176. {
  177. MessageBox.Show($"下载激活码失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  178. }
  179. }
  180. }
  181. private string GenerateActivationCode(string machineCode)
  182. {
  183. // 这里实现激活码生成算法
  184. // 示例:简单的加密算法,实际应用中应该使用更安全的加密方式
  185. using (var md5 = System.Security.Cryptography.MD5.Create())
  186. {
  187. // 加入时间戳使得生成的激活码具有时效性
  188. string input = machineCode + DateTime.Now.ToString("yyyyMMdd");
  189. byte[] inputBytes = Encoding.UTF8.GetBytes(input);
  190. byte[] hashBytes = md5.ComputeHash(inputBytes);
  191. // 转换为16进制字符串
  192. StringBuilder sb = new StringBuilder();
  193. for (int i = 0; i < hashBytes.Length; i++)
  194. {
  195. sb.Append(hashBytes[i].ToString("X2"));
  196. // 每4个字符添加一个分隔符,提高可读性
  197. if ((i + 1) % 4 == 0 && i != hashBytes.Length - 1)
  198. {
  199. sb.Append("-");
  200. }
  201. }
  202. return sb.ToString();
  203. }
  204. }
  205. }
  206. }