using Admin.Core.IService; using Aucma.Core.PrintTo.Common; using Aucma.Core.PrintTo.Views; using Aucma.Core.ProductOffLine.Config; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Messaging; using log4net; using Microsoft.Extensions.DependencyInjection; using System; using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Threading; namespace Aucma.Core.PrintTo.ViewModels { public partial class MainWindowViewModel : ObservableObject { private static readonly log4net.ILog log = LogManager.GetLogger(typeof(MainWindowViewModel)); private IndexPageView firstPage = new IndexPageView();//首页 ISysUserInfoServices _sysUserInfoServices; private AppConfig appConfig = AppConfig.Instance; public MainWindowViewModel() { InitializeAsync(); } private async void InitializeAsync() { await Task.Run(() => { UserContent = firstPage; _sysUserInfoServices = App.ServiceProvider.GetService(); //Task.Run(async () => //{ // await RefreshTeamTime();//班组时间 //}); InitUserInfo(); DispatcherTimer timer = new DispatcherTimer(); timer.Interval = new TimeSpan(0, 0, 1); //间隔1秒 timer.Tick += new EventHandler(timer_Tick); timer.Start(); }); } #region 更换界面 public System.Windows.Controls.UserControl _content; public System.Windows.Controls.UserControl UserContent { get => _content; set => SetProperty(ref _content, value); } #endregion #region plc 状态 /// /// UI plc 展示状态-文字 /// public string _plcUIStatus; public string PlcUIStatusWb { //get { return plcUIStatusWb; } //set { plcUIStatusWb = value; RaisePropertyChanged("PlcUIStatusWb"); } get => _plcUIStatus; set => SetProperty(ref _plcUIStatus, value); } /// /// UI plc 展示状态-颜色 /// public string _plcUIColor; public string PlcUIColor { //get { return plcUIColor; } //set { plcUIColor = value; RaisePropertyChanged("PlcUIColor"); } get => _plcUIColor; set => SetProperty(ref _plcUIColor, value); } /// /// UI plc 展示状态-图标 /// public string _plcUIIcon; public string PlcUIIcon { //get { return plcUIIcon; } //set { plcUIIcon = value; RaisePropertyChanged("plcUIIcon"); } get => _plcUIIcon; set => SetProperty(ref _plcUIIcon, value); } /// /// PLC连接状态-true:连接成功;false:失败 /// /// public void PlcState(bool type) { if (type) { PlcUIStatusWb = "PLC连接成功"; PlcUIColor = "White"; PlcUIIcon = "Assets/Images/正常.png"; } else { PlcUIStatusWb = "PLC状态异常"; PlcUIColor = "Red"; PlcUIIcon = "Assets/Images/失败-01.png"; } } #endregion #region 窗口操作 /// /// 窗口操作 /// [RelayCommand] private void FormControl(object obj) { try { string controlType = obj as string; switch (controlType) { // 打开软盘 case "TabTip": OSKHelper.OpenOsk(); break; // 还原 或者 最大化当前窗口 case "Normal": if (Application.Current.MainWindow.WindowState == WindowState.Normal) { Application.Current.MainWindow.WindowState = WindowState.Maximized; break; } if (Application.Current.MainWindow.WindowState == WindowState.Maximized) { Application.Current.MainWindow.WindowState = WindowState.Normal; break; } break; default: break; } } catch (Exception ex) { log.Error("窗体控制逻辑异常", ex); } } #endregion #region 最小化界面 /// /// 关闭当前界面 /// /// [RelayCommand] public void MinWindow(object parameter) { var window = parameter as Window; if (window == null) return; window.WindowState = WindowState.Minimized; } #endregion #region 关闭当前界面 /// /// 关闭当前界面 /// /// [RelayCommand] public void CloseWindow(object parameter) { var window = parameter as Window; if (window == null) return; if (MessageBox.Show("确定要退出系统吗?", "系统提醒", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes) { appConfig.TeamCode = ""; appConfig.TeamName = ""; appConfig.Account = ""; window.Hide(); //跳转到登录页 LoginPageView login = new LoginPageView(); login.Show(); window.Close(); } } #endregion #region 刷新时间 public string _currentDateTime; public string CurrentDateTime { get => _currentDateTime; set => SetProperty(ref _currentDateTime, value); } #endregion #region 班组信息 /// /// 班组信息 /// public string _teamName; public string TeamName { get => _teamName; set => SetProperty(ref _teamName, value); } #endregion #region 登陆人信息 /// /// 班组信息 /// public string account; public string Account { get => account; set => SetProperty(ref account, value); } #endregion private void InitUserInfo() { TeamName = $"班组:{appConfig.TeamName} 用户:{appConfig.Account}"; } #region 切换班组 //public async Task RefreshTeamTime() //{ // while (true) // { // var list = await _sysUserInfoServices.GetTeamData(); // if (list != null && list.Count > 0) // { // var sysUserInfo = list.First(); // TeamName = $"{appConfig.TeamName}({list.Min(d => d.StartTime).ToString("HH:mm")}~{list.Max(d => d.EndTime).ToString("HH:mm")})"; // //TeamName = $"{sysUserInfo.TeamName}"; // } // Thread.Sleep(5000); // } //} #endregion #region 定时刷新时间 void timer_Tick(object sender, EventArgs e) { System.Windows.Application.Current.Dispatcher.Invoke((Action)(() => { CurrentDateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); })); } #endregion } }