黄学彪
2020-12-17 9f326f4000847e6167d8166fa2f6a66f53cb3734
ZigbeeApp/Shared/Phone/Common/Logic/HdlCommonLogic.cs
New file
@@ -0,0 +1,329 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Shared.Phone
{
    /// <summary>
    /// 一般的共通逻辑方法
    /// </summary>
    public class HdlCommonLogic
    {
        #region ■ 变量声明___________________________
        /// <summary>
        /// 界面相关的逻辑
        /// </summary>
        private static HdlCommonLogic m_Current;
        /// <summary>
        /// 界面相关的逻辑
        /// </summary>
        public static HdlCommonLogic Current
        {
            get
            {
                if (m_Current == null)
                {
                    m_Current = new HdlCommonLogic();
                }
                return m_Current;
            }
        }
        #endregion
        #region ■ 16进制转化_________________________
        /// <summary>
        /// 将16进制的文本翻译为正常文本
        /// </summary>
        /// <param name="hexText">16进制的文本</param>
        /// <param name="count">以多少个字节为一组</param>
        /// <returns></returns>
        public string TranslateHexadecimalIntoText(string hexText, int count = 2)
        {
            string textValue = string.Empty;
            while (hexText.Length > 0)
            {
                string temp = hexText.Substring(0, count);
                hexText = hexText.Substring(count);
                int value = Convert.ToInt32(temp, 16);
                textValue += ((char)value).ToString();
            }
            return textValue;
        }
        /// <summary>
        /// 将文本翻译为16进制的文本
        /// </summary>
        /// <param name="text">指定文本</param>
        /// <returns></returns>
        public string TranslateTextIntoHexadecimal(string text)
        {
            string textValue = string.Empty;
            foreach (char c in text)
            {
                int value = Convert.ToInt32(c);
                textValue += Convert.ToString(value, 16);
            }
            return textValue;
        }
        #endregion
        #region ■ 加密和解密_________________________
        /// <summary>
        /// 加密密码
        /// </summary>
        /// <param name="keys"></param>
        /// <param name="strPsw"></param>
        /// <returns></returns>
        public string EncryptPassword(string keys, string strPsw)
        {
            try
            {
                if (string.IsNullOrEmpty(strPsw) == true)
                {
                    return strPsw;
                }
                var des = new System.Security.Cryptography.DESCryptoServiceProvider();
                byte[] inputByteArray = Encoding.Default.GetBytes(strPsw);
                des.Key = ASCIIEncoding.ASCII.GetBytes(keys);
                des.IV = ASCIIEncoding.ASCII.GetBytes(keys);
                var ms = new System.IO.MemoryStream();
                var cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                StringBuilder ret = new StringBuilder();
                foreach (byte b in ms.ToArray())
                {
                    ret.AppendFormat("{0:X2}", b);
                }
                return ret.ToString().ToLower();
            }
            catch { return strPsw; }
        }
        /// <summary>
        /// 解密密码
        /// </summary>
        /// <param name="strPsw"></param>
        /// <returns></returns>
        public string DecryptPassword(string keys, string strPsw)
        {
            try
            {
                if (strPsw == string.Empty)
                {
                    return strPsw;
                }
                var des = new System.Security.Cryptography.DESCryptoServiceProvider();
                byte[] inputByteArray = new byte[strPsw.Length / 2];
                for (int x = 0; x < strPsw.Length / 2; x++)
                {
                    int i = (Convert.ToInt32(strPsw.Substring(x * 2, 2), 16));
                    inputByteArray[x] = (byte)i;
                }
                des.Key = ASCIIEncoding.ASCII.GetBytes(keys);
                des.IV = ASCIIEncoding.ASCII.GetBytes(keys);
                var ms = new System.IO.MemoryStream();
                var cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                StringBuilder ret = new StringBuilder();
                return System.Text.Encoding.Default.GetString(ms.ToArray());
            }
            catch { return strPsw; }
        }
        #endregion
        #region ■ 时间转换___________________________
        /// <summary>
        /// 将utc时间类型的字符串(这种格式:2019-11-11T11:31:01),转换为本地时间
        /// </summary>
        /// <param name="timeText">这种格式:2019-11-11T11:31:01</param>
        /// <returns></returns>
        public DateTime ConvertUtcTimeToLocalTime(string timeText)
        {
            var utcTime = Convert.ToDateTime(timeText);
            return TimeZoneInfo.ConvertTimeFromUtc(utcTime, TimeZoneInfo.Local);
        }
        /// <summary>
        /// 将utc时间类型的字符串(这种格式:1597302318861,13位数),转换为本地时间
        /// </summary>
        /// <param name="timeText">这种格式:1597302318861,13位数</param>
        /// <returns></returns>
        public DateTime ConvertUtcTimeToLocalTime2(string timeText)
        {
            DateTime dtStart = new DateTime(1970, 1, 1);
            long lTime = long.Parse(timeText + "0000");
            TimeSpan toNow = new TimeSpan(lTime);
            DateTime utcTime = dtStart.Add(toNow);
            return TimeZoneInfo.ConvertTimeFromUtc(utcTime, TimeZoneInfo.Local);
        }
        #endregion
        #region ■ 拼接超时信息_______________________
        /// <summary>
        /// 拼接网关回复超时的信息
        /// </summary>
        /// <param name="errorMsg">错误信息</param>
        /// <param name="resultData">网关返回的resultData,里面有【errorMessageBase】这个东西的那种对象</param>
        /// <param name="strResultData">网关返回的resultData,里面的【errorMessageBase】的值,设定有此值时,resultData无效</param>
        /// <param name="addBrackets">是否添加括号</param>
        /// <returns></returns>
        public string CombineGatewayTimeOutMsg(string errorMsg, object resultData, string strResultData = null, bool addBrackets = true)
        {
            if (resultData == null && strResultData == null)
            {
                return errorMsg;
            }
            string errorMsgBase = strResultData;
            if (errorMsgBase == null)
            {
                if (resultData is ReceiptGatewayResult)
                {
                    errorMsgBase = string.Empty;
                    if (((ReceiptGatewayResult)resultData).ErrorMsgDiv == 0)
                    {
                        errorMsgBase = "回复超时";
                    }
                }
                else
                {
                    Type myType = resultData.GetType();
                    object errorObj = myType.InvokeMember("errorMessageBase", System.Reflection.BindingFlags.GetField, null, resultData, null);
                    if (errorObj == null)
                    {
                        return errorMsg;
                    }
                    errorMsgBase = errorObj.ToString();
                }
            }
            if (errorMsgBase.Contains("回复超时") == false)
            {
                //只有回复超时的时候才会加上
                return errorMsg;
            }
            errorMsg += "\r\n";
            if (addBrackets == true)
            {
                //(网关回复超时,请稍后再试)
                return errorMsg + "(" + Language.StringByID(R.MyInternationalizationString.uGatewayResponseTimeOut) + ")";
            }
            else
            {
                //网关回复超时,请稍后再试
                return errorMsg + Language.StringByID(R.MyInternationalizationString.uGatewayResponseTimeOut);
            }
        }
        #endregion
        #region ■ 读取Resources文件夹的INI___________
        /// <summary>
        /// 读取Resources文件夹的INI内容
        /// </summary>
        /// <param name="i_iniFileName">Ini文件的名字</param>
        /// <returns></returns>
        public List<string> GetInitFileContent(string i_iniFileName)
        {
            System.IO.StreamReader streamReader = null;
            var listText = new List<string>();
            try
            {
#if iOS
                string textFile = Foundation.NSBundle.MainBundle.PathForResource(i_iniFileName, 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(i_iniFileName);
                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>
        /// 从一堆文字中,获取这一堆文字里面数字字符串的最长长度
        /// </summary>
        /// <param name="listText"></param>
        /// <returns></returns>
        public int GetNumberMaxLength(List<string> listText)
        {
            int maxLength = 0;
            foreach (var text in listText)
            {
                string value = string.Empty;
                foreach (var c in text)
                {
                    if (char.IsNumber(c) == true)
                    {
                        //数字
                        value += c.ToString();
                        continue;
                    }
                    else if (value != string.Empty)
                    {
                        //判断数字长度
                        if (maxLength <= value.Length)
                        {
                            maxLength = value.Length;
                        }
                        value = string.Empty;
                    }
                }
                //判断数字长度
                if (maxLength <= value.Length)
                {
                    maxLength = value.Length;
                }
            }
            return maxLength;
        }
        #endregion
    }
}