using System;
using System.Collections.Generic;
using System.Linq;
using Shared.SimpleControl;
namespace Shared
{
[System.Serializable]
public class CommonConfig
{
public static readonly string ConfigFile = "CommonConfig";
///
/// 锁定事件
///
static readonly int LOCK_TIME = 5;
///
/// 锁定账号列表
///
public List lockList = new List ();
///
/// 接口类的返回信息
///
static CommonConfig m_Current = null;
///
/// 接口类的返回信息
///
public static CommonConfig Current
{
get
{
if (m_Current == null)
{
try
{
var APIInfoConfigBytes = IO.FileUtils.ReadFile(ConfigFile);
var APIInfoConfigString = CommonPage.MyEncodingUTF8.GetString(APIInfoConfigBytes);
CommonConfig temp = null;
if (APIInfoConfigString != null)
{
temp = Newtonsoft.Json.JsonConvert.DeserializeObject (APIInfoConfigString);
}
if (temp == null)
{
m_Current = new CommonConfig { };
}
else
{
m_Current = temp;
}
}
catch
{
m_Current = new CommonConfig { };
}
}
return m_Current;
}
}
byte[] GetAPIInfoConfigBytes()
{
return CommonPage.MyEncodingUTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this));
}
//public void Refresh()
//{
// m_Current = null;
// Save();
//}
public void Save()
{
IO.FileUtils.WriteFileByBytes(ConfigFile, GetAPIInfoConfigBytes());
}
#region 密码锁定操作方法
///
/// 判断当前账号是否锁定
///
///
public bool CheckIfLock (string account)
{
var lockAccount = lockList.Where (obj => obj.Account == account).FirstOrDefault ();
//var lockAccount =lockList.Find ((obj) => obj.Account == account);
if (lockAccount == null) {
LockAccount lockAccountNew = new LockAccount () { Account = account, errorCount = 0, lockTime = DateTime.MinValue, ifLock = false };
lockList.Add (lockAccountNew);
return false;
} else {
//判断是否小于5分钟
if (lockAccount.lockTime.AddMinutes (LOCK_TIME) > DateTime.Now) {
//是,判断是否锁定
if (lockAccount.ifLock) {
int unlocktime = LOCK_TIME - (DateTime.Now.Minute - lockAccount.lockTime.Minute);
new Alert ("", $"The current login account has been locked. Please try again in {unlocktime} minutes.", Language.StringByID (SimpleControl.R.MyInternationalizationString.Close)).Show ();
return true;
} else {
return false;
}
} else {
//否,解锁,重置参数
lockAccount.errorCount = 0;
lockAccount.lockTime = DateTime.Now;
lockAccount.ifLock = false;
Save ();
return false;
}
}
}
///
/// 错误锁定
///
///
public bool ErrorLockListUpdate (string account)
{
var lockAccount = lockList.Where (obj => obj.Account == account).FirstOrDefault ();
if (lockAccount == null) {
LockAccount lockAccountNew = new LockAccount () { Account = account, errorCount = 1, lockTime = DateTime.MinValue, ifLock = false };
lockList.Add (lockAccountNew);
} else {
if (lockAccount.errorCount < 5) {
//错误次数少于5,提示密码错误并且错误次数加一
int count = lockAccount.errorCount + 1;
lockAccount.errorCount = count;
//lockAccount.lockTime = DateTime.Now;
lockAccount.ifLock = false;
Save ();
} else {
//错误次数大于5,设备修改成锁定,设置锁定时间,重置错误次数
lockAccount.errorCount = 0;
lockAccount.lockTime = DateTime.Now;
lockAccount.ifLock = true;
Save ();
MainPage.ShowAlertOnMainThread ($"The current login account has been locked. Please try again in {LOCK_TIME} minutes.");
return true;
}
}
return false;
}
#endregion
}
///
/// 锁定的账号信息
///
/// [Serializable]
[Serializable]
public class LockAccount
{
///
/// 账号
///
public string Account { get; set; }
///
/// 锁定次数
///
public int errorCount { get; set; }
///
/// 锁定时间
///
public DateTime lockTime { get; set; }
///
/// ifLock
///
public bool ifLock { get; set; }
}
}