黄学彪
2020-12-17 9f326f4000847e6167d8166fa2f6a66f53cb3734
ZigbeeApp/Shared/Phone/Common/Logic/HdlMessageLogic.cs
New file
@@ -0,0 +1,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
    }
}