PasswordDialog.xaml.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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.Visibility = Visibility.Visible;
  106. }
  107. }
  108. } else if (type == PasswordType.CREATE)
  109. {
  110. //创建密码
  111. if (count == 0)
  112. {
  113. count++;
  114. tempPassword = pw;
  115. Title.Text = "再次输入密码";
  116. ClearVal();
  117. SetFocus(0);
  118. } else
  119. {
  120. if (tempPassword.Equals(pw))
  121. {
  122. //两次密码设置一致 显示提示输入框
  123. Title.Text = "填写密码提示";
  124. PasswordGrid.Visibility = Visibility.Collapsed;
  125. HintGrid.Visibility = Visibility.Visible;
  126. HintBox.Focus();
  127. } else
  128. {
  129. ErrorMsg.Text = "两次密码输入不一致";
  130. ErrorMsg.Visibility = Visibility.Visible;
  131. }
  132. }
  133. } else if (type == PasswordType.ALTER)
  134. {
  135. //修改密码
  136. if (appData.AppConfig.MenuPassword.Equals(pw))
  137. {
  138. tempType = type;
  139. type = PasswordType.CREATE;
  140. Title.Text = "设置新密码";
  141. ClearVal();
  142. SetFocus(0);
  143. } else
  144. {
  145. //密码比对不一致
  146. ErrorMsg.Text = "密码输入错误";
  147. ErrorMsg.Visibility = Visibility.Visible;
  148. HintMsg.Text = MainWindow.appData.AppConfig.PasswordHint;
  149. HintMsg.Visibility = Visibility.Visible;
  150. }
  151. }
  152. } else
  153. {
  154. //密码未输入完全 隐藏错误信息
  155. if (ErrorMsg.IsVisible)
  156. {
  157. ErrorMsg.Visibility = Visibility.Hidden;
  158. HintMsg.Visibility = Visibility.Hidden;
  159. HintMsg.Visibility = Visibility.Hidden;
  160. }
  161. }
  162. }
  163. public void SetFocus(int time = 100)
  164. {
  165. new Thread(() =>
  166. {
  167. Thread.Sleep(time);
  168. Dispatcher.Invoke(() =>
  169. {
  170. if (string.IsNullOrEmpty(P1.Password))
  171. {
  172. P1.Focus();
  173. return;
  174. }
  175. if (string.IsNullOrEmpty(P2.Password))
  176. {
  177. P2.Focus();
  178. return;
  179. }
  180. if (string.IsNullOrEmpty(P3.Password))
  181. {
  182. P3.Focus();
  183. return;
  184. }
  185. P4.Focus();
  186. });
  187. }).Start();
  188. }
  189. public void ClearVal()
  190. {
  191. P1.Clear();
  192. P2.Clear();
  193. P3.Clear();
  194. P4.Clear();
  195. }
  196. /// <summary>
  197. /// 跳过设置密码提示
  198. /// </summary>
  199. /// <param name="sender"></param>
  200. /// <param name="e"></param>
  201. private void NextTB_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  202. {
  203. appData.AppConfig.PasswordHint = "";
  204. DonePassword();
  205. }
  206. private void DoneTB_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  207. {
  208. string hint = HintBox.Text.Trim();
  209. appData.AppConfig.PasswordHint = hint;
  210. DonePassword();
  211. }
  212. private void DonePassword()
  213. {
  214. appData.AppConfig.MenuPassword = tempPassword;
  215. CommonCode.SavePassword(tempPassword);
  216. MainWindow.mainWindow.RightCard.PDDialog.Visibility = Visibility.Collapsed;
  217. PasswordGrid.Visibility = Visibility.Visible;
  218. HintGrid.Visibility = Visibility.Collapsed;
  219. if (tempType == PasswordType.ALTER)
  220. {
  221. HandyControl.Controls.Growl.Success("密码修改成功!", "MainWindowGrowl");
  222. } else
  223. {
  224. menuInfo.IsEncrypt = true;
  225. HandyControl.Controls.Growl.Success(menuInfo.MenuName + " 已加密!", "MainWindowGrowl");
  226. }
  227. }
  228. private void PasswordBox_KeyDown(object sender, KeyEventArgs e)
  229. {
  230. if (e.Key == Key.Back)
  231. {
  232. if (P2.IsKeyboardFocused)
  233. {
  234. if (string.IsNullOrEmpty(P2.Password))
  235. {
  236. P1.Password = "";
  237. } else
  238. {
  239. P2.Password = "";
  240. }
  241. }
  242. if (P3.IsKeyboardFocused)
  243. {
  244. if (string.IsNullOrEmpty(P3.Password))
  245. {
  246. P2.Password = "";
  247. }
  248. else
  249. {
  250. P3.Password = "";
  251. }
  252. }
  253. if (P4.IsKeyboardFocused)
  254. {
  255. if (string.IsNullOrEmpty(P4.Password))
  256. {
  257. P3.Password = "";
  258. }
  259. else
  260. {
  261. P4.Password = "";
  262. }
  263. }
  264. }
  265. SetFocus(0);
  266. }
  267. }
  268. }