using JY.Inspection.Enums; using JY.Inspection.Mes; using JY.MES.Entity; using JY.MES.MES; using JY.Model.Enums; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.IO; using System.Linq; using System.Runtime.Serialization.Formatters.Binary; using System.Text; using System.Threading.Tasks; namespace JY.Inspection.Common { public class CurrentInfo { public static Autuority autuority = Autuority.Empty; public static bool LoginOut = false; } [Serializable] public class User { /// /// 序号 /// /// [Display(Name = "序号")] public int Index { get; set; } /// /// 用户名 /// /// [Display(Name = "用户名")] public string UserName { get; set; } /// /// 密码 /// /// [Display(Name = "密码")] public string PassWord { get; set; } [Display(Name = "权限")] public Autuority Level { get; set; } } /// /// 权限枚举 /// public enum Autuority { 管理员,//管理员 工程师,//工程师 操作员,//操作员 Empty, } public class UserHelper { private string filePath = string.Empty; public UserHelper(string path) { filePath = path; } /// /// 序列化到文件 /// /// /// /// public bool SerializedUser(string path, List listUser) { if (listUser == null) { return false; } BinaryFormatter format = new BinaryFormatter(); using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write)) { format.Serialize(fs, listUser); return true; } } /// /// 反序列化到文件 /// /// /// public List DeSerializedUser(string path) { BinaryFormatter format = new BinaryFormatter(); try { using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)) { object o = format.Deserialize(fs); return o as List; } } catch (Exception) { return null; } } /// /// 创建超级用户 /// /// /// public void CheckSupperUser(string path, List listUser) { if (!File.Exists(path)) { User user = new User() { Index = 0, UserName = "Admin", PassWord = "Admin", Level = Autuority.管理员 }; listUser.Add(user); SerializedUser(path, listUser); } } /// /// 检查重复性 /// /// /// /// public bool CheckContainUser(List listUser, string userNmae) { var user = from item in listUser where item.UserName == userNmae select item; if (user.Count() > 0) { return true; } return false; } /// /// 添加用户 /// /// /// /// /// public bool AddUser(string path, List listUser, User user) { try { if (user == null) { return false; } if (CheckContainUser(listUser, user.UserName)) { return false; } listUser.Add(user); SerializedUser(path, listUser); return true; } catch { return true; } } /// /// 删除 /// /// /// /// /// public bool DeleteUser(string path, List listUser, string userNmae) { try { if (listUser == null) { return false; } int index = 0; foreach (var item in listUser) { if (item.UserName == userNmae) { break; } index++; } if (index == 0) { return false; } listUser.RemoveAt(index); SerializedUser(path, listUser); return true; } catch { return false; } } /// /// 修改用户 /// /// /// /// /// public bool EditUser(string path, List listUser, User user) { try { if (listUser == null) { return false; } foreach (var item in listUser) { if (item.UserName == user.UserName) { item.PassWord = user.PassWord; item.Level = user.Level; SerializedUser(path, listUser); return true; } } return false; } catch { return false; } } public User CheckUserLogin(string path, string userName, string passWord, ref string strErr) { if (!File.Exists(path)) { return null; } List list = DeSerializedUser(path); if (list == null) { return null; } User user = new User(); if (string.IsNullOrEmpty(userName)) { var res1 = from item in list where item.PassWord == passWord select item; if (res1.Count() == 0) { return null; } foreach (var item in res1) { user.UserName = item.UserName; user.PassWord = item.PassWord; user.Level = item.Level; } } else { var res = from item in list where item.PassWord == passWord && item.UserName == userName select item; if (res.Count() == 0) { return null; } foreach (var item in res) { user.UserName = item.UserName; user.PassWord = item.PassWord; user.Level = item.Level; } } return user; } /// /// 使用MES员工信息接口校验登录信息 /// /// public User CheckMesUserLogin(string userName, string passWord, ref string strErr) { User user = null; ReqLoginMes req = new ReqLoginMes() { SiteCode = Global.systemConfig.siteCode, LineCode = Global.systemConfig.lineCode, EquipNum = Global.systemConfig.equipCode, UserName = userName, PassWord = passWord, UserCardID = string.Empty }; string strJson = JsonConvert.SerializeObject(req); MesCallResult result = new MESDataCombin().LoginMes(req); if (result.IsSuccess && result.OriResponse?.Rows.Length == 1) { // TODO 权限转化 string levelStr = result.OriResponse.Rows[0].PermissionLevel; bool resultLevel = Int32.TryParse(levelStr, out int level); if (!resultLevel) { strErr = $"权限转化失败,权限值为:{levelStr}"; return user; } user = new User() { UserName = result.OriResponse.Rows[0].Username, Level = level.ToEnum().MapperEnum() }; } else { strErr = string.IsNullOrEmpty(result.OriResponse?.Message) ? result.ErrorMessage : result.OriResponse?.Message; } return user; } } }