PasswordDialog.xaml.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. using GeekDesk.Constant;
  2. using GeekDesk.Util;
  3. using GeekDesk.ViewModel;
  4. using Microsoft.Win32;
  5. using System;
  6. using System.Threading;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Input;
  10. using System.Windows.Media.Imaging;
  11. using System.Windows.Threading;
  12. namespace GeekDesk.Control.Other
  13. {
  14. /// <summary>
  15. /// TextDialog.xaml 的交互逻辑
  16. /// </summary>
  17. public partial class PasswordDialog
  18. {
  19. private AppData appData = MainWindow.appData;
  20. public PasswordType type;
  21. public MenuInfo menuInfo;
  22. public int count = 0;
  23. private string tempPassword = null;
  24. private PasswordType tempType;
  25. public PasswordDialog()
  26. {
  27. InitializeComponent();
  28. }
  29. private void PasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
  30. {
  31. PasswordBox pb = sender as PasswordBox;
  32. if (!string.IsNullOrEmpty(pb.Password))
  33. {
  34. char c = pb.Password.ToCharArray()[0];
  35. if (c > '9' || c < '0')
  36. {
  37. pb.Password = "";
  38. return;
  39. }
  40. }
  41. string tag = pb.Tag.ToString();
  42. switch (tag)
  43. {
  44. case "P1":
  45. if (!string.IsNullOrEmpty(pb.Password))
  46. {
  47. P2.Focus();
  48. }
  49. break;
  50. case "P2":
  51. if (!string.IsNullOrEmpty(pb.Password))
  52. {
  53. P3.Focus();
  54. }
  55. break;
  56. case "P3":
  57. if (!string.IsNullOrEmpty(pb.Password))
  58. {
  59. P4.Focus();
  60. }
  61. break;
  62. case "P4":
  63. if (string.IsNullOrEmpty(pb.Password))
  64. {
  65. P3.Focus();
  66. }
  67. break;
  68. }
  69. if (!string.IsNullOrEmpty(P1.Password)
  70. && !string.IsNullOrEmpty(P2.Password)
  71. && !string.IsNullOrEmpty(P3.Password)
  72. && !string.IsNullOrEmpty(P4.Password))
  73. {
  74. string pw = P1.Password
  75. + P2.Password
  76. + P3.Password
  77. + P4.Password;
  78. pw = MD5Util.CreateMD5(pw);
  79. if (type == PasswordType.INPUT || type == PasswordType.CANCEL)
  80. {
  81. if (pw.Equals(appData.AppConfig.MenuPassword))
  82. {
  83. //隐藏弹框
  84. MainWindow.mainWindow.RightCard.PDDialog.Visibility = Visibility.Collapsed;
  85. //赋值
  86. MainWindow.appData.AppConfig.SelectedMenuIcons
  87. = appData.MenuList[
  88. MainWindow.mainWindow.LeftCard.MenuListBox.SelectedIndex
  89. ].IconList;
  90. //显示数据托盘
  91. MainWindow.mainWindow.RightCard.WrapUFG.Visibility = Visibility.Visible;
  92. //取消加密操作
  93. if (type == PasswordType.CANCEL)
  94. {
  95. menuInfo.IsEncrypt = false;
  96. }
  97. } else
  98. {
  99. //密码比对不一致
  100. ErrorMsg.Text = "密码输入错误";
  101. ErrorMsg.Visibility = Visibility.Visible;
  102. if (!string.IsNullOrEmpty(appData.AppConfig.PasswordHint))
  103. {
  104. //显示提示信息
  105. HintMsg.Text = "提示: " + appData.AppConfig.PasswordHint;
  106. HintMsg.Visibility = Visibility.Visible;
  107. }
  108. }
  109. } else if (type == PasswordType.CREATE)
  110. {
  111. //创建密码
  112. if (count == 0)
  113. {
  114. count++;
  115. tempPassword = pw;
  116. Title.Text = "再次输入密码";
  117. ClearVal();
  118. SetFocus(0);
  119. } else
  120. {
  121. if (tempPassword.Equals(pw))
  122. {
  123. //两次密码设置一致 显示提示输入框
  124. Title.Text = "填写密码提示";
  125. PasswordGrid.Visibility = Visibility.Collapsed;
  126. HintGrid.Visibility = Visibility.Visible;
  127. HintBox.Focus();
  128. } else
  129. {
  130. ErrorMsg.Text = "两次密码输入不一致";
  131. ErrorMsg.Visibility = Visibility.Visible;
  132. }
  133. }
  134. } else if (type == PasswordType.ALTER)
  135. {
  136. //修改密码
  137. if (appData.AppConfig.MenuPassword.Equals(pw))
  138. {
  139. tempType = type;
  140. type = PasswordType.CREATE;
  141. Title.Text = "设置新密码";
  142. ClearVal();
  143. SetFocus(0);
  144. } else
  145. {
  146. //密码比对不一致
  147. ErrorMsg.Text = "密码输入错误";
  148. ErrorMsg.Visibility = Visibility.Visible;
  149. HintMsg.Text = MainWindow.appData.AppConfig.PasswordHint;
  150. HintMsg.Visibility = Visibility.Visible;
  151. }
  152. }
  153. } else
  154. {
  155. //密码未输入完全 隐藏错误信息
  156. if (ErrorMsg.IsVisible)
  157. {
  158. ErrorMsg.Visibility = Visibility.Hidden;
  159. HintMsg.Visibility = Visibility.Hidden;
  160. HintMsg.Visibility = Visibility.Hidden;
  161. }
  162. }
  163. }
  164. public void SetFocus(int time = 100)
  165. {
  166. new Thread(() =>
  167. {
  168. Thread.Sleep(time);
  169. try
  170. {
  171. Dispatcher.Invoke(() =>
  172. {
  173. try
  174. {
  175. if (string.IsNullOrEmpty(P1.Password))
  176. {
  177. P1.Focus();
  178. return;
  179. }
  180. if (string.IsNullOrEmpty(P2.Password))
  181. {
  182. P2.Focus();
  183. return;
  184. }
  185. if (string.IsNullOrEmpty(P3.Password))
  186. {
  187. P3.Focus();
  188. return;
  189. }
  190. P4.Focus();
  191. }
  192. catch (Exception ex) { }
  193. });
  194. }
  195. catch (Exception e2) { }
  196. }).Start();
  197. }
  198. public void ClearVal()
  199. {
  200. P1.Clear();
  201. P2.Clear();
  202. P3.Clear();
  203. P4.Clear();
  204. }
  205. /// <summary>
  206. /// 跳过设置密码提示
  207. /// </summary>
  208. /// <param name="sender"></param>
  209. /// <param name="e"></param>
  210. private void NextTB_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  211. {
  212. appData.AppConfig.PasswordHint = "";
  213. DonePassword();
  214. }
  215. private void DoneTB_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  216. {
  217. string hint = HintBox.Text.Trim();
  218. appData.AppConfig.PasswordHint = hint;
  219. DonePassword();
  220. }
  221. private void DonePassword()
  222. {
  223. appData.AppConfig.MenuPassword = tempPassword;
  224. CommonCode.SavePassword(tempPassword);
  225. MainWindow.mainWindow.RightCard.PDDialog.Visibility = Visibility.Collapsed;
  226. PasswordGrid.Visibility = Visibility.Visible;
  227. HintGrid.Visibility = Visibility.Collapsed;
  228. if (tempType == PasswordType.ALTER)
  229. {
  230. HandyControl.Controls.Growl.Success("密码修改成功!", "MainWindowGrowl");
  231. } else
  232. {
  233. menuInfo.IsEncrypt = true;
  234. HandyControl.Controls.Growl.Success(menuInfo.MenuName + " 已加密!", "MainWindowGrowl");
  235. }
  236. }
  237. private void PasswordBox_KeyDown(object sender, KeyEventArgs e)
  238. {
  239. if (e.Key == Key.Back)
  240. {
  241. if (P2.IsKeyboardFocused)
  242. {
  243. if (string.IsNullOrEmpty(P2.Password))
  244. {
  245. P1.Password = "";
  246. } else
  247. {
  248. P2.Password = "";
  249. }
  250. }
  251. if (P3.IsKeyboardFocused)
  252. {
  253. if (string.IsNullOrEmpty(P3.Password))
  254. {
  255. P2.Password = "";
  256. }
  257. else
  258. {
  259. P3.Password = "";
  260. }
  261. }
  262. if (P4.IsKeyboardFocused)
  263. {
  264. if (string.IsNullOrEmpty(P4.Password))
  265. {
  266. P3.Password = "";
  267. }
  268. else
  269. {
  270. P4.Password = "";
  271. }
  272. }
  273. }
  274. SetFocus(0);
  275. }
  276. }
  277. }