using Prism.Commands; using Prism.Events; using Prism.Ioc; using Prism.Mvvm; using Prism.Services.Dialogs; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Security.Principal; using System.Text; using System.Threading.Tasks; using System.Windows.Input; using System.Windows; using System.Windows.Shapes; using Txgy.EWS.Client.Common.Extensions; using Txgy.EWS.Client.Entity; using Txgy.EWS.Client.Models; using Txgy.EWS.Client.IBLL; using Txgy.EWS.Client.Common.MessageEvents; using System.Windows.Threading; using Txgy.EWS.Client.Common; using Txgy.EWS.Client.ILog; namespace Txgy.EWS.Client.Start.ViewModels { public class LoginViewModel : BindableBase { #region 属性 ILogHelper _logHelper; public DelegateCommand CancelLoadingCommand { get => new DelegateCommand(() => IsLoading = false); } private bool _isLoading; public bool IsLoading { get { return _isLoading; } set { SetProperty(ref _isLoading, value); } } private bool _isCanLoading = false; public bool IsCanLoading { get { return _isCanLoading; } set { SetProperty(ref _isCanLoading, value); } } private string _loadingMessage; public string LoadingMessage { get { return _loadingMessage; } set { SetProperty(ref _loadingMessage, value); } } private string _userName = "yuwu"; public string UserName { get { return _userName; } set { SetProperty(ref _userName, value); } } private string _password = "123456"; public string Password { get { return _password; } set { SetProperty(ref _password, value); } } private string _errorMsg; public string ErrorMsg { get { return _errorMsg; } set { SetProperty(ref _errorMsg, value); } } #endregion private DelegateCommand _loginCommand; public ICommand LoginCommand { get { if (_loginCommand == null) { _loginCommand = new DelegateCommand(OnLogin, CanLogin); } return _loginCommand; } } private bool CanLogin(object arg) { return IsCanLoading; } private async void OnLogin(object obj) { if (IsCanLoading) { try { IsCanLoading = false; RaisePropertyChanged(nameof(IsCanLoading)); this.ErrorMsg = ""; if (string.IsNullOrEmpty(this.UserName)) { this.ErrorMsg = "请输入用户名"; return; } if (string.IsNullOrEmpty(this.Password)) { this.ErrorMsg = "请输入密码"; return; } this.IsLoading = true; this.LoadingMessage = "正在登录"; // 请求WebAPI await Task.Delay(1000); if (await _loginBLL.Login(this.UserName, this.Password)) { _logHelper.Debug(this, $"{this.UserName}登录"); (obj as Window).DialogResult = true; } else { // 提示错误 this.ErrorMsg = "用户名或密码错误,请重新输入"; this.IsLoading = false; } } catch (Exception ex) { _logHelper.Error(this, ex.Message); //ex.StackTrace; //ex.Message; // 消息提示 MessageBox.Show("登录失败!"); } } } //GlobalEntity _globalEntity = null; IUpgradeFileBLL _systemBLL = null; ILoginBLL _loginBLL = null; public LoginViewModel( IContainerProvider containerProvider, IEventAggregator ea, IUpgradeFileBLL systemBLL, ILoginBLL loginBLL,ILogHelper logHelper) { if (GlobalConfig.IsDesign) { this.UserName = "admin"; this.Password = "123"; } //_globalEntity = containerProvider.Resolve(); _systemBLL = systemBLL; _loginBLL = loginBLL; _logHelper = logHelper; // 应用启动时开始检查程序版本 this.IsLoading = true; this.LoadingMessage = "正在检查软件版本"; Task.Run(async () => { await Task.Delay(2000); try { var result = await _systemBLL.DiffFileListAsync(); if (result.Count > 0) { // 需要更新的时候,启动更新程序,关闭主程序 this.LoadingMessage = "发现新版本"; Process process = Process.Start( "Txgy.EWS.Client.Upgrade.exe", string.Join(" ", result.ToArray()) ); process.WaitForInputIdle(); ea.GetEvent().Publish(); } this.LoadingMessage = "已是最新版本"; //CommandManager.InvalidateRequerySuggested(); // 否则的话开始执行登录 this.IsLoading = false; this.IsCanLoading = true; _loginCommand.RaiseCanExecuteChanged(); } catch (Exception) { MessageBox.Show("系统检查更新失败!"); } }); } } }