using HxModel; using HxUserManagement.Classes; using HxUserManagement.HxBLL; using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using XHandler.Class; namespace HxUserManagement.Views { /// /// EditUserInfo.xaml 的交互逻辑 /// public partial class EditUserInfo : UserControl { public event EventHandler closeEvent; public event EventHandler okEvent; private UserInfoModel userInfoModel = null; private bool bAdd = false; List roleList; List userList; UserInfoBLL userInfoBLL = new UserInfoBLL(); public EditUserInfo() { InitializeComponent(); bAdd = true; userInfoModel = new UserInfoModel(); this.DataContext = userInfoModel; userList = userInfoBLL.GetAllUserInfo(); title.Text = Properties.Resources.strNewUser; roleList = DataModel.GetRoles(); comboxRole.ItemsSource = roleList; comboxRole.SelectedIndex = 0; comboxStatus.ItemsSource = DataModel.GetStatusList(); comboxStatus.SelectedIndex = 1; } public EditUserInfo(string id) { InitializeComponent(); bAdd = false; userInfoModel = userInfoBLL.GetInfodById(id); this.DataContext = userInfoModel; userList = userInfoBLL.GetAllUserInfo(); title.Text = Properties.Resources.strEditUser; roleList = DataModel.GetRoles(); comboxRole.ItemsSource = roleList; comboxStatus.ItemsSource = DataModel.GetStatusList(); RolesModel role = roleList.Where(s => s.Id == userInfoModel.Authority).FirstOrDefault(); comboxRole.SelectedItem = role; comboxStatus.SelectedIndex = Convert.ToInt16(userInfoModel.Status); textboxUserName.IsEnabled = false; } #region 窗口关闭 private void btnClose_Click(object sender, RoutedEventArgs e) { if (EventResponseController.Instance.CanExecute() == false) return; Storyboard storyboard = new Storyboard(); DoubleAnimation doubleAnimation = new DoubleAnimation(500, 0, new Duration(TimeSpan.FromSeconds(0.25))); Storyboard.SetTarget(doubleAnimation, border);//Target对象 Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("Width"));//Target属性 storyboard.Children.Add(doubleAnimation); ObjectAnimationUsingKeyFrames showAnimation = new ObjectAnimationUsingKeyFrames(); showAnimation.BeginTime = TimeSpan.FromSeconds(0); Storyboard.SetTarget(showAnimation, border); Storyboard.SetTargetProperty(showAnimation, new PropertyPath("(UIElement.Visibility)")); DiscreteObjectKeyFrame closeKeyFrame = new DiscreteObjectKeyFrame(Visibility.Collapsed, TimeSpan.FromSeconds(0.26)); showAnimation.KeyFrames.Add(closeKeyFrame); storyboard.Children.Add(showAnimation); storyboard.Completed += delegate { closeEvent?.Invoke(this, EventArgs.Empty); }; storyboard.Begin(); } #endregion private void ButtonOK_Click(object sender, RoutedEventArgs e) { if (EventResponseController.Instance.CanExecute() == false) return; Storyboard storyboard = new Storyboard(); DoubleAnimation doubleAnimation = new DoubleAnimation(500, 0, new Duration(TimeSpan.FromSeconds(0.25))); Storyboard.SetTarget(doubleAnimation, border);//Target对象 Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("Width"));//Target属性 storyboard.Children.Add(doubleAnimation); ObjectAnimationUsingKeyFrames showAnimation = new ObjectAnimationUsingKeyFrames(); showAnimation.BeginTime = TimeSpan.FromSeconds(0); Storyboard.SetTarget(showAnimation, border); Storyboard.SetTargetProperty(showAnimation, new PropertyPath("(UIElement.Visibility)")); DiscreteObjectKeyFrame closeKeyFrame = new DiscreteObjectKeyFrame(Visibility.Collapsed, TimeSpan.FromSeconds(0.26)); showAnimation.KeyFrames.Add(closeKeyFrame); storyboard.Children.Add(showAnimation); storyboard.Completed += delegate { okEvent?.Invoke(this, EventArgs.Empty); }; storyboard.Begin(); } private void UserControl_Loaded(object sender, RoutedEventArgs e) { Storyboard storyboard = new Storyboard(); DoubleAnimation doubleAnimation = new DoubleAnimation(0, 500, new Duration(TimeSpan.FromSeconds(0.25))); Storyboard.SetTarget(doubleAnimation, border);//Target对象 Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("Width"));//Target属性 storyboard.Children.Add(doubleAnimation); ObjectAnimationUsingKeyFrames showAnimation = new ObjectAnimationUsingKeyFrames(); showAnimation.BeginTime = TimeSpan.FromSeconds(0); Storyboard.SetTarget(showAnimation, border); Storyboard.SetTargetProperty(showAnimation, new PropertyPath("(UIElement.Visibility)")); DiscreteObjectKeyFrame closeKeyFrame = new DiscreteObjectKeyFrame(Visibility.Visible, TimeSpan.FromSeconds(0.01)); showAnimation.KeyFrames.Add(closeKeyFrame); storyboard.Children.Add(showAnimation); storyboard.Begin(); } private void btnCancel_Click(object sender, RoutedEventArgs e) { if (EventResponseController.Instance.CanExecute() == false) return; btnClose_Click(this,null); } #region 保存 private void btnSave_Click(object sender, RoutedEventArgs e) { if (EventResponseController.Instance.CanExecute() == false) return; if (!CheckDataValid()) return; if (bAdd) // 添加用户 { AddNewUser(); } else { EditUser(); } ButtonOK_Click(this, null); } #endregion private void AddNewUser() { var user = userList.Where(s => s.UserName == userInfoModel.UserName).FirstOrDefault(); if (user != null) { textboxUserName.BorderBrush = Brushes.Red; usernameTips.Text = Properties.Resources.strUserNameExist; usernameTips.Visibility = Visibility.Visible; return; } userInfoModel.Id = Guid.NewGuid().ToString(); userInfoModel.UserPwd = Utilities.GetMD5("123456"); userInfoModel.CreateName = UserManagement.currentUser.UserName; userInfoModel.CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); UserInfoModel newUser = userInfoBLL.Add(userInfoModel); if (newUser != null) MessageBox.Show(Properties.Resources.strAddSuccess, "Tips"); else MessageBox.Show(Properties.Resources.strAddFailed, "Tips"); } private void EditUser() { userInfoModel.ModifyName = UserManagement.currentUser.UserName; userInfoModel.ModifyTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); UserInfoModel newUser = userInfoBLL.Update(userInfoModel); if (newUser != null) MessageBox.Show(Properties.Resources.strModifySucces, "Tips"); else MessageBox.Show(Properties.Resources.strModifyFailed, "Tips"); } private bool IsLegalPhoneNumber(string phoneNumber) { string phone = phoneNumber.Trim(); if (phone.Length == 11) return Regex.IsMatch(phone, @"^0?(13[0-9]|14[5-9]|15[012356789]|166|17[0-8]|18[0-9]|19[89])[0-9]{8}$"); else return false; } private bool IsLegalEmail(string email) { bool isValid = Regex.IsMatch(email, @"^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$"); return isValid; } private bool CheckDataValid() { if (string.IsNullOrEmpty(textboxUserName.Text.Trim())) { textboxUserName.BorderBrush = Brushes.Red; usernameTips.Text = Properties.Resources.strUserNameCannotEmpty; usernameTips.Visibility = Visibility.Visible; return false; } if (!string.IsNullOrEmpty(textboxPhone.Text.Trim())) { if (textboxPhone.Text.Trim().Length < 11) { textboxPhone.BorderBrush = Brushes.Red; phoneTips.Text = Properties.Resources.strPhoneLengthError; phoneTips.Visibility = Visibility.Visible; return false; } else if (!IsLegalPhoneNumber(textboxPhone.Text.Trim())) { textboxPhone.BorderBrush = Brushes.Red; phoneTips.Text = Properties.Resources.strPhoneError; phoneTips.Visibility = Visibility.Visible; return false; } } if (!string.IsNullOrEmpty(textboxMail.Text.Trim())) { if(!IsLegalEmail(textboxMail.Text.Trim())) { textboxMail.BorderBrush = Brushes.Red; emailTips.Text = Properties.Resources.strEmailError; emailTips.Visibility = Visibility.Visible; return false; } } return true; } private void Grid_PreviewMouseDown(object sender, MouseButtonEventArgs e) { if(e.OriginalSource is Grid) { Grid grid = e.OriginalSource as Grid; if(grid.Name == "root") btnClose_Click(this, null); } } private void textboxUserName_TextChanged(object sender, TextChangedEventArgs e) { textboxUserName.BorderBrush = Brushes.DarkGray; usernameTips.Visibility = Visibility.Collapsed; } private void textboxPhone_TextChanged(object sender, TextChangedEventArgs e) { textboxPhone.BorderBrush = Brushes.DarkGray; phoneTips.Visibility = Visibility.Collapsed; } private void textboxMail_TextChanged(object sender, TextChangedEventArgs e) { textboxMail.BorderBrush = Brushes.DarkGray; emailTips.Visibility = Visibility.Collapsed; } private void comboxRole_SelectionChanged(object sender, SelectionChangedEventArgs e) { RolesModel role = comboxRole.SelectedItem as RolesModel; userInfoModel.Authority = role.Id; userInfoModel.AuthorityName = role.Name; } private void comboxStatus_SelectionChanged(object sender, SelectionChangedEventArgs e) { userInfoModel.Status = comboxStatus.SelectedIndex.ToString(); } } }