using System.Data; using System.Windows; using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; using MySql.Data.MySqlClient; using SubCabinetSolution.Utils; using SubCabinetSolution.Views; namespace SubCabinetSolution.ViewModel { public class MainViewModel : ViewModelBase { /* * TextBlock双向绑定 */ public string _userName; /* * PasswordBox双向绑定 */ public string _userPassword; public string UserName { get { return _userName; } set { _userName = value; RaisePropertyChanged(() => UserName); } } public string UserPassword { get { return _userPassword; } set { _userPassword = value; RaisePropertyChanged(() => UserPassword); } } public RelayCommand<MainWindow> LoginToMainCommand { get; set; } public string VersionName { get; set; } private readonly MySqlConnection _connection; /// <summary> /// Initializes a new instance of the MainViewModel class. /// </summary> public MainViewModel() { this._connection = DataBaseManager.GetInstance().GetConnection(); this.LoginToMainCommand = new RelayCommand<MainWindow>(Login); this.VersionName = "版本号 Ver " + Application.ResourceAssembly.GetName().Version; } private void Login(MainWindow window) { if (string.IsNullOrWhiteSpace(_userName)) { MessageBox.Show("请输入用户名", "错误", MessageBoxButton.OK, MessageBoxImage.Error); return; } if (string.IsNullOrWhiteSpace(_userPassword)) { MessageBox.Show("请输入密码", "错误", MessageBoxButton.OK, MessageBoxImage.Error); return; } // 显示Loading对话框 // 前期可能是单机版本,连接数据库登录 // string publicKey = HttpRequestHandler.GetPublicKey(LocalConstant.BaseHttpUrl + "/config/baseConfig"); // PublicKeyModel keyModel = JsonConvert.DeserializeObject<PublicKeyModel>(publicKey); // Debug.WriteLine(keyModel.data.publicKey); if (_connection.State == ConnectionState.Open) { const string selectUser = "select * from sys_user"; var cmd = new MySqlCommand(selectUser, _connection); var reader = cmd.ExecuteReader(); while (reader.Read()) { var account = reader.GetString("ACCOUNT"); var secretKey = reader.GetString("PASSWORD"); //被加盐之后的密码 // TODO 解码得到密码 if (account != _userName || secretKey != _userPassword) continue; reader.Close(); var cabinetWindow = new CabinetWindow(); cabinetWindow.Show(); window.Close(); return; } MessageBox.Show("账号或者密码错误,无法登录", "错误", MessageBoxButton.OK, MessageBoxImage.Error); reader.Close(); } else { MessageBox.Show("数据库连接异常,请检查数据库配置", "错误", MessageBoxButton.OK, MessageBoxImage.Error); } } } }