using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text.RegularExpressions;
|
using Shared.SimpleControl;
|
|
namespace Shared
|
{
|
public class CommonUtlis
|
{
|
#region ■ 变量声明___________________________
|
|
/// <summary>
|
/// 通用方法
|
/// </summary>
|
private static CommonUtlis m_Current = null;
|
/// <summary>
|
/// 通用方法
|
/// </summary>
|
public static CommonUtlis Current {
|
get {
|
if (m_Current == null) {
|
m_Current = new CommonUtlis ();
|
}
|
return m_Current;
|
}
|
}
|
|
#endregion
|
|
#region ■ 各种正确检测_______________________
|
|
/// <summary>
|
/// 用于中国大陆验证手机号正则表达式
|
/// </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>
|
/// 用于密码强度正则表达式
|
/// 密码必须为8-20个字符,包含字母、数字和符号
|
/// </summary>
|
//public static string PasswordRegexStr = "^(?![0-9]+$)(?![a-zA-Z]+$)(?![0-9a-zA-Z]+$)(?![0-9\\W]+$)(?![a-zA-Z\\W]+$)[0-9A-Za-z\\W]{8,20}$";
|
public static string PasswordRegexStr = @"^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[^\w\s]).{8,20}$";//必须包含一个大写,一个小写字母,一个数字,一个特殊字符,且长度为8到20位
|
|
|
|
|
/// <summary>
|
/// 判断是否包含大写字母
|
/// </summary>
|
/// <returns><c>true</c>, if contain upper was checked, <c>false</c> otherwise.</returns>
|
/// <param name="value">Value.</param>
|
public 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 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 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 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 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 bool CheckPhoneNumber (string phoneNumber, string areaCode)
|
{
|
//校验外国手机号
|
if (areaCode != "86") {
|
Regex reg = new Regex (PhoneForForeignRegexStr);
|
return reg.IsMatch (phoneNumber);
|
}
|
|
//校验国内手机号
|
if (phoneNumber.Length > 11) {
|
return false;
|
} else if (phoneNumber.Length == 11) {
|
Regex reg = new Regex (PhoneRegexStr);
|
return reg.IsMatch (phoneNumber);
|
} else {
|
//正则表达式判断是否数字
|
Regex reg = new Regex ("^[0-9]*$");
|
return reg.IsMatch (phoneNumber);
|
}
|
}
|
|
/// <summary>
|
/// 检测密码是否合法
|
/// </summary>
|
/// <param name="password"></param>
|
/// <returns></returns>
|
public bool CheckPassword (string password)
|
{
|
Regex reg = new Regex (PasswordRegexStr);
|
return reg.IsMatch (password);
|
}
|
|
#endregion
|
|
/// <summary>
|
/// 检测iOS是否越狱和Android是否root
|
/// </summary>
|
public void CheckIfJailBreak ()
|
{
|
|
#if __IOS__
|
var mes = "Your phone has been Jailbroken and you cannot use the APP.";
|
#else
|
var mes = "Your phone has been ROOT and you cannot use the APP.";
|
#endif
|
|
if (Shared.HDLUtils.ISJailBreak ()) {
|
|
Alert alert = new Alert ("", mes, Language.StringByID (SimpleControl.R.MyInternationalizationString.Confrim));
|
alert.ResultEventHandler += (sender2, e2) => {
|
Shared.HDLUtils.ExitApplication ();
|
};
|
alert.Show ();
|
}
|
|
|
}
|
|
/// <summary>
|
/// 弹窗提示
|
/// </summary>
|
/// <param name="mes">弹窗提示信息</param>
|
/// <param name="okAction">确认按钮事件</param>
|
public void ShowActionAlert (string mes, Action okAction)
|
{
|
Alert alert = new Alert ("", mes, Language.StringByID (SimpleControl.R.MyInternationalizationString.Cancel), Language.StringByID (SimpleControl.R.MyInternationalizationString.Confrim));
|
alert.ResultEventHandler += (sender2, e2) => {
|
if (e2) {
|
okAction?.Invoke ();
|
}
|
};
|
alert.Show ();
|
}
|
|
#region ■ 通用Dialog_______________________
|
/// <summary>
|
/// 通用编辑Dialog
|
/// </summary>
|
/// <param name="nameStr"></param>
|
/// <param name="titleStr"></param>
|
public void ShowEditTextDialog (string nameStr, Action<string> saveAction, string titleStr = "")
|
{
|
Dialog dialog = new Dialog ();
|
|
FrameLayout dialogBodyView = new FrameLayout () {
|
Gravity = Gravity.Center,
|
Width = Application.GetRealWidth (500),
|
Height = Application.GetRealHeight (330),
|
Radius = 5,
|
BorderColor = SkinStyle.Current.Transparent,
|
BorderWidth = 0,
|
BackgroundColor = SkinStyle.Current.DialogColor,
|
};
|
dialog.AddChidren (dialogBodyView);
|
|
Button btnTitle = new Button () {
|
Height = Application.GetRealHeight (80),
|
TextAlignment = TextAlignment.Center,
|
Text = titleStr,
|
TextColor = SkinStyle.Current.DialogTextColor,
|
BackgroundColor = SkinStyle.Current.DialogTitle,
|
};
|
dialogBodyView.AddChidren (btnTitle);
|
|
EditText etZoneName = new EditText () {
|
X = Application.GetRealWidth (50),
|
Y = Application.GetRealHeight (120),
|
Width = Application.GetRealWidth (400),
|
Height = Application.GetRealHeight (80),
|
TextColor = SkinStyle.Current.TextColor,
|
TextAlignment = TextAlignment.Center,
|
Radius = 5,
|
BorderColor = SkinStyle.Current.BorderColor,
|
BorderWidth = 2,
|
Text = nameStr,
|
};
|
dialogBodyView.AddChidren (etZoneName);
|
|
FrameLayout BottomView = new FrameLayout () {
|
Y = dialogBodyView.Height - Application.GetRealHeight (88),
|
Height = Application.GetRealHeight (90),
|
BackgroundColor = SkinStyle.Current.Black50Transparent,
|
};
|
dialogBodyView.AddChidren (BottomView);
|
|
Button btnClose = new Button () {
|
Width = Application.GetRealWidth (250),
|
TextID = SimpleControl.R.MyInternationalizationString.Close,
|
TextColor = SkinStyle.Current.DialogTextColor,
|
BackgroundColor = SkinStyle.Current.DialogTitle,
|
};
|
BottomView.AddChidren (btnClose);
|
btnClose.MouseUpEventHandler += (sdf, fds) => {
|
dialog.Close ();
|
};
|
|
Button btnOption = new Button () {
|
X = btnClose.Right + Application.GetRealWidth (2),
|
Width = Application.GetRealWidth (250),
|
TextID = SimpleControl.R.MyInternationalizationString.SAVE,
|
TextColor = SkinStyle.Current.DialogTextColor,
|
BackgroundColor = SkinStyle.Current.DialogTitle,
|
};
|
BottomView.AddChidren (btnOption);
|
|
btnOption.MouseUpEventHandler += (sdf, fds) => {
|
//修改名称
|
var newStr = etZoneName.Text.Trim ();
|
if (nameStr != newStr) {
|
saveAction?.Invoke (newStr);
|
}
|
dialog.Close ();
|
};
|
dialog.Show ();
|
}
|
#endregion
|
|
|
#region ■ 共用上传备份操作_______________________
|
/// <summary>
|
/// 将当前本地数据上传到自动备份
|
/// </summary>
|
/// <param name="finishAction">上传结束事件</param>
|
public void ShowUploadToAutomaticBackupDialog (Action finishAction)
|
{
|
Dialog dialog = new Dialog ();
|
|
FrameLayout dialogBodyView = new FrameLayout () {
|
Gravity = Gravity.Center,
|
Width = Application.GetRealWidth (500),
|
Height = Application.GetRealHeight (500),
|
BackgroundColor = SkinStyle.Current.DialogColor,
|
Radius = 5,
|
BorderColor = SkinStyle.Current.Transparent,
|
BorderWidth = 0,
|
};
|
dialog.AddChidren (dialogBodyView);
|
|
Button btnTitle = new Button () {
|
Height = Application.GetRealHeight (80),
|
BackgroundColor = SkinStyle.Current.DialogTitle,
|
TextAlignment = TextAlignment.Center,
|
TextID = SimpleControl.R.MyInternationalizationString.Backup,
|
TextColor = SkinStyle.Current.DialogTextColor
|
};
|
dialogBodyView.AddChidren (btnTitle);
|
//Button btnGoDownTip = new Button () {
|
// Gravity = Gravity.CenterHorizontal,
|
// Y = Application.GetRealHeight (90 - 26),
|
// Width = Application.GetRealWidth (53),
|
// Height = Application.GetRealHeight (26),
|
// UnSelectedImagePath = "Room/godown.png",
|
//};
|
//dialogBodyView.AddChidren (btnGoDownTip);
|
|
Button btnackupRemark = new Button () {
|
Gravity = Gravity.CenterHorizontal,
|
Y = Application.GetRealHeight (100),
|
Width = Application.GetRealWidth (400),
|
Height = Application.GetRealHeight (80),
|
TextID = SimpleControl.R.MyInternationalizationString.Remark,
|
TextAlignment = TextAlignment.CenterLeft,
|
TextColor = SkinStyle.Current.TextColor,
|
};
|
dialogBodyView.AddChidren (btnackupRemark);
|
|
EditText etBackupRemark = new EditText () {
|
Gravity = Gravity.CenterHorizontal,
|
Y = btnackupRemark.Bottom,
|
Width = Application.GetRealWidth (400),
|
Height = Application.GetRealHeight (80),
|
TextAlignment = TextAlignment.Center,
|
Radius = 5,
|
BorderColor = SkinStyle.Current.BorderColor,
|
BorderWidth = 1,
|
TextColor = SkinStyle.Current.TextColor,
|
};
|
dialogBodyView.AddChidren (etBackupRemark);
|
etBackupRemark.EditorEnterAction += (obj) => {
|
Application.HideSoftInput ();
|
};
|
FrameLayout bottomView = new FrameLayout () {
|
Y = Application.GetRealHeight (420),
|
Height = Application.GetRealHeight (85),
|
BackgroundColor = SkinStyle.Current.DialogTitle
|
};
|
dialogBodyView.AddChidren (bottomView);
|
|
Button btnClose = new Button () {
|
Width = Application.GetRealWidth (249),
|
TextID = SimpleControl.R.MyInternationalizationString.Close,
|
TextAlignment = TextAlignment.Center
|
};
|
bottomView.AddChidren (btnClose);
|
btnClose.MouseUpEventHandler += (send2er, e2) => {
|
dialog.Close ();
|
};
|
|
Button btnBottomLine = new Button () {
|
X = btnClose.Right,
|
Width = 1,
|
BackgroundColor = SkinStyle.Current.Black50Transparent,
|
};
|
bottomView.AddChidren (btnBottomLine);
|
|
Button btnSave = new Button () {
|
X = btnBottomLine.Right,
|
Width = Application.GetRealWidth (249),
|
TextID = SimpleControl.R.MyInternationalizationString.SAVE,
|
TextAlignment = TextAlignment.Center
|
};
|
bottomView.AddChidren (btnSave);
|
|
btnSave.MouseUpEventHandler += (sender2, e2) => {
|
if (etBackupRemark.Text.Trim () == "") {
|
new Alert (Language.StringByID (SimpleControl.R.MyInternationalizationString.Tip), Language.StringByID (SimpleControl.R.MyInternationalizationString.InputNewBakeUpFilesName),
|
Language.StringByID (SimpleControl.R.MyInternationalizationString.Close)).Show ();
|
return;
|
}
|
if (MainPage.LoginUser == null) {
|
new Alert (Language.StringByID (SimpleControl.R.MyInternationalizationString.Tip), Language.StringByID (SimpleControl.R.MyInternationalizationString.PleaseLoginSystem),
|
Language.StringByID (SimpleControl.R.MyInternationalizationString.Close)).Show ();
|
return;
|
}
|
userBakeupFile (etBackupRemark.Text.Trim (), finishAction);
|
dialog.Close ();
|
};
|
dialog.Show ();
|
|
}
|
|
|
///// <summary>
|
///// 交互备份
|
///// </summary>
|
//int InteractiveBackup = 0;
|
///// <summary>
|
///// 自定义工程备份
|
///// </summary>
|
//int CustomProjectBackup = 1;
|
///// <summary>
|
///// 用户自动备份
|
///// </summary>
|
//int UserAutoBackup = 2;
|
///// <summary>
|
///// 用户自定义备份
|
///// </summary>
|
//int UserCustomizedBackup = 3;
|
/// <summary>
|
/// 用户备份文件
|
/// </summary>
|
void userBakeupFile (string folderName, Action finishAction)
|
{
|
MainPage.Loading.Start ("Upload...");
|
System.Threading.Tasks.Task.Run (() => {
|
try {
|
var revertObj = HttpServerRequest.Current.CreateBackupFolder(folderName);
|
if (revertObj.Code == StateCode.SUCCESS) {
|
var mBackupFolderCreateRes = Newtonsoft.Json.JsonConvert.DeserializeObject<BackupFolderCreateRes> (revertObj.Data.ToString ());
|
//UpLoadBackupFileNEW (mBackupFolderCreateRes.id);
|
UpLoadBackupFileOne (mBackupFolderCreateRes.id);
|
} else {
|
IMessageCommon.Current.ShowErrorInfoAlter (revertObj.Code);
|
}
|
} catch (Exception ex) {
|
Shared.Application.RunOnMainThread (() => {
|
new Alert (Language.StringByID (SimpleControl.R.MyInternationalizationString.Tip), Language.StringByID (SimpleControl.R.MyInternationalizationString.CheckInternet),
|
Language.StringByID (SimpleControl.R.MyInternationalizationString.Close)).Show ();
|
Shared.Utlis.WriteLine (ex.Message);
|
});
|
} finally {
|
Shared.Application.RunOnMainThread (() => {
|
MainPage.Loading.Hide ();
|
//结束事件
|
finishAction?.Invoke ();
|
});
|
}
|
});
|
}
|
|
|
|
|
//2020-01-11
|
/// <summary>
|
/// 删除某个备份
|
/// </summary>
|
/// <param name="folderID"></param>
|
void DeleteFolderDataAfterUploadFailed (string folderID)
|
{
|
try {
|
var revertObj = HttpServerRequest.Current.DeleteBackupFolder (folderID);
|
if (revertObj.Code == StateCode.SUCCESS) {
|
//GetHomeDataBackupList ();
|
} else {
|
//IMessageCommon.Current.ShowErrorInfoAlter (API.API_DELETE_Folder_Delete, revertObj.StateCode);
|
}
|
} catch { } finally {
|
Application.RunOnMainThread (() => {
|
MainPage.Loading.Hide ();
|
});
|
}
|
}
|
|
/// <summary>
|
/// 上传备份文件
|
/// 多次上传多个文件
|
/// </summary>
|
/// <param name="mBackupClassId"></param>
|
public void UpLoadBackupFileOne (string folderId)
|
{
|
var backuplist = IO.FileUtils.ReadFiles ();
|
var mFileList = backuplist.FindAll ((obj) => obj != "null" && obj != UserConfig.configFile && obj != UserInfo.GlobalRegisterFile && obj != "AccountListDB" && obj != CommonConfig.ConfigFile && (!obj.Contains (".json")));
|
|
if (mFileList.Count <= 0) return;
|
|
int resultCount = mFileList.Count;
|
int index = 0;
|
foreach (var fileName in mFileList) {
|
byte [] fileBytes = IO.FileUtils.ReadFile (fileName);
|
var result = UploadDataBackupByOne (folderId, fileName, fileBytes);
|
if (result) {
|
index++;
|
Application.RunOnMainThread (() => {
|
int pro = (int)(index * 1.0 / resultCount * 100);
|
MainPage.Loading.Text = pro.ToString () + "%";
|
});
|
} else {
|
Console.WriteLine ("上传失败:" + fileName);
|
}
|
}
|
|
|
Utlis.WriteLine ($"上传完成");
|
|
if (index != resultCount) {
|
//2020-01-11 备份失败
|
DeleteFolderDataAfterUploadFailed (folderId);
|
Shared.Application.RunOnMainThread (() => {
|
new Alert ("", Language.StringByID (SimpleControl.R.MyInternationalizationString.FailedToBackupFile), Language.StringByID (SimpleControl.R.MyInternationalizationString.Close)).Show ();
|
MainPage.Loading.Hide ();
|
});
|
return;
|
}
|
|
Application.RunOnMainThread (() => {
|
//MainPage.Loading.Text = "100%";
|
new Alert ("", Language.StringByID (SimpleControl.R.MyInternationalizationString.BackupFileIsSuccessful), Language.StringByID (SimpleControl.R.MyInternationalizationString.Close)).Show ();
|
});
|
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="folderId"></param>
|
/// <param name="fileName"></param>
|
/// <param name="fileBytes"></param>
|
/// <returns></returns>
|
bool UploadDataBackupByOne (string folderId, string fileName, byte [] fileBytes)
|
{
|
try {
|
var queryDic = new Dictionary<string, object> ();
|
queryDic.Add ("folderId", folderId);
|
queryDic.Add ("fileName", fileName);
|
queryDic.Add ("homeId", UserConfig.Instance.CurrentRegion.Id);
|
|
var revertObj = HttpUtil.RequestHttpsUpload (RestSharp.Method.POST, NewAPI.API_POST_File_Create, fileBytes, queryDic, null, UserConfig.Instance.CurrentRegion.regionUrl);
|
if (revertObj.Code == StateCode.SUCCESS) {
|
return true;
|
} else {
|
//提示错误
|
return false;
|
}
|
} catch {
|
return false;
|
}
|
}
|
|
|
#endregion
|
|
//#region ■ 文件操作_______________________
|
///// <summary>
|
///// 删除本地文件
|
///// </summary>
|
//public void ReadFilesAndDelete ()
|
//{
|
// var fileNames = IO.FileUtils.ReadFiles ();
|
// foreach (var fileName in fileNames) {
|
// if (fileName == UserInfo.GlobalRegisterFile || fileName == UserConfig.configFile || fileName == MqttInfoConfig.ConfigFile || fileName == APIInfoConfig.ConfigFile) {
|
// continue;
|
// }
|
// IO.FileUtils.DeleteFile (fileName);
|
// }
|
|
//}
|
//#endregion
|
|
|
}
|
}
|