using System;
using System.Collections.Generic;
namespace Shared.Common
{
[System.Serializable]
public class Config
{
#region ■ 需要保存的变量_____________________
///
/// 当前账号:刷新Token用的token(不用记录什么有效期,如果刷新失败,就踢人即可)
///
public string RefreshToken = string.Empty;
///
/// 将极光ID发给云端之后,云端返回的Token
///
public string PushId = string.Empty;
///
/// 账户登录成功时的时间
///
public DateTime LoginDateTime = DateTime.MinValue;
///
/// 当前登录的帐号
///
public string Account = string.Empty;
///
/// 添加到Token头部的东西(不要理它,只给底层使用)
///
public string HeaderPrefix = string.Empty;
///
/// 登陆账号的Guid,也是账号的userId
///
public string Guid = string.Empty;
///
/// 当前的住宅ID
///
public string HomeId = string.Empty;
///
/// 住宅文件列表
///
public List HomeFilePathList = new List();
#endregion
#region ■ 缓存变量___________________________
///
/// 服务器注册ID(可以说是极光ID)
///
[Newtonsoft.Json.JsonIgnore]
public string RegistrationID = string.Empty;
///
/// 判断当前时间点是否能够自动登录
///
public bool CanAutoLogin
{
get
{
return (DateTime.Now - LoginDateTime).Days < 7;
}
}
///
/// 是否同意隐私政策
///
[Newtonsoft.Json.JsonIgnore]
public bool AcceiptPolicy = false;
///
/// 当前帐号的Token(这个东西不用存了)
///
[Newtonsoft.Json.JsonIgnore]
public string Token = string.Empty;
///
/// 管理员获取到的主人的Token(这个东西不用存了)
///
[Newtonsoft.Json.JsonIgnore]
public string MasterToken = string.Empty;
///
/// 远程连接的Mqtt的客户端ID
///
[Newtonsoft.Json.JsonIgnore]
public string ConnEmqClientId = string.Empty;
///
/// 当前登录的账号是不是之前的账号
///
[Newtonsoft.Json.JsonIgnore]
public bool TheSameLoginAccount = false;
///
/// 安卓的系统返回按键能否按下(比如在备份还原时,不能按下返回键)
///
[Newtonsoft.Json.JsonIgnore]
public bool BackKeyCanClick = true;
///
/// 访问云端的语言(有些接口需要,目前是 CHINESE 或者 ENGLISH)
///
[Newtonsoft.Json.JsonIgnore]
public string HttpLanguage = "CHINESE";
///
/// 当前住宅
///
[Newtonsoft.Json.JsonIgnore]
private House m_Home = null;
///
/// 当前住宅
///
/// The home.
[Newtonsoft.Json.JsonIgnore]
public House Home
{
get
{
if (m_Home != null)
{
return m_Home;
}
m_Home = Phone.HdlResidenceLogic.Current.GetHouseByHouseId(HomeId);
if (m_Home == null)
{
m_Home = new House();
}
return m_Home;
}
set
{
m_Home = value;
}
}
///
/// 全路径
/// 使用方法: FullPath + FileName
///
/// The full path.
[Newtonsoft.Json.JsonIgnore]
public string FullPath
{
get
{
return System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Instance.Guid, Instance.HomeId);
}
}
#endregion
#region ■ 初始化_____________________________
///
/// Config文件名
///
private const string fileName = "Config.json";
private static Config config = null;
///
/// Config对象
///
public static Config Instance
{
get
{
if (config == null)
{
ReFresh();
}
return config;
}
}
///
/// 刷新
///
private static void ReFresh()
{
var file = Shared.IO.FileUtils.ReadFile(fileName);
if (file != null && file.Length > 0)
{
config = Newtonsoft.Json.JsonConvert.DeserializeObject(System.Text.Encoding.UTF8.GetString(file));
//解密
config.RefreshToken = Phone.HdlCommonLogic.Current.DecryptPassword("hD1(La3o", config.RefreshToken);
}
else
{
config = new Config();
}
}
#endregion
#region ■ 保存_______________________________
///
/// 保存当前对象
///
public void Save()
{
//加密,不能保存明码
var oldToken1 = this.RefreshToken;
this.RefreshToken = Phone.HdlCommonLogic.Current.EncryptPassword("hD1(La3o", oldToken1);
var bytes = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this));
Shared.IO.FileUtils.WriteFileByBytes(fileName, bytes);
this.RefreshToken = oldToken1;
}
#endregion
}
}