|
- using BPA.Message;
- using BPASmartClient.Helper;
- using FryPot_DosingSystem.Model;
- using Microsoft.Toolkit.Mvvm.ComponentModel;
- using Microsoft.Toolkit.Mvvm.Input;
- using Newtonsoft.Json;
- using System;
- using System.Collections.ObjectModel;
- using System.IO;
- using System.Windows;
-
- namespace FryPot_DosingSystem.ViewModel
- {
- public class AdministratorLoginViewModel : ObservableObject
- {
- /// <summary>
- /// 按钮内容修改:注册/修改
- /// </summary>
- private string _registOrChange;
- public string RegistOrChange { get { return _registOrChange; } set { _registOrChange = value; OnPropertyChanged(); } }
- public string Admin { get { return _admin; } set { _admin = value; OnPropertyChanged(); } }
- private string _admin;
-
- public string Password { get { return _password; } set { _password = value; OnPropertyChanged(); } }
- private string _password;
-
- public string ErrorMessage { get { return _errorMessage; } set { _errorMessage = value; OnPropertyChanged(); } }
- private string _errorMessage;
-
- public string SelectText { get { return _mSelectText; } set { _mSelectText = value; OnPropertyChanged(); } }
- private string _mSelectText;
-
- private UserInfo _userInfo;
- public UserInfo UserInfo { get { return _userInfo; }set { _userInfo = value;OnPropertyChanged(); } }
-
- private string _account;
- public string Account { get { return _account; } set { _account = value;OnPropertyChanged(); } }
-
- private string _txtpassword;
- public string Txtpassword { get { return _txtpassword; } set { _txtpassword = value; OnPropertyChanged(); } }
-
- public RelayCommand AdminLoginCommand { get; set; }
- public RelayCommand RegistCommand { get; set; }
-
- public ObservableCollection<string> permission { get; set; } = new ObservableCollection<string>();
-
- public AdministratorLoginViewModel()
- {
- permission.Add("管理员");
- permission.Add("操作员");
- permission.Add("观察员");
- permission.Add("技术员");
- SelectText = permission[0];
- var content = ActionManage.GetInstance.SendResult("ContentUpdate");
- if (content != null && content is string strContent)
- {
- RegistOrChange = strContent;
- }
- var loginInfo = ActionManage.GetInstance.SendResult("LoginInfo");
- if (loginInfo != null && loginInfo is UserInfo LoginInfo)
- {
- UserInfo = LoginInfo;
- if (UserInfo != null)
- {
- switch (UserInfo.Authority)
- {
- case Authority.管理员: SelectText = permission[0]; break;
- case Authority.操作员: SelectText = permission[1]; break;
- case Authority.观察员: SelectText = permission[2]; break;
- case Authority.技术员: SelectText = permission[3]; break;
- }
- Account = UserInfo.UserName;
- Txtpassword = UserInfo.Password;
- }
- }
- RegistCommand = new RelayCommand(() =>
- {
- if (RegistOrChange != null)
- {
- if (RegistOrChange == "注册")
- {
- if (Admin == null || Admin == string.Empty || Password == null || Password == string.Empty)
- {
- ErrorMessage = "注册信息不能为空!!!";
- }
- else
- {
- ep:
- string guid=Guid.NewGuid().ToString();//用户唯一ID
- var resultManage = JsonConvert.DeserializeObject<UserManage>(File.ReadAllText("LoginUp.hbl").AESDecrypt());
- if (resultManage != null && resultManage is UserManage manage)
- {
- int a= manage.userInfos.FindIndex(p => p.UserId == guid);
- if (a == -1)
- {
- switch (SelectText)
- {
- case "管理员": Global.userManager.userInfos.Add(new UserInfo() { Authority = Authority.管理员, UserName = Admin, Password = Password,UserId=guid }); break;
- case "技术员": Global.userManager.userInfos.Add(new UserInfo() { Authority = Authority.技术员, UserName = Admin, Password = Password,UserId = guid }); break;
- case "操作员": Global.userManager.userInfos.Add(new UserInfo() { Authority = Authority.操作员, UserName = Admin, Password = Password, UserId = guid }); break;
- case "观察员": Global.userManager.userInfos.Add(new UserInfo() { Authority = Authority.观察员, UserName = Admin, Password = Password, UserId = guid }); break;
- }
- File.WriteAllText("LoginUp.hbl", JsonConvert.SerializeObject(Global.userManager).AESEncrypt());
- var result = JsonConvert.DeserializeObject<UserManage>(File.ReadAllText("LoginUp.hbl").AESDecrypt());
- Global.userManager = result;
- ErrorMessage = "注册成功";
- }
- else
- {
- goto ep;
-
- }
- }
-
- }
- }
- if (RegistOrChange == "修改")
- {
- //执行密码修改操作
- try
- {
- if (UserInfo != null)
- {
- UserInfo info = Global.userManager.userInfos.Find(p => p.UserId == UserInfo.UserId);
- if (info != null)
- {
- switch (SelectText)
- {
- case "管理员": info.Authority = Authority.管理员; info.UserName = Admin; info.Password = Password; break;
- case "技术员": info.Authority = Authority.技术员; info.UserName = Admin; info.Password = Password; break;
- case "操作员": info.Authority = Authority.操作员; info.UserName = Admin; info.Password = Password; break;
- case "观察员": info.Authority = Authority.观察员; info.UserName = Admin; info.Password = Password; break;
- }
- File.WriteAllText("LoginUp.hbl", JsonConvert.SerializeObject(Global.userManager).AESEncrypt());
- var result = JsonConvert.DeserializeObject<UserManage>(File.ReadAllText("LoginUp.hbl").AESDecrypt());
- Global.userManager = result;
- ErrorMessage = "修改成功";
- }
- else
- {
- ErrorMessage = "修改失败";
- }
- }
- else
- {
- ErrorMessage = "修改失败";
- }
- }
- catch (Exception)
- {
- ErrorMessage = "修改失败";
- }
- }
- }
-
- });
- AdminLoginCommand = new RelayCommand(() =>
- {
- var rest = ActionManage.GetInstance.SendResult("LoginDosingSystem", $"{Admin}-={Password}-={SelectText}");
- if (rest != null && rest is string str)
- {
- Account = String.Empty;
- Txtpassword = String.Empty;
- ErrorMessage = str;
-
- }
- });
-
-
- }
- }
- }
|