添加项目文件。
This commit is contained in:
@@ -0,0 +1,296 @@
|
||||
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
|
||||
{
|
||||
/// <summary>
|
||||
/// 序号
|
||||
/// </summary>
|
||||
///
|
||||
[Display(Name = "序号")]
|
||||
public int Index { get; set; }
|
||||
/// <summary>
|
||||
/// 用户名
|
||||
/// </summary>
|
||||
///
|
||||
[Display(Name = "用户名")]
|
||||
public string UserName { get; set; }
|
||||
/// <summary>
|
||||
/// 密码
|
||||
/// </summary>
|
||||
///
|
||||
[Display(Name = "密码")]
|
||||
public string PassWord { get; set; }
|
||||
/// <summary>
|
||||
/// 权限
|
||||
/// </summary>
|
||||
///
|
||||
[Display(Name = "权限")]
|
||||
public Autuority Level { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 权限枚举
|
||||
/// </summary>
|
||||
public enum Autuority
|
||||
{
|
||||
管理员,//管理员
|
||||
工程师,//工程师
|
||||
操作员,//操作员
|
||||
Empty,
|
||||
}
|
||||
|
||||
public class UserHelper
|
||||
{
|
||||
private string filePath = string.Empty;
|
||||
public UserHelper(string path)
|
||||
{
|
||||
filePath = path;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 序列化到文件
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <param name="listUser"></param>
|
||||
/// <returns></returns>
|
||||
public bool SerializedUser(string path, List<User> 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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 反序列化到文件
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <returns></returns>
|
||||
public List<User> 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<User>;
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建超级用户
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <param name="listUser"></param>
|
||||
public void CheckSupperUser(string path, List<User> listUser)
|
||||
{
|
||||
if (!File.Exists(path))
|
||||
{
|
||||
User user = new User()
|
||||
{
|
||||
Index = 0,
|
||||
UserName = "Admin",
|
||||
PassWord = "Admin",
|
||||
Level = Autuority.管理员
|
||||
};
|
||||
listUser.Add(user);
|
||||
SerializedUser(path, listUser);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查重复性
|
||||
/// </summary>
|
||||
/// <param name="listUser"></param>
|
||||
/// <param name="userNmae"></param>
|
||||
/// <returns></returns>
|
||||
public bool CheckContainUser(List<User> listUser, string userNmae)
|
||||
{
|
||||
var user = from item in listUser
|
||||
where item.UserName == userNmae
|
||||
select item;
|
||||
if (user.Count() > 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加用户
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <param name="listUser"></param>
|
||||
/// <param name="user"></param>
|
||||
/// <returns></returns>
|
||||
public bool AddUser(string path, List<User> 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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <param name="listUser"></param>
|
||||
/// <param name="userNmae"></param>
|
||||
/// <returns></returns>
|
||||
public bool DeleteUser(string path, List<User> 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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 修改用户
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <param name="listUser"></param>
|
||||
/// <param name="user"></param>
|
||||
/// <returns></returns>
|
||||
public bool EditUser(string path, List<User> 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<User> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user