using System;
using Shared;
using System.Text.RegularExpressions;
using System.Text;
using System.Collections.Generic;
using System.Collections;
namespace HDL_ON
{
///
/// 常用工具类
///
public class Utlis
{
///
/// 全局打印
///
public static void WriteLine(object mes)
{
#if DEBUG
Console.WriteLine(mes);
#endif
}
///
/// 弹窗提示
///
///
public static void ShowAlertOnMainThread(string mes)
{
Application.RunOnMainThread(() => {
new Alert("", mes, Language.StringByID(StringId.Close)).Show();
});
}
///
/// ShowTip
///
///
/// 关闭时间
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 ■ 各种正则检测_______________________
///
/// 用于中国大陆验证手机号正则表达式
/// 以1开头,11位
///
public static string PhoneRegexStr = "^[1][0-9]{10}$";
///
/// 用于验证非中国大陆手机号正则表达式
///
public static string PhoneForForeignRegexStr = "^[0-9]*$";
///
/// 用于验证邮箱正则表达式
///
public static string EmailRegexStr = "^\\s*([A-Za-z0-9_-]+(\\.\\w+)*@(\\w+\\.)+\\w{2,5})\\s*$";
///
/// 判断是否包含大写字母
///
/// true, if contain upper was checked, false otherwise.
/// Value.
public static bool CheckContainUpper(string value)
{
Regex reg = new Regex("[A-Z]+");
return reg.IsMatch(value);
}
///
/// 判断是否包含小写字母
///
/// true, if contain lower was checked, false otherwise.
/// Value.
public static bool CheckContainLower(string value)
{
Regex reg = new Regex("[a-z]+");
return reg.IsMatch(value);
}
///
/// 判断是否包含数字
///
/// true, if contain lower was checked, false otherwise.
/// Value.
public static bool CheckContainNum(string value)
{
Regex reg = new Regex("[0-9]+");
return reg.IsMatch(value);
}
///
/// 判断是否包含符号
///
/// true, if contain lower was checked, false otherwise.
/// Value.
public static bool CheckContainSymbol(string value)
{
Regex reg = new Regex("([^a-z0-9A-Z])+");
return reg.IsMatch(value);
}
///
/// 检测邮箱是否合法
///
///
///
public static bool CheckEmail(string email)
{
Regex reg = new Regex(EmailRegexStr);
return reg.IsMatch(email);
}
///
/// 检测手机号是否合法
///
/// 手机号
/// 地区代码
///
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
///
/// TextButton
/// 根据文本,计算按钮它实际的宽度
///
///
/// 补充宽度
///
public static int GetRealWidthByTextButton(Button btn, int extendWidth = 1)
{
if (string.IsNullOrEmpty(btn.Text)) { return Application.GetRealWidth(extendWidth); }
return btn.GetTextWidth() + Application.GetRealWidth(extendWidth);
}
///
/// 根据文本,计算按钮它实际的宽度
///
///
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
}
///
/// 生成随机字符串
///
/// 字符串的长度
///
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();
}
///
/// 生成单个随机数字
///
static int createNum()
{
Random random = new Random(Guid.NewGuid().GetHashCode());
int num = random.Next(10);
return num;
}
///
/// 生成单个大写随机字母
///
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;
}
///
/// 生成单个小写随机字母
///
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;
}
///
///
///
///
///
public static DateTime UnixToDateTime(long unixTimeStamp)
{
DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); // 当地时区
return startTime.AddSeconds(unixTimeStamp);
}
///
///
///
///
///
public static long DateTimeToUnix(DateTime dateTime)
{
DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); // 当地时区
return (long)(dateTime - startTime).TotalSeconds; // 相差秒数
}
///
///
///
///
///
public static DateTime UnixToDateTimeMS(long unixTimeStamp)
{
DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); // 当地时区
return startTime.AddMilliseconds(unixTimeStamp);
}
///
///
///
///
///
///
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);
}
///
///
///
/// 毫秒时间戳
///
public static string UnixToDateTimeYearMS(long unixTimeStamp)
{
try
{
return UnixToDateTimeWithFormatMS(unixTimeStamp);
}
catch
{
return "2000";
}
}
///
/// 获取当前时间戳值
///
///
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(); // 相差秒数
}
}
///
/// 获取请求提交的语言参数
///
public static string GetPostLanguageType()
{
return Language.CurrentLanguage == "Chinese" ? LanguageTypeEnum.CHINESE.ToString() : LanguageTypeEnum.ENGLISH.ToString();
}
}
}