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;
}
}
}