using FATrace.WPLApp.Core; using FATrace.WPLApp.ModelDto; using FATrace.WPLApp.Services; using FATrace.WPLApp.Views; using Prism.Commands; using Prism.Events; using Prism.Regions; using Prism.Services.Dialogs; using System; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Windows; namespace FATrace.WPLApp.ViewModels { public class MainViewModel : NavigationViewModel { public MainViewModel(IRegionManager regionManager, IDialogService dialogService, IEventAggregator eventAggregator, NavigationServices navigationServices, SysRunService sysRunService) { this.regionManager = regionManager; DialogService = dialogService; NavigationServices = navigationServices; _sys = sysRunService; NavigationItems = BuildNavigationItems(_sys.CurAccessLevel); _sys.PropertyChanged += SysOnPropertyChanged; this.regionManager.RegisterViewWithRegion("FootRegion", typeof(FootView)); this.regionManager.RegisterViewWithRegion("HeadRegion", typeof(HeadView)); //this.regionManager.RegisterViewWithRegion("ContentRegion", typeof(ProFlowView)); } private IRegionManager regionManager { get; set; } public IDialogService DialogService { get; } public NavigationServices NavigationServices { get; } private readonly SysRunService _sys; public ObservableCollection NavigationItems { get; set; } private void SysOnPropertyChanged(object? sender, PropertyChangedEventArgs e) { if (e.PropertyName == nameof(SysRunService.CurAccessLevel) || e.PropertyName == nameof(SysRunService.CurUser)) { NavigationItems = BuildNavigationItems(_sys.CurAccessLevel); RaisePropertyChanged(nameof(NavigationItems)); } } private ObservableCollection BuildNavigationItems(string? accessLevel) { // 未登录/访客:仅允许 Dashboard + 用户登录 if (string.IsNullOrWhiteSpace(accessLevel) || string.Equals(accessLevel, UserManageViewModel.AccessLevels.Guest, StringComparison.OrdinalIgnoreCase)) { var dashboard = NavigationServices.NavItemDtos.FirstOrDefault(a => string.Equals(a.CmdPar, "Dashboard", StringComparison.OrdinalIgnoreCase)); var system = NavigationServices.NavItemDtos.FirstOrDefault(a => string.Equals(a.Name, "系统", StringComparison.OrdinalIgnoreCase)); var result = new ObservableCollection(); if (dashboard != null) result.Add(CloneNavItem(dashboard)); if (system != null) { var sysClone = CloneNavItem(system); sysClone.ChildrenNavItemDtos = new ObservableCollection( sysClone.ChildrenNavItemDtos .Where(a => string.Equals(a.CmdPar, "用户登录", StringComparison.OrdinalIgnoreCase)) .ToList()); result.Add(sysClone); } return result; } // 管理员/操作员:完整菜单 return new ObservableCollection(NavigationServices.NavItemDtos.Select(CloneNavItem)); } private NavItemDto CloneNavItem(NavItemDto src) { var dst = new NavItemDto { Name = src.Name, CmdPar = src.CmdPar, Icon = src.Icon, IsParent = src.IsParent, ChildrenNavItemDtos = new ObservableCollection() }; if (src.ChildrenNavItemDtos != null && src.ChildrenNavItemDtos.Count > 0) { foreach (var c in src.ChildrenNavItemDtos) { dst.ChildrenNavItemDtos.Add(CloneNavItem(c)); } } return dst; } private string titel = string.Empty; /// /// 标题 /// public string Title { get { return titel; } set { titel = value; RaisePropertyChanged(); } } private DelegateCommand _OpenCommand; /// /// 命令 /// public DelegateCommand OpenCommand { get { if (_OpenCommand == null) { _OpenCommand = new DelegateCommand((p) => OpenCommandMethod(p)); } return _OpenCommand; } } private void OpenCommandMethod(object obj) { var cmd = obj?.ToString() ?? string.Empty; // 父级目录/空命令不做任何处理(避免点击父节点触发弹窗或导航) if (string.IsNullOrWhiteSpace(cmd)) { return; } // 访客/未登录:仅 Dashboard + 用户登录 if (string.IsNullOrWhiteSpace(_sys.CurAccessLevel) || string.Equals(_sys.CurAccessLevel, UserManageViewModel.AccessLevels.Guest, StringComparison.OrdinalIgnoreCase)) { if (!string.Equals(cmd, "Dashboard", StringComparison.OrdinalIgnoreCase) && !string.Equals(cmd, "用户登录", StringComparison.OrdinalIgnoreCase)) { MessageBox.Show("当前账号权限为【访客】,仅允许访问 Dashboard。", "权限不足", MessageBoxButton.OK, MessageBoxImage.Warning); this.regionManager.Regions["ContentRegion"].RequestNavigate("DashBoardView"); return; } } switch (cmd) { case "Dashboard": this.regionManager.Regions["ContentRegion"].RequestNavigate("DashBoardView"); //this.regionManager.Regions["ContentRegion"].Activate(viewA); break; case "3D工艺流程": this.regionManager.Regions["ContentRegion"].RequestNavigate("ProFlowView"); //this.regionManager.Regions["DriveTree"].RequestNavigate("DeviceProtocolTreeView"); //this.regionManager.Regions["ContentRegion"].Activate(viewB); break; case "管道线路": DialogService.ShowDialog("DialogLineSelectView", new DialogParameters() { { "TabIndex", "SeletedGroupTabIndex" } }, (par) => { if (par.Result == ButtonResult.OK) { //程序名称 var ReturnValue = par.Parameters.GetValue("Name"); //返回数据,刷新Chart } else if (par.Result == ButtonResult.Cancel) { //取消 } }); break; //case "报表数据": // this.regionManager.Regions["ContentRegion"].RequestNavigate("ReportView"); //this.regionManager.Regions["ContentRegion"].Activate(viewB); //break; case "原料使用查询": this.regionManager.Regions["ContentRegion"].RequestNavigate("RawProUseView"); break; case "原料入库查询": this.regionManager.Regions["ContentRegion"].RequestNavigate("RawProInputView"); break; case "文件导入日志": this.regionManager.Regions["ContentRegion"].RequestNavigate("FileImportLogView"); break; // 工厂/OEM Excel 导入数据查询 case "工厂-入库": this.regionManager.Regions["ContentRegion"].RequestNavigate("FactoryInboundView"); break; case "工厂-领料": this.regionManager.Regions["ContentRegion"].RequestNavigate("FactoryMaterialWithdrawalView"); break; case "工厂-出入库": this.regionManager.Regions["ContentRegion"].RequestNavigate("FactoryInventoryTransactionView"); break; case "工厂-原料生产信息": this.regionManager.Regions["ContentRegion"].RequestNavigate("FactoryProductionRecordView"); break; case "工厂-成品出库": this.regionManager.Regions["ContentRegion"].RequestNavigate("FactoryOutboundView"); break; case "OEM-入库": this.regionManager.Regions["ContentRegion"].RequestNavigate("OEMInboundView"); break; case "OEM-出库": this.regionManager.Regions["ContentRegion"].RequestNavigate("OEMOutboundView"); break; case "OEM-出入库": this.regionManager.Regions["ContentRegion"].RequestNavigate("OEMInventoryTransactionView"); break; case "OEM-原料使用信息": this.regionManager.Regions["ContentRegion"].RequestNavigate("OEMRawUsageInfoView"); break; case "历史报警": this.regionManager.Regions["ContentRegion"].RequestNavigate("HistoryAlarmView"); //this.regionManager.Regions["ContentRegion"].Activate(viewB); break; case "日志信息": this.regionManager.Regions["ContentRegion"].RequestNavigate("LogView"); break; case "用户登录": this.regionManager.Regions["ContentRegion"].RequestNavigate("LoginView"); //this.regionManager.Regions["ContentRegion"].Activate(Shift); break; case "用户管理": this.regionManager.Regions["ContentRegion"].RequestNavigate("UserManageView"); break; case "称重用户": this.regionManager.Regions["ContentRegion"].RequestNavigate("WeightUserManageView"); break; case "使用手册": this.regionManager.Regions["ContentRegion"].RequestNavigate("HelpManualView"); break; default: break; } //this.regionManager.RegisterViewWithRegion("ContentRegion", typeof(CoreShift.Views.Shift.Index)); //MessageBox.Show(obj.ToString()); } } }