using System.Windows; using System.Collections.ObjectModel; using System; using System.Windows.Input; using MaterialDesignThemes.Wpf; using HighWayIot.Config; using GalaSoft.MvvmLight; using HighWayIot.Repository.service; using Aucma.Scada.Model.domain; using Admin.Core.Common; using HighWayIot.Common; using System.Text.Json; using Aucma.Scada.UI.Page.Model; using Aucma.Scada.Business; using HighWayIot.Log4net; namespace Aucma.Scada.UI.viewModel { public partial class LoginPageViewModel : ViewModelBase { private LogHelper logHelper = LogHelper.Instance; private RegisterServices registerServices = RegisterServices.Instance; protected readonly IBaseTeamMembersServices _baseTeamMembersServices; private AppConfig appConfig = AppConfig.Instance; public LoginPageViewModel() { _baseTeamMembersServices = registerServices.GetService(); AddTeamData(); } #region 登录 /// /// 登录 /// /// 选择的班组信息 /// 用户名 /// 密码 public void Login(BaseTeamMembers team, string userName, string passWord, object parameter) { try { //获取Token string url = $"http://10.100.72.10:8080/login"; var content = new { username = userName, password = passWord }; var loginResult = HttpHelper.Post(url, JsonChange.Instance.ModeToJson(content));//发送用户名密码给API Result result = JsonSerializer.Deserialize(loginResult); if (result == null) { MessageBox.Show("登录失败!", "系统提醒"); return; } if (result.code == 200) { // 存储账号信息 appConfig.TeamCode = team.TeamCode; appConfig.TeamName = team.TeamName; appConfig.Account = userName; logHelper.Info($"登陆成功,班组代码{appConfig.TeamCode};班组名称:{appConfig.TeamName};用户名{appConfig.Account}"); //跳转 var window = parameter as LoginPageView; if (window == null) return; window.Hide(); MainWindow mainPage = MainWindow.Instance; mainPage.Show(); window.Close(); } else { MessageBox.Show($"登录失败!{result.msg}", "系统提醒"); logHelper.Error($"班组代码{appConfig.TeamCode};班组名称:{appConfig.TeamName};用户名{appConfig.Account}登录失败!{result.msg}"); return; } } catch (Exception ex) { // log.Error($"登录异常:{ex.Message}"); } } #endregion #region 关闭当前界面 /// /// 关闭当前界面 /// /// public void CloseWindow(object parameter) { var window = parameter as Window; if (window == null) return; window.Close(); } #endregion #region 班组数据 /// /// 班组数据 /// private ObservableCollection _teamMembersList = new ObservableCollection(); public ObservableCollection TeamMembersList { get { return this._teamMembersList; } set { _teamMembersList = value; RaisePropertyChanged(); } } #endregion #region 班组添加到集合中 public void AddTeamData() { var baseTeamMembersList = _baseTeamMembersServices.getTeam(); foreach (var item in baseTeamMembersList) { TeamMembersList.Add(item); } } #endregion } }