using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Newtonsoft.Json.Linq;
namespace CommonLibrary
{
///
/// 服务器的账户管理类,如果你采用这种方式存储,请参照本项目事例
///
/// 账户类,该类必须派生自UserAccount类
public class ServerAccounts : HslCommunication.BasicFramework.SoftFileSaveBase where T : UserAccount, new()
{
#region Constructor
///
/// 初始化构造方法
///
public ServerAccounts()
{
}
///
/// 初始化构造方法,将添加几个初始化账户
///
public ServerAccounts(IEnumerable accounts)
{
all_list_accounts.AddRange(accounts);
}
#endregion
#region Private Member
private List all_list_accounts = new List();
///
/// 一个简单的混合锁,相比Lock速度更快
///
private HslCommunication.SimpleHybirdLock hybirdLock = new HslCommunication.SimpleHybirdLock();
#endregion
#region Public Method
///
/// 更新指定账户的密码
///
///
///
public void UpdatePassword(string name, string password)
{
hybirdLock.Enter();
for (int i = 0; i < all_list_accounts.Count; i++)
{
if (name == all_list_accounts[i].UserName)
{
all_list_accounts[i].Password = password;
ILogNet?.WriteInfo(SoftResources.StringResouce.AccountModifyPassword + name);
break;
}
}
hybirdLock.Leave();
}
///
/// 更新指定账户的大小尺寸的头像MD5码
///
///
/// 大尺寸头像的MD5
/// 小尺寸头像的MD5
public void UpdatePortraitMD5(string name, string smallPortraitMD5, string largePortraitMD5)
{
hybirdLock.Enter();
for (int i = 0; i < all_list_accounts.Count; i++)
{
if (name == all_list_accounts[i].UserName)
{
all_list_accounts[i].SmallPortraitMD5 = smallPortraitMD5;
all_list_accounts[i].LargePortraitMD5 = largePortraitMD5;
ILogNet?.WriteInfo(SoftResources.StringResouce.AccountUploadPortrait + name);
break;
}
}
hybirdLock.Leave();
}
///
/// 筛选特定的账户信息
///
///
///
public List WhereAccounts(Func selector)
{
return all_list_accounts.Where(selector).ToList();
}
///
/// 将所有账户信息转换成另一种元素,并返回列表
///
/// 目标类型
/// 转换方法
/// 转换后的结果列表
public List ConvertAll(Converter converter)
{
return all_list_accounts.ConvertAll(converter);
}
///
/// 检查账户信息,并返回账户对象
///
/// 需要验证的用户名
/// 需要验证的密码
/// 登录的ip地址
/// 验证的结果对象
public T CheckAccount(string name, string code, string ipAddress, string way)
{
T result = new T()
{
UserName = name,
Password = code,
ForbidMessage = "用户名不存在!",
};
hybirdLock.Enter();
for (int i = 0; i < all_list_accounts.Count; i++)
{
T item = all_list_accounts[i];
if (item.UserName == name)
{
if (item.Password != code)
{
item.LoginFailedCount++;
result.ForbidMessage = "密码错误!";
break;
}
else
{
// 说明已经登录成功,需要进行进一步操作
item.LoginFrequency++;
result = item.DeepCopy();
// 下面两个数据应该是旧的数据
item.LastLoginIpAddress = ipAddress;
item.LastLoginTime = DateTime.Now;
item.LastLoginWay = way;
break;
}
}
}
hybirdLock.Leave();
return result;
}
///
/// 新增一个账户,如果账户名称已经存在,则返回False,注册成功返回True
///
/// 账户对象的JSON表示方式
/// 成功True,失败False
public bool AddNewAccount(string json_account)
{
T account = JObject.Parse(json_account).ToObject();
return AddNewAccount(account);
}
///
/// 新增一个账户,如果账户名称已经存在,则返回False,注册成功返回True
///
/// 账户对象
/// 成功True,失败False
public bool AddNewAccount(T account)
{
bool result = true;
hybirdLock.Enter();
for (int i = 0; i < all_list_accounts.Count; i++)
{
if (all_list_accounts[i].UserName == account.UserName)
{
result = false;
break;
}
}
all_list_accounts.Add(account);
ILogNet?.WriteInfo(SoftResources.StringResouce.AccountAddSuccess + account.UserName);
hybirdLock.Leave();
return result;
}
///
/// 删除一个账户信息,
///
/// 需要删除的账户的名称
public void DeleteAccount(string name)
{
hybirdLock.Enter();
for (int i = 0; i < all_list_accounts.Count; i++)
{
if (name == all_list_accounts[i].UserName)
{
all_list_accounts.RemoveAt(i);
ILogNet?.WriteInfo(SoftResources.StringResouce.AccountDeleteSuccess + name);
break;
}
}
hybirdLock.Leave();
}
///
/// 检查账户对象并返回账户的JSON字符串
///
/// 登录的用户名
/// 登录的密码
/// 检查的客户端的登录的ip地址
///
public string CheckAccountJson(string name, string code, string ipAddress, string way)
{
T result = CheckAccount(name, code, ipAddress, way);
return JObject.FromObject(result).ToString();
}
///
/// 获取所有的账户的JSON字符串
///
///
public string GetAllAccountsJson()
{
string result = string.Empty;
hybirdLock.Enter();
result = JArray.FromObject(all_list_accounts).ToString();
hybirdLock.Leave();
return result;
}
///
/// 从所有的账户的json数据加载账户
///
///
public void LoadAllAccountsJson(string json)
{
hybirdLock.Enter();
try
{
all_list_accounts = JArray.Parse(json).ToObject>();
}
catch (Exception ex)
{
ILogNet?.WriteException(SoftResources.StringResouce.AccountLoadFailed, ex);
}
hybirdLock.Leave();
}
#endregion
#region Override Method
///
/// 从字符串加载数据内容
///
///
public override void LoadByString(string content)
{
LoadAllAccountsJson(content);
}
///
/// 获取需要保存的数据内容
///
///
public override string ToSaveString()
{
return GetAllAccountsJson();
}
///
/// 使用加密规则从文件加载
///
public override void LoadByFile()
{
LoadByFile(m => m);
}
///
/// 使用加密规则保存到文件
///
public override void SaveToFile()
{
SaveToFile(m => m);
}
#endregion
}
}