using System;
|
using Shared;
|
using System.Text.RegularExpressions;
|
using System.Text;
|
|
namespace HDL_ON
|
{
|
/// <summary>
|
/// 常用工具类
|
/// </summary>
|
public static class Utlis
|
{
|
/// <summary>
|
/// 全局打印
|
/// </summary>
|
public static void WriteLine(object mes)
|
{
|
#if DEBUG
|
Console.WriteLine(mes);
|
#endif
|
}
|
|
/// <summary>
|
/// 弹窗提示
|
/// </summary>
|
/// <param name="mes"></param>
|
public static void ShowAlertOnMainThread(string mes)
|
{
|
Application.RunOnMainThread(() => {
|
new Alert("", mes, Language.StringByID(StringId.Close)).Show();
|
});
|
}
|
|
/// <summary>
|
/// ShowTip
|
/// </summary>
|
/// <param name="mes"></param>
|
/// <param name="closeTime">关闭时间</param>
|
public static void ShowTip(String mes, int closeTime = 2)
|
{
|
var tip = new Tip()
|
{
|
Text = mes,
|
CloseTime = closeTime,
|
Direction = AMPopTipDirection.None
|
};
|
tip.Show(MainPage.BaseView);
|
}
|
|
|
#region ■ 各种正则检测_______________________
|
/// <summary>
|
/// 用于中国大陆验证手机号正则表达式
|
/// 以1开头,11位
|
/// </summary>
|
public static string PhoneRegexStr = "^[1][0-9]{10}$";
|
/// <summary>
|
/// 用于验证非中国大陆手机号正则表达式
|
/// </summary>
|
public static string PhoneForForeignRegexStr = "^[0-9]*$";
|
/// <summary>
|
/// 用于验证邮箱正则表达式
|
/// </summary>
|
public static string EmailRegexStr = "^\\s*([A-Za-z0-9_-]+(\\.\\w+)*@(\\w+\\.)+\\w{2,5})\\s*$";
|
|
/// <summary>
|
/// 判断是否包含大写字母
|
/// </summary>
|
/// <returns><c>true</c>, if contain upper was checked, <c>false</c> otherwise.</returns>
|
/// <param name="value">Value.</param>
|
public static bool CheckContainUpper(string value)
|
{
|
Regex reg = new Regex("[A-Z]+");
|
return reg.IsMatch(value);
|
}
|
|
/// <summary>
|
/// 判断是否包含小写字母
|
/// </summary>
|
/// <returns><c>true</c>, if contain lower was checked, <c>false</c> otherwise.</returns>
|
/// <param name="value">Value.</param>
|
public static bool CheckContainLower(string value)
|
{
|
Regex reg = new Regex("[a-z]+");
|
return reg.IsMatch(value);
|
}
|
|
/// <summary>
|
/// 判断是否包含数字
|
/// </summary>
|
/// <returns><c>true</c>, if contain lower was checked, <c>false</c> otherwise.</returns>
|
/// <param name="value">Value.</param>
|
public static bool CheckContainNum(string value)
|
{
|
Regex reg = new Regex("[0-9]+");
|
return reg.IsMatch(value);
|
}
|
|
/// <summary>
|
/// 判断是否包含符号
|
/// </summary>
|
/// <returns><c>true</c>, if contain lower was checked, <c>false</c> otherwise.</returns>
|
/// <param name="value">Value.</param>
|
public static bool CheckContainSymbol(string value)
|
{
|
Regex reg = new Regex("([^a-z0-9A-Z])+");
|
return reg.IsMatch(value);
|
}
|
|
/// <summary>
|
/// 检测邮箱是否合法
|
/// </summary>
|
/// <param name="email"></param>
|
/// <returns></returns>
|
public static bool CheckEmail(string email)
|
{
|
Regex reg = new Regex(EmailRegexStr);
|
return reg.IsMatch(email);
|
}
|
|
/// <summary>
|
/// 检测手机号是否合法
|
/// </summary>
|
/// <param name="phoneNumber">手机号</param>
|
/// <param name="areaCode">地区代码</param>
|
/// <returns></returns>
|
public static bool CheckPhoneNumber(string phoneNumber, string areaCode)
|
{
|
//校验外国手机号
|
if (areaCode != "86")
|
{
|
Regex reg = new Regex(PhoneForForeignRegexStr);
|
return reg.IsMatch(phoneNumber);
|
}
|
else
|
{
|
//校验国内手机号
|
Regex reg = new Regex(PhoneRegexStr);
|
return reg.IsMatch(phoneNumber);
|
}
|
}
|
|
#endregion
|
|
/// <summary>
|
/// TextButton
|
/// 根据文本,计算按钮它实际的宽度
|
/// </summary>
|
/// <param name="btn"></param>
|
/// <param name="extendWidth">补充宽度</param>
|
/// <returns></returns>
|
public static int GetRealWidthByTextButton(Button btn, int extendWidth = 1)
|
{
|
if (string.IsNullOrEmpty(btn.Text)) { return Application.GetRealWidth(extendWidth); }
|
|
return btn.GetTextWidth() + Application.GetRealWidth(extendWidth);
|
}
|
|
/// <summary>
|
/// 根据文本,计算按钮它实际的宽度
|
/// </summary>
|
/// <returns></returns>
|
public static int GetRealWidthByText(Button btn)
|
{
|
if (string.IsNullOrEmpty(btn.Text)) { return Application.GetRealWidth(4); }
|
#if __IOS__
|
//需要增加一个误差值
|
return btn.GetTextWidth() + Application.GetRealWidth(8);
|
#else
|
//需要增加一个误差值
|
return btn.GetTextWidth() + Application.GetRealWidth(4);
|
#endif
|
}
|
|
|
/// <summary>
|
/// 生成随机字符串
|
/// </summary>
|
/// <param name="length">字符串的长度</param>
|
/// <returns></returns>
|
public static string CreateRandomString(int length)
|
{
|
// 创建一个StringBuilder对象存储密码
|
StringBuilder sb = new StringBuilder();
|
//使用for循环把单个字符填充进StringBuilder对象里面变成14位密码字符串
|
for (int i = 0; i < length; i++)
|
{
|
Random random = new Random(Guid.NewGuid().GetHashCode());
|
//随机选择里面其中的一种字符生成
|
switch (random.Next(3))
|
{
|
case 0:
|
//调用生成生成随机数字的方法
|
sb.Append(createNum());
|
break;
|
case 1:
|
//调用生成生成随机小写字母的方法
|
sb.Append(createSmallAbc());
|
break;
|
case 2:
|
//调用生成生成随机大写字母的方法
|
sb.Append(createBigAbc());
|
break;
|
}
|
}
|
return sb.ToString();
|
}
|
|
/// <summary>
|
/// 生成单个随机数字
|
/// </summary>
|
static int createNum()
|
{
|
Random random = new Random(Guid.NewGuid().GetHashCode());
|
int num = random.Next(10);
|
return num;
|
}
|
|
/// <summary>
|
/// 生成单个大写随机字母
|
/// </summary>
|
static string createBigAbc()
|
{
|
//A-Z的 ASCII值为65-90
|
Random random = new Random(Guid.NewGuid().GetHashCode());
|
int num = random.Next(65, 91);
|
string abc = Convert.ToChar(num).ToString();
|
return abc;
|
}
|
|
/// <summary>
|
/// 生成单个小写随机字母
|
/// </summary>
|
static string createSmallAbc()
|
{
|
//a-z的 ASCII值为97-122
|
Random random = new Random(Guid.NewGuid().GetHashCode());
|
int num = random.Next(97, 123);
|
string abc = Convert.ToChar(num).ToString();
|
return abc;
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="unixTimeStamp"></param>
|
/// <returns></returns>
|
public static DateTime UnixToDateTime(long unixTimeStamp)
|
{
|
DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); // 当地时区
|
return startTime.AddSeconds(unixTimeStamp);
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="unixTimeStamp"></param>
|
/// <returns></returns>
|
public static long DateTimeToUnix(DateTime dateTime)
|
{
|
DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); // 当地时区
|
return (long)(dateTime - startTime).TotalSeconds; // 相差秒数
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="unixTimeStamp"></param>
|
/// <returns></returns>
|
public static DateTime UnixToDateTimeMS(long unixTimeStamp)
|
{
|
DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); // 当地时区
|
return startTime.AddMilliseconds(unixTimeStamp);
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="unixTimeStamp"></param>
|
/// <param name="format"></param>
|
/// <returns></returns>
|
public static string UnixToDateTimeWithFormatMS(long unixTimeStamp, string format = "yyyy")
|
{
|
DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); // 当地时区
|
return startTime.AddMilliseconds(unixTimeStamp).ToString(format);
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="unixTimeStamp">毫秒时间戳</param>
|
/// <returns></returns>
|
public static string UnixToDateTimeYearMS(long unixTimeStamp)
|
{
|
try
|
{
|
return UnixToDateTimeWithFormatMS(unixTimeStamp);
|
}
|
catch
|
{
|
return "2000";
|
}
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="isMilliseconds">是否微秒</param>
|
/// <returns></returns>
|
public static string GetTimestamp(bool isMilliseconds = true)
|
{
|
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
|
if (isMilliseconds)
|
{
|
return ((long)(DateTime.Now - startTime).TotalMilliseconds).ToString(); // 相差微秒数
|
}
|
else
|
{
|
return ((long)(DateTime.Now - startTime).TotalSeconds).ToString(); // 相差秒数
|
}
|
}
|
|
/// <summary>
|
/// 获取请求提交的语言参数
|
/// </summary>
|
public static string GetPostLanguageType()
|
{
|
return Language.CurrentLanguage == "Chinese" ? LanguageTypeEnum.CHINESE.ToString() : LanguageTypeEnum.ENGLISH.ToString();
|
}
|
|
|
#region 时间格式转换
|
private const int Second = 1;
|
private const int Minute = 60 * Second;
|
private const int Hour = 60 * Minute;
|
private const int Day = 24 * Hour;
|
private const int Month = 30 * Day;
|
|
/// <summary>
|
/// 时间转换
|
/// 少于1天 显示 时分
|
/// 少于一年 显示 月日
|
/// 大于一年 显示 年
|
/// </summary>
|
/// <param name="dateTime"></param>
|
/// <returns></returns>
|
public static string ToFriendlyDisplay(this DateTime dateTime)
|
{
|
var ts = DateTime.Now - dateTime;
|
var delta = ts.TotalSeconds;
|
if (delta < 24 * Hour)
|
{
|
//显示 时:分
|
return dateTime.ToString("HH:mm");
|
}
|
else if (delta < 12 * Month)
|
{
|
//显示 月:日
|
return dateTime.ToString("MM/dd");
|
}
|
else
|
{ //显示 年
|
return dateTime.ToString("yyyy");
|
}
|
}
|
/// <summary>
|
/// 时间转换
|
/// 少于1天 显示 时分
|
/// 少于一年 显示 月日
|
/// 大于一年 显示 年
|
/// </summary>
|
/// <param name="dateTime"></param>
|
/// <returns></returns>
|
public static string ToFriendlyDisplay(long unixTimeStamp)
|
{
|
return ToFriendlyDisplay(UnixToDateTimeMS(unixTimeStamp));
|
}
|
#endregion
|
|
}
|
|
|
}
|