UserEntityWindow.xaml.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using ivf_tl_Entity.Entity;
  2. using ivf_tl_Entity.Entity.AppSetting;
  3. using ivf_tl_Entity.Entity.Mark;
  4. using ivf_tl_Entity.Enums;
  5. using ivf_tl_Manage.Converts;
  6. using Newtonsoft.Json;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Collections.ObjectModel;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Controls;
  15. using System.Windows.Data;
  16. using System.Windows.Documents;
  17. using System.Windows.Input;
  18. using System.Windows.Media;
  19. using System.Windows.Media.Imaging;
  20. using System.Windows.Shapes;
  21. namespace ivf_tl_Manage.Win
  22. {
  23. /// <summary>
  24. /// UserEntityWindow.xaml 的交互逻辑
  25. /// </summary>
  26. public partial class UserEntityWindow : Window
  27. {
  28. private bool isAdd = true;
  29. private UserManageEntity UserManageEntity { get; set; } = null;
  30. public UserEntityWindow(ObservableCollection<RoleEntity> roleList)
  31. {
  32. InitializeComponent();
  33. this.Owner = AppData.Instance.MainWindow;
  34. this._role_ComBox.ItemsSource = roleList;
  35. this.Closed += (a, b) =>
  36. {
  37. AppData.Instance.MainWindow.Activate();
  38. };
  39. }
  40. public UserEntityWindow(UserManageEntity markKeyEntity, ObservableCollection<RoleEntity> roleList)
  41. {
  42. InitializeComponent();
  43. this.Owner = AppData.Instance.MainWindow;
  44. this._role_ComBox.ItemsSource = roleList;
  45. UserManageEntity = markKeyEntity;
  46. if(markKeyEntity.roles != null && markKeyEntity.roles.Any() && roleList.Any())
  47. {
  48. this._role_ComBox.SelectedItem = roleList.FirstOrDefault(x => x.id == markKeyEntity.roles.First().id);
  49. }
  50. isAdd = false;
  51. //this._title_TextBlock.Text = "编辑";
  52. this._title_TextBlock.Text = KeyToStringConvert.GetLanguageStringByKey("0083");
  53. this._name_TextBox.Text = UserManageEntity.username;
  54. this._account_TextBox.Text = UserManageEntity.account;
  55. this._password_TextBox.Text = "********";
  56. this._passSure_TextBox.Text = "********";
  57. this._account_TextBox.IsEnabled = false;
  58. this._password_TextBox.IsEnabled = false;
  59. this._passSure_TextBox.IsEnabled = false;
  60. this.Closed += (a, b) =>
  61. {
  62. AppData.Instance.MainWindow.Activate();
  63. };
  64. }
  65. private void MessageBoxShow(string mess)
  66. {
  67. this.message.Text = mess;
  68. }
  69. private void ExLog(Exception ex, string name)
  70. {
  71. AppData.Instance.LogService.ExceptionLog(ex, $"UserEntityWindow.{name}", LogEnum.RunException);
  72. }
  73. private void ErrorLog(string message, LogEnum logType)
  74. {
  75. AppData.Instance.LogService.TLLog($"UserEntityWindow.{message}", logType);
  76. }
  77. private void Cancel_Click(object sender, RoutedEventArgs e)
  78. {
  79. this.DialogResult = false;
  80. return;
  81. }
  82. private void Ok_Click(object sender, RoutedEventArgs e)
  83. {
  84. try
  85. {
  86. string nameString = this._name_TextBox.Text.Trim();
  87. if (string.IsNullOrEmpty(nameString))
  88. {
  89. //MessageBoxShow($"用户名称不能为空");
  90. MessageBoxShow(KeyToStringConvert.GetLanguageStringByKey("0135"));
  91. return;
  92. }
  93. string accountString = this._account_TextBox.Text.Trim();
  94. if (string.IsNullOrEmpty(accountString))
  95. {
  96. //MessageBoxShow($"登录账号不能为空");
  97. MessageBoxShow(KeyToStringConvert.GetLanguageStringByKey("0131"));
  98. return;
  99. }
  100. string wordString = this._password_TextBox.Text.Trim();
  101. if (string.IsNullOrEmpty(wordString))
  102. {
  103. //MessageBoxShow($"登录密码不能为空");
  104. MessageBoxShow(KeyToStringConvert.GetLanguageStringByKey("0136"));
  105. return;
  106. }
  107. string wordSureString = this._passSure_TextBox.Text.Trim();
  108. if (string.IsNullOrEmpty(wordSureString))
  109. {
  110. //MessageBoxShow($"确认密码不能为空");
  111. MessageBoxShow(KeyToStringConvert.GetLanguageStringByKey("0137"));
  112. return;
  113. }
  114. if(wordString != wordSureString)
  115. {
  116. //MessageBoxShow($"两次密码不一致");
  117. MessageBoxShow(KeyToStringConvert.GetLanguageStringByKey("0285"));
  118. return;
  119. }
  120. if(!(this._role_ComBox.SelectedItem is RoleEntity currentRole))
  121. {
  122. //MessageBoxShow($"请选择角色");
  123. MessageBoxShow(KeyToStringConvert.GetLanguageStringByKey("0133"));
  124. return;
  125. }
  126. long roleId = currentRole.id;
  127. var pro = AppData.Instance.GetSettingProvider();
  128. string error = null;
  129. if (isAdd)
  130. {
  131. string body = JsonConvert.SerializeObject(new
  132. {
  133. account = accountString,
  134. password = wordString,
  135. username = nameString,
  136. verifyPassword = wordSureString,
  137. roleIds = new List<long> { roleId },
  138. });
  139. error = pro.AddUserApi(body);
  140. }
  141. else
  142. {
  143. string body = JsonConvert.SerializeObject(new
  144. {
  145. id = UserManageEntity.id,
  146. username= nameString,
  147. roleIds=new List<long> { roleId },
  148. });
  149. error = pro.EditUserApi(body);
  150. }
  151. if (!string.IsNullOrEmpty(error))
  152. {
  153. //MessageBoxShow($"操作失败,{error}");
  154. MessageBoxShow($"{KeyToStringConvert.GetLanguageStringByKey("0286")},{error}");
  155. return;
  156. }
  157. if (!isAdd)
  158. {
  159. UserManageEntity.username = nameString;
  160. UserManageEntity.roles = new List<RoleEntity> { currentRole };
  161. }
  162. this.DialogResult = true;
  163. return;
  164. }
  165. catch (Exception ex)
  166. {
  167. ExLog(ex, "Ok_Click");
  168. //MessageBoxShow($"操作失败:{ex.Message}");
  169. MessageBoxShow($"{KeyToStringConvert.GetLanguageStringByKey("0291")}:{ex.Message}");
  170. }
  171. }
  172. }
  173. }