黄学彪
2020-12-16 0d9f64668fd7350d6a21fd157e32009a96d98134
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using System;
using System.Collections.Generic;
 
namespace Shared.Common
{
    [System.Serializable]
    public class Config
    {
        #region ■ 需要保存的变量_____________________
 
        /// <summary>
        /// 当前账号:刷新Token用的token(不用记录什么有效期,如果刷新失败,就踢人即可)
        /// </summary>
        public string RefreshToken = string.Empty;
        /// <summary>
        /// 将极光ID发给云端之后,云端返回的Token
        /// </summary>
        public string PushId = string.Empty;
        /// <summary>
        /// 账户登录成功时的时间
        /// </summary>
        public DateTime LoginDateTime = DateTime.MinValue;
        /// <summary>
        /// 当前登录的帐号
        /// </summary>
        public string Account = string.Empty;
        /// <summary>
        /// 添加到Token头部的东西(不要理它,只给底层使用)
        /// </summary>
        public string HeaderPrefix = string.Empty;
        /// <summary>
        /// 登陆账号的Guid,也是账号的userId
        /// </summary>
        public string Guid = string.Empty;
        /// <summary>
        /// 当前的住宅ID
        /// </summary>
        public string HomeId = string.Empty;
        /// <summary>
        /// 住宅文件列表
        /// </summary>
        public List<string> HomeFilePathList = new List<string>();
 
        #endregion
 
        #region ■ 缓存变量___________________________
 
        /// <summary>
        /// 服务器注册ID(可以说是极光ID)
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public string RegistrationID = string.Empty;
        /// <summary>
        /// 判断当前时间点是否能够自动登录
        /// </summary>
        public bool CanAutoLogin
        {
            get
            {
                return (DateTime.Now - LoginDateTime).Days < 7;
            }
        }
        /// <summary>
        /// 是否同意隐私政策
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public bool AcceiptPolicy = false;
        /// <summary>
        /// 当前帐号的Token(这个东西不用存了)
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public string Token = string.Empty;
        /// <summary>
        /// 管理员获取到的主人的Token(这个东西不用存了)
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public string MasterToken = string.Empty;
        /// <summary>
        /// 远程连接的Mqtt的客户端ID
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public string ConnEmqClientId = string.Empty;
        /// <summary>
        /// 当前登录的账号是不是之前的账号
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public bool TheSameLoginAccount = false;
        /// <summary>
        /// 安卓的系统返回按键能否按下(比如在备份还原时,不能按下返回键)
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public bool BackKeyCanClick = true;
        /// <summary>
        /// 访问云端的语言(有些接口需要,目前是 CHINESE 或者 ENGLISH)
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public string HttpLanguage = "CHINESE";
        /// <summary>
        /// 当前住宅
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        private House m_Home = null;
        /// <summary>
        /// 当前住宅
        /// </summary>
        /// <value>The home.</value>
        [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;
            }
        }
        /// <summary>
        /// 全路径
        /// <para> 使用方法: FullPath + FileName </para>
        /// </summary>
        /// <value>The full path.</value>
        [Newtonsoft.Json.JsonIgnore]
        public string FullPath
        {
            get
            {
                return System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Instance.Guid, Instance.HomeId);
            }
        }
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// Config文件名
        /// </summary>
        private const string fileName = "Config.json";
 
        private static Config config = null;
        /// <summary>
        /// Config对象
        /// </summary>
        public static Config Instance
        {
            get
            {
                if (config == null)
                {
                    ReFresh();
                }
                return config;
            }
        }
 
        /// <summary>
        /// 刷新
        /// </summary>
        private static void ReFresh()
        {
            var file = Shared.IO.FileUtils.ReadFile(fileName);
            if (file != null && file.Length > 0)
            {
                config = Newtonsoft.Json.JsonConvert.DeserializeObject<Config>(System.Text.Encoding.UTF8.GetString(file));
                //解密
                config.RefreshToken = Phone.HdlCommonLogic.Current.DecryptPassword("hD1(La3o", config.RefreshToken);
            }
            else
            {
                config = new Config();
            }
        }
 
        #endregion
 
        #region ■ 保存_______________________________
 
        /// <summary>
        /// 保存当前对象
        /// </summary>
        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
    }
}