From 9f326f4000847e6167d8166fa2f6a66f53cb3734 Mon Sep 17 00:00:00 2001 From: 黄学彪 <hxb@hdlchina.com.cn> Date: 星期四, 17 十二月 2020 09:07:13 +0800 Subject: [PATCH] 新云端Ver1.3 --- ZigbeeApp/Shared/Phone/Common/Logic/HdlCommonLogic.cs | 329 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 329 insertions(+), 0 deletions(-) diff --git a/ZigbeeApp/Shared/Phone/Common/Logic/HdlCommonLogic.cs b/ZigbeeApp/Shared/Phone/Common/Logic/HdlCommonLogic.cs new file mode 100644 index 0000000..f193271 --- /dev/null +++ b/ZigbeeApp/Shared/Phone/Common/Logic/HdlCommonLogic.cs @@ -0,0 +1,329 @@ +锘縰sing 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杩涘埗鐨勬枃鏈炕璇戜负姝e父鏂囨湰 + /// </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> + /// 瑙e瘑瀵嗙爜 + /// </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> + /// 灏唘tc鏃堕棿绫诲瀷鐨勫瓧绗︿覆(杩欑鏍煎紡: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> + /// 灏唘tc鏃堕棿绫诲瀷鐨勫瓧绗︿覆(杩欑鏍煎紡: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">缃戝叧杩斿洖鐨剅esultData,閲岄潰鏈夈�恊rrorMessageBase銆戣繖涓笢瑗跨殑閭g瀵硅薄</param> + /// <param name="strResultData">缃戝叧杩斿洖鐨剅esultData,閲岄潰鐨勩�恊rrorMessageBase銆戠殑鍊�,璁惧畾鏈夋鍊兼椂锛宺esultData鏃犳晥</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 + } +} -- Gitblit v1.8.0