using BPASmartClient.CustomResource.Pages.Model; using BPASmartClient.CustomResource.Pages.View; using BPASmartClient.Helper; using Microsoft.Toolkit.Mvvm.ComponentModel; using Microsoft.Toolkit.Mvvm.Input; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BPASmartClient.CustomResource.Pages.ViewModel { public class UserConfigViewModel:ObservableObject { /// <summary> /// 页面列表集合 /// </summary> public ObservableCollection<UserTreeViewModel> pageTreeViewModels { get; set; } = new ObservableCollection<UserTreeViewModel>(); /// <summary> /// 已选择页面 /// </summary> public ObservableCollection<UserTreeViewModel> SelectePage { get; set; } = new ObservableCollection<UserTreeViewModel>(); /// <summary> /// 用户名 /// </summary> public string UserName { get { return _username; } set { _username = value; OnPropertyChanged(); } } private string _username; public RelayCommand<object> AddUserPageCommand { get; set; } public RelayCommand<object> DeleteUserPageCommand { get; set; } public RelayCommand DeleteAllPageCommand { get; set; } public RelayCommand AddAllPageCommand { get; set; } public RelayCommand SaveCommand { get; set; } public RelayCommand CancelCommand { get; set; } private void AddUserPage(object o) { if (o == null) return; if(o is string name) { var s = SelectePage.FirstOrDefault(x => x.Name == name); if (!SelectePage.Contains(s)) { foreach (UserTreeViewModel item in pageTreeViewModels) { var res = item.TreeViewItems.FirstOrDefault(P => P.Name == name); if(res != null) { SelectePage.Add(res); return; } } } } } private void DeleteUserPage(object o) { if (o == null) return; if (o is string name) { var res = SelectePage.FirstOrDefault(p=>p.Name == name); SelectePage.Remove(res); } } public UserConfigViewModel() { AddUserPageCommand = new RelayCommand<object>(AddUserPage); DeleteUserPageCommand = new RelayCommand<object>(DeleteUserPage); DeleteAllPageCommand = new RelayCommand(() => { SelectePage.Clear(); }); AddAllPageCommand = new RelayCommand(() => { foreach(UserTreeViewModel item in pageTreeViewModels) { foreach(var res in item.TreeViewItems) { if (!SelectePage.Contains(res)) SelectePage.Add(res); } } }); SaveCommand = new RelayCommand(() => { var res = Global.userManager.userInfos.FirstOrDefault(p => p.UserName == Global.changeUserInfo.UserName && p.Password == Global.changeUserInfo.Password); if(res != null) { res.userTreeViewModels = new List<UserTreeViewModel>(); res.userTreeViewModels.AddRange(SelectePage); Config.GetInstance.SaveUser(); } Global.changeUserInfo = null; ActionManage.GetInstance.Send("UserConfigViewConfirm"); }); CancelCommand = new RelayCommand(() => { Global.changeUserInfo = null; ActionManage.GetInstance.Send("UserConfigViewCancel"); }); foreach (var item in MenuManage.GetInstance.menuModels) { List<UserTreeViewModel> list = new List<UserTreeViewModel>(); foreach(var res in item.subMenumodels) { list.Add(new UserTreeViewModel { Name = res.SubMenuName}); } pageTreeViewModels.Add(new UserTreeViewModel { Name = item.MainMenuName, TreeViewItems = list }); } if(Global.changeUserInfo != null) { UserName = Global.changeUserInfo.UserName; if (Global.changeUserInfo.userTreeViewModels != null) { if(Global.changeUserInfo.userTreeViewModels.Count > 0) { foreach(UserTreeViewModel item in Global.changeUserInfo.userTreeViewModels) { SelectePage.Add(item); } } } } } } }