using Shared;
|
using System;
|
using System.Collections.Generic;
|
using System.Text;
|
|
namespace HDL_ON.Stan
|
{
|
/// <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="utcTimeText">utc时间,这种格式:2019-11-11T11:31:01</param>
|
/// <returns></returns>
|
public DateTime ConvertUtcTimeToLocalTime(string utcTimeText)
|
{
|
var utcTime = Convert.ToDateTime(utcTimeText);
|
return TimeZoneInfo.ConvertTimeFromUtc(utcTime, TimeZoneInfo.Local);
|
}
|
|
/// <summary>
|
/// 将utc时间类型的字符串(这种格式:1597302318861,13位数),转换为本地时间
|
/// </summary>
|
/// <param name="utcTimeText">utc时间,这种格式:1597302318861,13位数</param>
|
/// <returns></returns>
|
public DateTime ConvertUtcTimeToLocalTime2(string utcTimeText)
|
{
|
DateTime dtStart = new DateTime(1970, 1, 1);
|
long lTime = long.Parse(utcTimeText + "0000");
|
TimeSpan toNow = new TimeSpan(lTime);
|
DateTime utcTime = dtStart.Add(toNow);
|
|
return TimeZoneInfo.ConvertTimeFromUtc(utcTime, TimeZoneInfo.Local);
|
}
|
|
/// <summary>
|
/// 单纯的将时间类型的字符串(这种格式:1597302318861,13位数),转换时间类型
|
/// </summary>
|
/// <param name="timeText"></param>
|
/// <returns></returns>
|
public DateTime ConvertTextToTime(string timeText)
|
{
|
DateTime dtStart = new DateTime(1970, 1, 1);
|
long lTime = long.Parse(timeText + "0000");
|
TimeSpan toNow = new TimeSpan(lTime);
|
return dtStart.Add(toNow);
|
}
|
|
/// <summary>
|
/// 转换为日期文本(考虑中英文,返回格式:2021年9月1日(中文情况) Sept 1,2021(英文))
|
/// </summary>
|
/// <param name="i_month">月</param>
|
/// <param name="i_day">日</param>
|
/// <param name="i_year">年(可以省略)</param>
|
/// <returns></returns>
|
public string ConvertDayText(int i_month, int i_day, int i_year = -1)
|
{
|
//中文
|
if (Language.CurrentLanguage == "Chinese")
|
{
|
string txt = i_month + Language.StringByID(StringId.month) + i_day + Language.StringByID(StringId.day);
|
if (i_year != -1)
|
{
|
txt = i_year + Language.StringByID(StringId.Years) + txt;
|
}
|
return txt;
|
}
|
//英文
|
string yearText = i_year == -1 ? string.Empty : "," + i_year.ToString();
|
if (i_month == 1) { return "Jan " + i_day + yearText; }
|
if (i_month == 2) { return "Feb " + i_day + yearText; }
|
if (i_month == 3) { return "Mar " + i_day + yearText; }
|
if (i_month == 4) { return "Apr " + i_day + yearText; }
|
if (i_month == 5) { return "May " + i_day + yearText; }
|
if (i_month == 6) { return "Jun " + i_day + yearText; }
|
if (i_month == 7) { return "Jul " + i_day + yearText; }
|
if (i_month == 8) { return "Aug " + i_day + yearText; }
|
if (i_month == 9) { return "Sept " + i_day + yearText; }
|
if (i_month == 10) { return "Oct " + i_day + yearText; }
|
if (i_month == 11) { return "Nov " + i_day + yearText; }
|
if (i_month == 12) { return "Dec " + i_day + yearText; }
|
return string.Empty;
|
}
|
|
/// <summary>
|
/// 获取时间的翻译文本
|
/// </summary>
|
/// <param name="second"></param>
|
/// <param name="hourText">小时的文本</param>
|
/// <param name="minuText">分的文本</param>
|
/// <param name="secondText">秒的文本</param>
|
/// <returns></returns>
|
public string GetTimeString(int second, string hourText, string minuText, string secondText)
|
{
|
if (second == 0) { return string.Empty; }
|
|
string timeStr = string.Empty;
|
int hour = second / 3600;
|
int minu = second % 3600 / 60;
|
int sec = second % 60;
|
|
//中文
|
if (Language.CurrentLanguage == "Chinese")
|
{
|
if (hour > 0) { timeStr += hour + hourText; }
|
if (minu > 0) { timeStr += minu + minuText; }
|
if (sec > 0) { timeStr += sec + secondText; }
|
}
|
//英文
|
else
|
{
|
if (hour > 0) { timeStr += hour + "h"; }
|
if (minu > 0) { timeStr += minu + "min"; }
|
if (sec > 0) { timeStr += sec + "s"; }
|
}
|
return timeStr;
|
}
|
|
/// <summary>
|
/// 获取月的简写文本(考虑中英文,返回格式:月(中文情况) Sept.(英文))
|
/// </summary>
|
/// <param name="i_month">月</param>
|
/// <returns></returns>
|
public string GetMonthShortText(int i_month)
|
{
|
//中文
|
if (Language.CurrentLanguage == "Chinese")
|
{
|
return Language.StringByID(StringId.month);
|
}
|
//英文
|
if (i_month == 1) { return "Jan."; }
|
if (i_month == 2) { return "Feb."; }
|
if (i_month == 3) { return "Mar."; }
|
if (i_month == 4) { return "Apr."; }
|
if (i_month == 5) { return "May."; }
|
if (i_month == 6) { return "Jun."; }
|
if (i_month == 7) { return "Jul."; }
|
if (i_month == 8) { return "Aug."; }
|
if (i_month == 9) { return "Sept."; }
|
if (i_month == 10) { return "Oct."; }
|
if (i_month == 11) { return "Nov."; }
|
if (i_month == 12) { return "Dec."; }
|
return string.Empty;
|
}
|
|
/// <summary>
|
/// 获取小时的文本(中英文)
|
/// </summary>
|
/// <returns></returns>
|
public string GetHourText()
|
{
|
//中文
|
if (Language.CurrentLanguage == "Chinese")
|
{
|
//小时
|
return Language.StringByID(StringId.h);
|
}
|
//英文
|
return "h";
|
}
|
|
/// <summary>
|
/// 获取分钟的文本(中英文)
|
/// </summary>
|
/// <returns></returns>
|
public string GetMinuteText()
|
{
|
//中文
|
if (Language.CurrentLanguage == "Chinese")
|
{
|
//分
|
return Language.StringByID(StringId.m);
|
}
|
//英文
|
return "min";
|
}
|
|
/// <summary>
|
/// 获取分钟的文本(中英文)
|
/// </summary>
|
/// <returns></returns>
|
public string GetSecondText()
|
{
|
//中文
|
if (Language.CurrentLanguage == "Chinese")
|
{
|
//秒
|
return Language.StringByID(StringId.s);
|
}
|
//英文
|
return "s";
|
}
|
|
#endregion
|
|
#region ■ 系统剪切板_________________________
|
|
/// <summary>
|
/// 将文本复制到系统的剪切板
|
/// </summary>
|
/// <param name="i_text">需要复制进去的东西</param>
|
/// <param name="i_msg">复制成功之后,显示的文本,可以为null</param>
|
public void SetTextToShearPlate(string i_text, string i_msg)
|
{
|
#if __Android__
|
var cm = (Android.Content.ClipboardManager)Application.Activity.GetSystemService(Android.Content.Context.ClipboardService);
|
// 将文本内容放到系统剪贴板里。
|
cm.Text = i_text;
|
#endif
|
//显示tip消息
|
HdlMessageLogic.Current.ShowMassage(ShowMsgType.Tip, i_msg);
|
}
|
|
#endregion
|
|
#region ■ 一般方法___________________________
|
|
/// <summary>
|
/// 检测日期是否合法
|
/// </summary>
|
/// <param name="i_year">年</param>
|
/// <param name="i_month">月</param>
|
/// <param name="i_day">日</param>
|
/// <returns></returns>
|
public bool CheckDateIsRight(int i_year, int i_month, int i_day)
|
{
|
if (i_year <= 0) { i_year = DateTime.Now.Year; }
|
|
int two = DateTime.IsLeapYear(i_year) == true ? 29 : 28;
|
int[] arry = new int[] { 31, two, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
|
|
if (i_month < 1 || i_month > 12) { return false; }
|
|
return i_day <= arry[i_month - 1];
|
}
|
|
/// <summary>
|
/// 检测是否是纯数字
|
/// </summary>
|
/// <param name="i_text"></param>
|
/// <returns></returns>
|
public bool CheckIsNumber(string i_text)
|
{
|
foreach (var c in i_text)
|
{
|
if (char.IsNumber(c) == false)
|
{
|
return false;
|
}
|
}
|
return true;
|
}
|
|
/// <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
|
}
|
}
|