终端一体化运控平台
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.
 
 
 

173 line
8.4 KiB

  1. using BPA.Message;
  2. using BPASmartClient.Helper;
  3. using FryPot_DosingSystem.Model;
  4. using Microsoft.Toolkit.Mvvm.ComponentModel;
  5. using Microsoft.Toolkit.Mvvm.Input;
  6. using Newtonsoft.Json;
  7. using System;
  8. using System.Collections.ObjectModel;
  9. using System.IO;
  10. using System.Windows;
  11. namespace FryPot_DosingSystem.ViewModel
  12. {
  13. public class AdministratorLoginViewModel : ObservableObject
  14. {
  15. /// <summary>
  16. /// 按钮内容修改:注册/修改
  17. /// </summary>
  18. private string _registOrChange;
  19. public string RegistOrChange { get { return _registOrChange; } set { _registOrChange = value; OnPropertyChanged(); } }
  20. public string Admin { get { return _admin; } set { _admin = value; OnPropertyChanged(); } }
  21. private string _admin;
  22. public string Password { get { return _password; } set { _password = value; OnPropertyChanged(); } }
  23. private string _password;
  24. public string ErrorMessage { get { return _errorMessage; } set { _errorMessage = value; OnPropertyChanged(); } }
  25. private string _errorMessage;
  26. public string SelectText { get { return _mSelectText; } set { _mSelectText = value; OnPropertyChanged(); } }
  27. private string _mSelectText;
  28. private UserInfo _userInfo;
  29. public UserInfo UserInfo { get { return _userInfo; }set { _userInfo = value;OnPropertyChanged(); } }
  30. private string _account;
  31. public string Account { get { return _account; } set { _account = value;OnPropertyChanged(); } }
  32. private string _txtpassword;
  33. public string Txtpassword { get { return _txtpassword; } set { _txtpassword = value; OnPropertyChanged(); } }
  34. public RelayCommand AdminLoginCommand { get; set; }
  35. public RelayCommand RegistCommand { get; set; }
  36. public ObservableCollection<string> permission { get; set; } = new ObservableCollection<string>();
  37. public AdministratorLoginViewModel()
  38. {
  39. permission.Add("管理员");
  40. permission.Add("操作员");
  41. permission.Add("观察员");
  42. permission.Add("技术员");
  43. SelectText = permission[0];
  44. var content = ActionManage.GetInstance.SendResult("ContentUpdate");
  45. if (content != null && content is string strContent)
  46. {
  47. RegistOrChange = strContent;
  48. }
  49. var loginInfo = ActionManage.GetInstance.SendResult("LoginInfo");
  50. if (loginInfo != null && loginInfo is UserInfo LoginInfo)
  51. {
  52. UserInfo = LoginInfo;
  53. if (UserInfo != null)
  54. {
  55. switch (UserInfo.Authority)
  56. {
  57. case Authority.管理员: SelectText = permission[0]; break;
  58. case Authority.操作员: SelectText = permission[1]; break;
  59. case Authority.观察员: SelectText = permission[2]; break;
  60. case Authority.技术员: SelectText = permission[3]; break;
  61. }
  62. Account = UserInfo.UserName;
  63. Txtpassword = UserInfo.Password;
  64. }
  65. }
  66. RegistCommand = new RelayCommand(() =>
  67. {
  68. if (RegistOrChange != null)
  69. {
  70. if (RegistOrChange == "注册")
  71. {
  72. if (Admin == null || Admin == string.Empty || Password == null || Password == string.Empty)
  73. {
  74. ErrorMessage = "注册信息不能为空!!!";
  75. }
  76. else
  77. {
  78. ep:
  79. string guid=Guid.NewGuid().ToString();//用户唯一ID
  80. var resultManage = JsonConvert.DeserializeObject<UserManage>(File.ReadAllText("LoginUp.hbl").AESDecrypt());
  81. if (resultManage != null && resultManage is UserManage manage)
  82. {
  83. int a= manage.userInfos.FindIndex(p => p.UserId == guid);
  84. if (a == -1)
  85. {
  86. switch (SelectText)
  87. {
  88. case "管理员": Global.userManager.userInfos.Add(new UserInfo() { Authority = Authority.管理员, UserName = Admin, Password = Password,UserId=guid }); break;
  89. case "技术员": Global.userManager.userInfos.Add(new UserInfo() { Authority = Authority.技术员, UserName = Admin, Password = Password,UserId = guid }); break;
  90. case "操作员": Global.userManager.userInfos.Add(new UserInfo() { Authority = Authority.操作员, UserName = Admin, Password = Password, UserId = guid }); break;
  91. case "观察员": Global.userManager.userInfos.Add(new UserInfo() { Authority = Authority.观察员, UserName = Admin, Password = Password, UserId = guid }); break;
  92. }
  93. File.WriteAllText("LoginUp.hbl", JsonConvert.SerializeObject(Global.userManager).AESEncrypt());
  94. var result = JsonConvert.DeserializeObject<UserManage>(File.ReadAllText("LoginUp.hbl").AESDecrypt());
  95. Global.userManager = result;
  96. ErrorMessage = "注册成功";
  97. }
  98. else
  99. {
  100. goto ep;
  101. }
  102. }
  103. }
  104. }
  105. if (RegistOrChange == "修改")
  106. {
  107. //执行密码修改操作
  108. try
  109. {
  110. if (UserInfo != null)
  111. {
  112. UserInfo info = Global.userManager.userInfos.Find(p => p.UserId == UserInfo.UserId);
  113. if (info != null)
  114. {
  115. switch (SelectText)
  116. {
  117. case "管理员": info.Authority = Authority.管理员; info.UserName = Admin; info.Password = Password; break;
  118. case "技术员": info.Authority = Authority.技术员; info.UserName = Admin; info.Password = Password; break;
  119. case "操作员": info.Authority = Authority.操作员; info.UserName = Admin; info.Password = Password; break;
  120. case "观察员": info.Authority = Authority.观察员; info.UserName = Admin; info.Password = Password; break;
  121. }
  122. File.WriteAllText("LoginUp.hbl", JsonConvert.SerializeObject(Global.userManager).AESEncrypt());
  123. var result = JsonConvert.DeserializeObject<UserManage>(File.ReadAllText("LoginUp.hbl").AESDecrypt());
  124. Global.userManager = result;
  125. ErrorMessage = "修改成功";
  126. }
  127. else
  128. {
  129. ErrorMessage = "修改失败";
  130. }
  131. }
  132. else
  133. {
  134. ErrorMessage = "修改失败";
  135. }
  136. }
  137. catch (Exception)
  138. {
  139. ErrorMessage = "修改失败";
  140. }
  141. }
  142. }
  143. });
  144. AdminLoginCommand = new RelayCommand(() =>
  145. {
  146. var rest = ActionManage.GetInstance.SendResult("LoginDosingSystem", $"{Admin}-={Password}-={SelectText}");
  147. if (rest != null && rest is string str)
  148. {
  149. Account = String.Empty;
  150. Txtpassword = String.Empty;
  151. ErrorMessage = str;
  152. }
  153. });
  154. }
  155. }
  156. }