黄学彪
2020-12-17 9f326f4000847e6167d8166fa2f6a66f53cb3734
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
using Shared.Phone.UserCenter;
using System;
using System.Collections.Generic;
using System.Text;
 
namespace Shared.Phone
{
    /// <summary>
    /// 消息的逻辑
    /// </summary>
    public class HdlMessageLogic
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 消息的逻辑
        /// </summary>
        private static HdlMessageLogic m_Current = null;
        /// <summary>
        /// 消息的逻辑
        /// </summary>
        public static HdlMessageLogic Current
        {
            get
            {
                if (m_Current == null)
                {
                    m_Current = new HdlMessageLogic();
                    //初始化消息列表
                    m_Current.InitHttpMessageEnum();
                }
                return m_Current;
            }
        }
 
        /// <summary>
        /// 消息列表 key:状态码  value:翻译名称
        /// </summary>
        private Dictionary<string, string> dicHttpMsg = null;
 
        #endregion
 
        #region ■ 初始化消息_________________________
 
        /// <summary>
        /// 初始化设备的模块ID的枚举
        /// </summary>
        private void InitHttpMessageEnum()
        {
            if (this.dicHttpMsg != null)
            {
                return;
            }
            this.dicHttpMsg = new Dictionary<string, string>();
 
            var listText = this.GetDeviceNameFileContent();
            foreach (var dataText in listText)
            {
                if (dataText == string.Empty || dataText.StartsWith(";") == true)
                {
                    //这是注释
                    continue;
                }
                string[] strArry1 = dataText.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
                if (strArry1.Length != 2)
                {
                    //非法设置
                    continue;
                }
                this.dicHttpMsg[strArry1[0].Trim()] = strArry1[1].Trim();
            }
        }
 
        /// <summary>
        /// 获取设备名字文件的内容
        /// </summary>
        /// <returns></returns>
        private List<string> GetDeviceNameFileContent()
        {
            System.IO.StreamReader streamReader = null;
            var listText = new List<string>();
            try
            {
#if iOS
                string textFile = Foundation.NSBundle.MainBundle.PathForResource("HttpMessage.ini", null);
                streamReader = new System.IO.StreamReader(textFile, Encoding.UTF8);
                string text;
                while ((text = streamReader.ReadLine()) != null)
                {
                    listText.Add(text.Trim());
                }
                return listText;
#endif
#if Android
                var stream = Application.Activity.Assets.Open("HttpMessage.ini");
                streamReader = new System.IO.StreamReader(stream, Encoding.UTF8);
                string text;
                while ((text = streamReader.ReadLine()) != null)
                {
                    listText.Add(text.Trim());
                }
                stream.Close();
                return listText;
#endif
            }
            catch
            {
                return listText;
            }
            finally
            {
                try
                {
                    streamReader?.Close();
                }
                catch
                {
                }
            }
        }
 
        #endregion
 
        #region ■ 获取云端消息列表___________________
 
        /// <summary>
        /// 获取云端消息列表(消息记录,可能会返回null)
        /// </summary>
        /// <returns></returns>
        public List<MessageRecordInfo> GetListMessageFromDb()
        {
            //如果没有极光id的话
            if (string.IsNullOrEmpty(Common.Config.Instance.PushId) == true)
            {
                return null;
            }
            var pra = new { pushId = Common.Config.Instance.PushId, homeId = Common.Config.Instance.Home.Id };
            var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("smart-footstone/app/message/list", RestSharp.Method.POST, pra, null, null, CheckMode.A账号权限, false, 5);
            if (result == null || result.Code != HttpMessageEnum.A成功)
            {
                return null;
            }
            return Newtonsoft.Json.JsonConvert.DeserializeObject<List<MessageRecordInfo>>(result.Data.ToString());
        }
 
        #endregion
 
        #region ■ 标记消息已读_______________________
 
        /// <summary>
        /// 标记云端消息已读
        /// </summary>
        /// <param name="i_strId">消息的主键</param>
        public bool SetTickIsRead(string i_strId)
        {
            var pra = new { msgId = i_strId };
            var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("smart-footstone/app/message/read", RestSharp.Method.POST, pra, null, null, CheckMode.A账号权限);
            if (result == null || result.Code != HttpMessageEnum.A成功)
            {
                return false;
            }
            return true;
        }
 
        /// <summary>
        /// 设置云端消息全部为已读
        /// </summary>
        public bool SetAllTickIsRead()
        {
            //如果没有极光id的话
            if (string.IsNullOrEmpty(Common.Config.Instance.PushId) == true)
            {
                return true;
            }
            var pra = new { pushId = Common.Config.Instance.PushId, homeId = Common.Config.Instance.Home.Id };
            var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("smart-footstone/app/message/read_all", RestSharp.Method.POST, pra, null, null, CheckMode.A账号权限);
            if (result == null || result.Code != HttpMessageEnum.A成功)
            {
                return false;
            }
            return true;
        }
 
        #endregion
 
        #region ■ 删除信息记录_______________________
 
        /// <summary>
        /// 删除云端信息记录
        /// </summary>
        /// <param name="i_strId">消息的主键</param>
        public bool DeleteCloundMessage(string i_strId)
        {
            var pra = new { msgId = i_strId };
            var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("smart-footstone/app/message/delete_by_id", RestSharp.Method.POST, pra, null, null, CheckMode.A账号权限);
            if (result == null || result.Code != HttpMessageEnum.A成功)
            {
                return false;
            }
            return true;
        }
 
        /// <summary>
        /// 删除云端全部的消息
        /// </summary>
        public bool DeleteAllCloundMessage()
        {
            //如果没有极光id的话
            if (string.IsNullOrEmpty(Common.Config.Instance.PushId) == true)
            {
                return true;
            }
            var pra = new { pushId = Common.Config.Instance.PushId, homeId = Common.Config.Instance.Home.Id };
            var result = HdlHttpLogic.Current.RequestResponseFromZigbeeHttps("smart-footstone/app/message/clear", RestSharp.Method.POST, pra, null, null, CheckMode.A账号权限);
            if (result == null || result.Code != HttpMessageEnum.A成功)
            {
                return false;
            }
            return true;
        }
 
        #endregion
 
        #region ■ 一般的方法_________________________
 
        /// <summary>
        /// 显示信息框
        /// </summary>
        /// <param name="msgType">信息类型</param>
        /// <param name="msg">信息</param>
        /// <param name="action">单击确认后执行的回调函数</param>
        /// <param name="buttonText">按钮的文本</param>
        /// <param name="i_waitTime">等待时间,单位为秒,设置确认按钮在多长时间后才能够点击</param>
        public void ShowMassage(ShowMsgType msgType, string msg, Action action = null, string buttonText = null, int i_waitTime = -1)
        {
            //空对象时,不显示
            if (string.IsNullOrEmpty(msg))
            {
                return;
            }
            HdlThreadLogic.Current.RunMain(() =>
            {
                var alert = new ShowMsgControl(msgType, msg, buttonText, null, i_waitTime);
                if (action != null)
                {
                    alert.ConfirmClickEvent += () =>
                    {
                        try
                        {
                            //回调函数
                            action?.Invoke();
                        }
                        catch (Exception ex)
                        {
                            //出现未知错误,数据丢失
                            this.ShowMassage(ShowMsgType.Error, Language.StringByID(R.MyInternationalizationString.uUnKnownError));
 
                            //Log出力
                            HdlLogLogic.Current.WriteLog(ex);
                        }
                        action = null;
                    };
                }
                alert.Show();
            });
        }
 
        /// <summary>
        /// 显示没有网络的tip消息
        /// </summary>
        /// <param name="i_mode">显示模式</param>
        public void ShowNotNetTipMsg(ShowNetCodeMode i_mode)
        {
            if (i_mode == ShowNetCodeMode.No)
            {
                //节约代码量,这样整而已,外面就不用判断了
                return;
            }
            //网关连接失败,请确认网络
            this.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.uGatewayIsNotLinkAndCheckNetwork));
        }
 
        /// <summary>
        /// 显示访问接口返回的状态码的翻译tip消息
        /// </summary>
        /// <param name="i_mode">显示模式</param>
        /// <param name="i_response">云端返回的东西</param>
        public void ShowNetCodeTipMsg(ShowNetCodeMode i_mode, ResponsePack i_response)
        {
            if (i_mode == ShowNetCodeMode.No || i_response.Code == string.Empty)
            {
                //节约代码量,这样整而已,外面就不用判断了
                return;
            }
            //翻译状态码
            var strMsg = this.TranslateHttpCode(i_response.Code);
            if (strMsg != null)
            {
                this.ShowMassage(ShowMsgType.Tip, strMsg);
            }
            else
            {
                this.ShowMassage(ShowMsgType.Tip, i_response.Message + "(" + i_response.Code + ")");
            }
        }
 
        /// <summary>
        /// 根据接口的状态码,翻译返回信息(出Log用的)
        /// </summary>
        /// <param name="requestName">接口</param>
        /// <param name="revertObj">云端返回的数据</param>
        /// <param name="bodyObj">承载在body里面的类对象</param>
        /// <param name="dicQueryTip">存放文档上标签为【query】的变量,无此标签,则请设置为null</param>
        /// <param name="dicPathTip">存放文档上标签为【path】的变量,无此标签,则请设置为null</param>
        /// <returns></returns>
        public string GetMsgByRequestName(string requestName, ResponsePack revertObj,
            object bodyObj, Dictionary<string, object> dicQueryTip, Dictionary<string, object> dicPathTip)
        {
            string errorMsg = "接口访问失败:" + requestName + " " + revertObj.Code + " " + revertObj.Message + "\r\n";
 
            errorMsg += "当前激活的界面:" + HdlFormLogic.Current.NowActionFormID + "\r\n";
            if (bodyObj != null)
            {
                //序列化对象
                try
                {
                    var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject(bodyObj);
                    errorMsg += "参数1:\r\n" + requestJson + "\r\n";
                }
                catch { errorMsg += "参数1:序列化异常!\r\n"; }
            }
            if (dicQueryTip != null)
            {
                //序列化对象
                try
                {
                    var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject(dicQueryTip);
                    errorMsg += "参数2:\r\n" + requestJson + "\r\n";
                }
                catch { errorMsg += "参数2:序列化异常!\r\n"; }
            }
            if (dicPathTip != null)
            {
                //序列化对象
                try
                {
                    var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject(dicPathTip);
                    errorMsg += "参数3:\r\n" + requestJson + "\r\n";
                }
                catch { errorMsg += "参数3:序列化异常!\r\n"; }
            }
            HdlLogLogic.Current.WriteLog(-1, errorMsg + "\r\n");
 
            return this.TranslateHttpCode(revertObj.Code);
        }
 
        /// <summary>
        /// 翻译云端的状态码(非在册的状态码,会直接返回状态码)
        /// </summary>
        /// <param name="i_code"></param>
        /// <returns></returns>
        public string TranslateHttpCode(string i_code)
        {
            if (this.dicHttpMsg.ContainsKey(i_code) == true)
            {
                return this.dicHttpMsg[i_code];
            }
            return null;
        }
 
        #endregion
    }
 
    /// <summary>
    /// 信息显示的类型
    /// </summary>
    public enum ShowMsgType
    {
        /// <summary>
        /// 普通提示类型
        /// </summary>
        Normal = 1,
        /// <summary>
        /// 确认类型
        /// </summary>
        Confirm = 2,
        /// <summary>
        /// 错误类型
        /// </summary>
        Error = 3,
        /// <summary>
        /// Tip类型
        /// </summary>
        Tip = 4,
        /// <summary>
        /// 提醒类型
        /// </summary>
        Remind = 5
    }
}