tycg_carviolation_BE/Traffic.Service/Helpers/Util.cs

71 lines
2.7 KiB
C#
Raw Permalink Normal View History

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Traffic.Data.ViewModels;
namespace LiangLiSystem.Services.Helpers
{
public static class Util
{
public static string GetEnumDesc(Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes = fi.GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[];
if (attributes != null && attributes.Any())
{
return attributes.First().Description;
}
return value.ToString();
}
public static DateTime GetDateTimeNow()
{
return Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
}
public static string GetDateTimeNow(string date)
{
return Convert.ToDateTime(date).ToString("yyyy-MM-dd HH:mm:ss");
}
public static SearchModel GetSearchModel(SearchModelViewModel searchModel)
{
return new SearchModel
{
Term = searchModel.Term,
Page = Convert.ToInt32(string.IsNullOrWhiteSpace(searchModel.Page) ? "1" : searchModel.Page),
PageSize = Convert.ToInt32(string.IsNullOrWhiteSpace(searchModel.PageSize) ? "1" : searchModel.PageSize),
IsAsc = (searchModel.IsAsc == "true" || searchModel.IsAsc == "") ? true : false,
Order = string.IsNullOrWhiteSpace(searchModel.Order) ? "id" : searchModel.Order,
};
}
/// <summary>
/// #必須包含數字, #必須包含小寫字母, #必須包含大寫字母, #必須包含特殊符號, #至少8個字符最多30個字符
/// </summary>
/// <param name="password"></param>
/// <returns></returns>
public static bool IsValidPassword(string password)
{
var regex = new Regex(@"
(?=.*[0-9]) #
(?=.*[a-z]) #
(?=.*[A-Z]) #
(?=([\x21-\x7e]+)[^a-zA-Z0-9]) #
.{12,30} #1230
", RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
return regex.Match(password).Success;
}
}
}