using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.CommandWpf; using GalaSoft.MvvmLight.Ioc; using GalaSoft.MvvmLight.Messaging; using GalaSoft.MvvmLight.Views; using MahApps.Metro.Controls.Dialogs; using Microsoft.Practices.ServiceLocation; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using SoftArcs.WPFSmartLibrary.SmartUserControls; using System; using System.Collections.Generic; using System.Configuration; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using Bro.UI.Model; using Bro.Common.Helper; namespace Bro.UI.ViewModel { public class LoginViewModel : ViewModelBase { #region IDialogService public IDialogCoordinator Dialog { get { return SimpleIoc.Default.GetInstance("Login"); } } #endregion #region Public Properties private string version = ""; public string Version { get => version; set => Set(ref version, value); } private string userName = ""; public string UserName { get => userName; set => Set(ref userName, value); } private string password = ""; public string Password { get => password; set => Set(ref password, value); } private string userImageSource = ""; public string UserImageSource { get => userImageSource; set => Set(ref userImageSource, value); } private List productionCodes = new List(); public List ProductionCodes { get => productionCodes; set => Set(ref productionCodes, value); } private string selectedProductionCode = ""; public string SelectedProductionCode { get => selectedProductionCode; set => Set(ref selectedProductionCode, value); } #endregion public RelayCommand Cmmd_Submit { get; set; } public LoginViewModel() { Version = " V" + Assembly.GetExecutingAssembly().GetName().Version.ToString(); //string codesStr = ConfigurationManager.AppSettings["ProductionCodes"]; //if (!string.IsNullOrWhiteSpace(codesStr)) //{ // ProductionCodes = JsonConvert.DeserializeObject>(codesStr); // SelectedProductionCode = ProductionCodes[0]; //} //string configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Setting.json"); //if (File.Exists(configPath)) //{ // using (StreamReader reader = new StreamReader(configPath, System.Text.Encoding.UTF8)) // { // string dataStr = reader.ReadToEnd(); // JObject data = JsonConvert.DeserializeObject(dataStr); // ProductionCodes = JsonConvert.DeserializeObject>(data.Value("ProductionCodes")); // } //} ProductionCodes = SettingHelper.GetProductionCodes(); SelectedProductionCode = ProductionCodes[0]; Cmmd_Submit = new RelayCommand(OnLoginSubmit, CanLoginSubmit); } private bool CanLoginSubmit(SmartLoginOverlay arg) { return !(string.IsNullOrWhiteSpace(UserName) || string.IsNullOrWhiteSpace(Password)); } private void OnLoginSubmit(SmartLoginOverlay obj) { if (ValidateUser(this.UserName, this.Password)) { SimpleIoc.Default.Register(() => { LoginInfo info = new LoginInfo(); info.UserName = UserName; info.ProductionCode = SelectedProductionCode; return info; }, true); Messenger.Default.Send(null, "LoginOK"); } else { Dialog.ShowMessageAsync(this, "登录失败", "用户名或密码错误,登录失败", MessageDialogStyle.Affirmative).ContinueWith(t => { UserName = Password = ""; }); } } private bool ValidateUser(string username, string password) { return userName.ToUpper() == "ADMIN" && password.ToUpper() == "ADMIN"; } } }