core-build
This commit is contained in:
72
src/WMS.Web.Core/Help/CNSpellTranslator.cs
Normal file
72
src/WMS.Web.Core/Help/CNSpellTranslator.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
using Microsoft.International.Converters.PinYinConverter;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace WMS.Web.Core.Help
|
||||
{
|
||||
/// <summary>
|
||||
/// 汉字首字母提取
|
||||
/// </summary>
|
||||
public static class CNSpellTranslator
|
||||
{
|
||||
public static string GetFirstCode(string name)
|
||||
{
|
||||
string result = string.Empty;
|
||||
foreach (char item in name)
|
||||
{
|
||||
try
|
||||
{
|
||||
ChineseChar cc = new ChineseChar(item);
|
||||
if (cc.Pinyins.Count > 0 && cc.Pinyins[0].Length > 0)
|
||||
{
|
||||
string temp = cc.Pinyins[0].ToString();
|
||||
// result += temp.Substring(0, temp.Length - 1);
|
||||
result += temp.Substring(0, 1);//取首字母
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
result += item.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 汉字转首字母大写
|
||||
/// </summary>
|
||||
/// <param name="strChinese"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetFirstSpell(string strChinese)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (strChinese.Length != 0)
|
||||
{
|
||||
StringBuilder fullSpell = new StringBuilder();
|
||||
for (int i = 0; i < strChinese.Length; i++)
|
||||
{
|
||||
var chr = strChinese[i];
|
||||
fullSpell.Append(GetSpell(chr)[0]);
|
||||
}
|
||||
return fullSpell.ToString().ToUpper();//转小写方法为 .ToLower()
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
private static string GetSpell(char chr)
|
||||
{
|
||||
var coverchr = NPinyin.Pinyin.GetPinyin(chr);
|
||||
return coverchr;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
50
src/WMS.Web.Core/Help/CacheHelp.cs
Normal file
50
src/WMS.Web.Core/Help/CacheHelp.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace WMS.Web.Core.Help
|
||||
{
|
||||
public class CacheHelp
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 定义cache
|
||||
/// </summary>
|
||||
private static MemoryCache cache = new MemoryCache(new MemoryCacheOptions());
|
||||
|
||||
/// <summary>
|
||||
/// 设置cache
|
||||
/// </summary>
|
||||
/// <param name="key">键</param>
|
||||
/// <param name="value">值</param>
|
||||
public static void SetCacheValue(string key, object value)
|
||||
{
|
||||
if (key != null)
|
||||
{
|
||||
cache.Set(key, value, new MemoryCacheEntryOptions
|
||||
{
|
||||
//SlidingExpiration = TimeSpan.FromSeconds(10)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取cache
|
||||
/// </summary>
|
||||
/// <param name="key">键</param>
|
||||
/// <returns></returns>
|
||||
public static object GetCacheValue(string key)
|
||||
{
|
||||
object val = null;
|
||||
if (key != null && cache.TryGetValue(key, out val))
|
||||
{
|
||||
return val;
|
||||
}
|
||||
else
|
||||
{
|
||||
return default(object);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
34
src/WMS.Web.Core/Help/ClassClone.cs
Normal file
34
src/WMS.Web.Core/Help/ClassClone.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
|
||||
namespace WMS.Web.Core.Help
|
||||
{
|
||||
[Serializable]
|
||||
public static class ClassClone
|
||||
{
|
||||
public static void CopyPropertiesToD<D>(this object s, D d)
|
||||
{
|
||||
try
|
||||
{
|
||||
var Types = s.GetType();//获得类型
|
||||
var Typed = d.GetType();
|
||||
foreach (PropertyInfo sp in Types.GetProperties())//获得类型的属性字段
|
||||
{
|
||||
foreach (PropertyInfo dp in Typed.GetProperties())
|
||||
{
|
||||
if (dp.Name == sp.Name)//判断属性名是否相同
|
||||
{
|
||||
dp.SetValue(d, sp.GetValue(s, null), null);//获得s对象属性的值复制给d对象的属性
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
35
src/WMS.Web.Core/Help/ClassCopyUtil.cs
Normal file
35
src/WMS.Web.Core/Help/ClassCopyUtil.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Runtime.Serialization.Formatters.Binary;
|
||||
using System.Text;
|
||||
|
||||
namespace WMS.Web.Core.Help
|
||||
{
|
||||
[Serializable]
|
||||
public static class ClassCopyUtil
|
||||
{
|
||||
/// <summary>
|
||||
/// 深度copy
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="t"></param>
|
||||
/// <returns></returns>
|
||||
public static T Clone<T>(this T t) where T : class
|
||||
{
|
||||
if (!typeof(T).IsSerializable)
|
||||
{
|
||||
throw new ArgumentException("The class type must be serializable.", "source");
|
||||
}
|
||||
if (t == null) return null;
|
||||
MemoryStream memoryStream = new MemoryStream();
|
||||
BinaryFormatter formatter = new BinaryFormatter();
|
||||
using (memoryStream)
|
||||
{
|
||||
formatter.Serialize(memoryStream, t);
|
||||
memoryStream.Position = 0;
|
||||
return (T)formatter.Deserialize(memoryStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
168
src/WMS.Web.Core/Help/DateTimeUtil.cs
Normal file
168
src/WMS.Web.Core/Help/DateTimeUtil.cs
Normal file
@@ -0,0 +1,168 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace WMS.Web.Core.Help
|
||||
{
|
||||
/// <summary>
|
||||
/// 时间转换
|
||||
/// </summary>
|
||||
public static class DateTimeUtil
|
||||
{
|
||||
/// <summary>
|
||||
/// 时间戳计时开始时间
|
||||
/// </summary>
|
||||
private static DateTime timeStampStartTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
||||
|
||||
/// <summary>
|
||||
/// DateTime转换为10位时间戳(单位:秒)
|
||||
/// </summary>
|
||||
/// <param name="dateTime"> DateTime</param>
|
||||
/// <returns>10位时间戳(单位:秒)</returns>
|
||||
public static long DateTimeToTimeStamp(this DateTime dateTime)
|
||||
{
|
||||
return (long)(dateTime.ToUniversalTime() - timeStampStartTime).TotalSeconds;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// DateTime转换为13位时间戳(单位:毫秒)
|
||||
/// </summary>
|
||||
/// <param name="dateTime"> DateTime</param>
|
||||
/// <returns>13位时间戳(单位:毫秒)</returns>
|
||||
public static long DateTimeToLongTimeStamp(this DateTime dateTime)
|
||||
{
|
||||
return (long)(dateTime.ToUniversalTime() - timeStampStartTime).TotalMilliseconds;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 10位时间戳(单位:秒)转换为DateTime
|
||||
/// </summary>
|
||||
/// <param name="timeStamp">10位时间戳(单位:秒)</param>
|
||||
/// <returns>DateTime</returns>
|
||||
public static DateTime TimeStampToDateTime(this long timeStamp)
|
||||
{
|
||||
return timeStampStartTime.AddSeconds(timeStamp).ToLocalTime();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 13位时间戳(单位:毫秒)转换为DateTime
|
||||
/// </summary>
|
||||
/// <param name="longTimeStamp">13位时间戳(单位:毫秒)</param>
|
||||
/// <returns>DateTime</returns>
|
||||
public static DateTime LongTimeStampToDateTime(this long longTimeStamp)
|
||||
{
|
||||
return timeStampStartTime.AddMilliseconds(longTimeStamp).ToLocalTime();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 转字符串
|
||||
/// </summary>
|
||||
/// <param name="date">)</param>
|
||||
/// <returns>DateTime</returns>
|
||||
public static string DateToStringDay(this DateTime? date)
|
||||
{
|
||||
if (date == null) return "";
|
||||
try
|
||||
{
|
||||
DateTime dt = Convert.ToDateTime(date);
|
||||
return dt.ToString("yyyy-MM-dd");
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 转字符串
|
||||
/// </summary>
|
||||
/// <param name="date">)</param>
|
||||
/// <returns>DateTime</returns>
|
||||
public static string DateToStringSeconds(this DateTime? date)
|
||||
{
|
||||
if (date == null) return "";
|
||||
try
|
||||
{
|
||||
DateTime dt = Convert.ToDateTime(date);
|
||||
return dt.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 转字符串
|
||||
/// </summary>
|
||||
/// <param name="date">)</param>
|
||||
/// <returns>DateTime</returns>
|
||||
public static string DateToStringMonth(this DateTime? date)
|
||||
{
|
||||
if (date == null) return "";
|
||||
try
|
||||
{
|
||||
DateTime dt = Convert.ToDateTime(date);
|
||||
return dt.ToString("MM年dd月");
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转字符串
|
||||
/// </summary>
|
||||
/// <param name="date">)</param>
|
||||
/// <returns>DateTime</returns>
|
||||
public static string DateToStringDay(this DateTime date)
|
||||
{
|
||||
if (date == null) return "";
|
||||
try
|
||||
{
|
||||
DateTime dt = Convert.ToDateTime(date);
|
||||
return dt.ToString("yyyy-MM-dd");
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 转字符串
|
||||
/// </summary>
|
||||
/// <param name="date">)</param>
|
||||
/// <returns>DateTime</returns>
|
||||
public static string DateToStringSeconds(this DateTime date)
|
||||
{
|
||||
if (date == null) return "";
|
||||
try
|
||||
{
|
||||
DateTime dt = Convert.ToDateTime(date);
|
||||
return dt.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 转字符串
|
||||
/// </summary>
|
||||
/// <param name="date">)</param>
|
||||
/// <returns>DateTime</returns>
|
||||
public static string DateToStringMonth(this DateTime date)
|
||||
{
|
||||
if (date == null) return "";
|
||||
try
|
||||
{
|
||||
DateTime dt = Convert.ToDateTime(date);
|
||||
return dt.ToString("MM年dd月");
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
220
src/WMS.Web.Core/Help/HttpClientHelp.cs
Normal file
220
src/WMS.Web.Core/Help/HttpClientHelp.cs
Normal file
@@ -0,0 +1,220 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Json;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WMS.Web.Core.Help
|
||||
{
|
||||
public class HttpClientHelp
|
||||
{
|
||||
private HttpOptions _options;
|
||||
|
||||
public HttpClientHelp(IOptions<HttpOptions> options)
|
||||
{
|
||||
this._options = options?.Value;
|
||||
}
|
||||
public string PostHttp(string url, JsonObject reqData, bool isHaveCookie = false, string cookieValue = "", string appSecret = "")
|
||||
{
|
||||
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
||||
|
||||
HMACSHA1 hmacsha1 = new HMACSHA1();
|
||||
if (!string.IsNullOrEmpty(appSecret))
|
||||
{
|
||||
byte[] secret = Encoding.UTF8.GetBytes(appSecret);
|
||||
hmacsha1.Key = secret;
|
||||
}
|
||||
byte[] dataBuffer = Encoding.UTF8.GetBytes(reqData.ToString());
|
||||
byte[] hashBytes = hmacsha1.ComputeHash(dataBuffer);
|
||||
|
||||
int contentLength = dataBuffer.Length;
|
||||
string rawHmac = Convert.ToBase64String(hashBytes);
|
||||
request.Method = "POST";
|
||||
request.ContentType = "application/json; charset=utf-8";
|
||||
request.ContentLength = contentLength;
|
||||
request.Accept = "application/json; charset=utf-8";
|
||||
request.Headers.Add("Content-Signature", "HMAC-SHA1 " + rawHmac);
|
||||
if (isHaveCookie)
|
||||
{
|
||||
var cook = new Cookie();
|
||||
cook.Domain = _options.Url;
|
||||
cook.Name = "PHPSESSID";
|
||||
cook.Value = cookieValue;
|
||||
request.CookieContainer = new CookieContainer();
|
||||
request.CookieContainer.Add(cook);
|
||||
}
|
||||
|
||||
Stream writer;
|
||||
try
|
||||
{
|
||||
writer = request.GetRequestStream();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
writer = null;
|
||||
Console.WriteLine("链接单点错误异常:" + ex.ToString());
|
||||
}
|
||||
writer.Write(dataBuffer, 0, dataBuffer.Length);
|
||||
writer.Close();
|
||||
|
||||
Stream responseStream;
|
||||
try
|
||||
{
|
||||
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
||||
responseStream = response.GetResponseStream();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
responseStream = null;
|
||||
//WriteLog("服务器异常" + ex.Message);
|
||||
}
|
||||
StreamReader streamReader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
|
||||
|
||||
string respData = streamReader.ReadToEnd();
|
||||
responseStream.Close();
|
||||
return respData;
|
||||
}
|
||||
|
||||
public string PostHttpNoData(string url, string cookieValue = "")
|
||||
{
|
||||
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
||||
|
||||
request.Method = "POST";
|
||||
request.ContentType = "application/json; charset=utf-8";
|
||||
request.Accept = "application/json; charset=utf-8";
|
||||
if (!string.IsNullOrEmpty(cookieValue))
|
||||
{
|
||||
var cook = new Cookie();
|
||||
cook.Domain = _options.Url;
|
||||
cook.Name = "PHPSESSID";
|
||||
cook.Value = cookieValue;
|
||||
request.CookieContainer = new CookieContainer();
|
||||
request.CookieContainer.Add(cook);
|
||||
}
|
||||
|
||||
Stream writer;
|
||||
try
|
||||
{
|
||||
writer = request.GetRequestStream();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
writer = null;
|
||||
Console.WriteLine("链接单点错误异常:" + ex.ToString());
|
||||
}
|
||||
writer.Close();
|
||||
|
||||
Stream responseStream;
|
||||
try
|
||||
{
|
||||
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
||||
responseStream = response.GetResponseStream();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
responseStream = null;
|
||||
//WriteLog("服务器异常" + ex.Message);
|
||||
}
|
||||
StreamReader streamReader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
|
||||
|
||||
string respData = streamReader.ReadToEnd();
|
||||
responseStream.Close();
|
||||
return respData;
|
||||
}
|
||||
|
||||
public string GetHttp(string url, bool isHaveCookie = false, string cookieValue = "")
|
||||
{
|
||||
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
||||
|
||||
HMACSHA1 hmacsha1 = new HMACSHA1();
|
||||
|
||||
request.Method = "Get";
|
||||
request.ContentType = "application/json; charset=utf-8";
|
||||
request.Accept = "application/json; charset=utf-8";
|
||||
if (isHaveCookie)
|
||||
{
|
||||
var cook = new Cookie();
|
||||
cook.Domain = _options.Url;
|
||||
cook.Name = "PHPSESSID";
|
||||
cook.Value = cookieValue;
|
||||
request.CookieContainer = new CookieContainer();
|
||||
request.CookieContainer.Add(cook);
|
||||
}
|
||||
|
||||
Stream writer;
|
||||
try
|
||||
{
|
||||
writer = request.GetRequestStream();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
writer = null;
|
||||
//WriteLog("连接服务器异常");
|
||||
}
|
||||
|
||||
Stream responseStream;
|
||||
try
|
||||
{
|
||||
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
||||
responseStream = response.GetResponseStream();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
responseStream = null;
|
||||
//WriteLog("服务器异常" + ex.Message);
|
||||
}
|
||||
StreamReader streamReader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
|
||||
|
||||
string respData = streamReader.ReadToEnd();
|
||||
responseStream.Close();
|
||||
return respData;
|
||||
}
|
||||
|
||||
|
||||
private static readonly DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
||||
|
||||
public static long CurrentTimeMillis()
|
||||
{
|
||||
return (long)(DateTime.UtcNow - Jan1st1970).TotalMilliseconds;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public async Task<JsonValue> Post(string url, string param, string token)
|
||||
{
|
||||
HttpClient _client = new HttpClient();
|
||||
//读取返回消息
|
||||
string res = string.Empty;
|
||||
try
|
||||
{
|
||||
//填充formData
|
||||
if (param == null) param = "";
|
||||
HttpContent hc = new StringContent(param, Encoding.UTF8)
|
||||
{
|
||||
Headers =
|
||||
{
|
||||
ContentType = new MediaTypeHeaderValue("application/json")//application/x-www-form-urlencoded
|
||||
}
|
||||
};
|
||||
_client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
|
||||
var responseMessage = _client.PostAsync(url, hc).Result;
|
||||
var msg = await responseMessage.Content.ReadAsStringAsync();
|
||||
if (msg.Equals(""))
|
||||
return null;
|
||||
else
|
||||
return JsonObject.Parse(msg);
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
20
src/WMS.Web.Core/Help/HttpOptions.cs
Normal file
20
src/WMS.Web.Core/Help/HttpOptions.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace WMS.Web.Core.Help
|
||||
{
|
||||
public class HttpOptions
|
||||
{
|
||||
public string Url { get; set; }
|
||||
|
||||
public string K3Url { get; set; }
|
||||
public string K3UrlLogin { get; set; }
|
||||
public bool K3Enable { get; set; } = false;
|
||||
|
||||
public string EspUrl { get; set; }
|
||||
|
||||
public string EspAppId { get; set; }
|
||||
public string EspAppSecret { get; set; }
|
||||
}
|
||||
}
|
||||
119
src/WMS.Web.Core/Help/RedisClient.cs
Normal file
119
src/WMS.Web.Core/Help/RedisClient.cs
Normal file
@@ -0,0 +1,119 @@
|
||||
using Newtonsoft.Json;
|
||||
using StackExchange.Redis;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace WMS.Web.Core.Help
|
||||
{
|
||||
public class RedisClient
|
||||
{
|
||||
private static readonly object Locker = new object();
|
||||
|
||||
private ConnectionMultiplexer redisMultiplexer;
|
||||
|
||||
IDatabase db = null;
|
||||
|
||||
private static RedisClient _redisClient = null;
|
||||
public static RedisClient redisClient
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_redisClient == null)
|
||||
{
|
||||
lock (Locker)
|
||||
{
|
||||
if (_redisClient == null)
|
||||
{
|
||||
_redisClient = new RedisClient();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
return _redisClient;
|
||||
}
|
||||
}
|
||||
|
||||
public void InitConnect(string RedisConnection)
|
||||
{
|
||||
try
|
||||
{
|
||||
redisMultiplexer = ConnectionMultiplexer.Connect(RedisConnection);
|
||||
db = redisMultiplexer.GetDatabase();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.Message);
|
||||
redisMultiplexer = null;
|
||||
db = null;
|
||||
}
|
||||
}
|
||||
|
||||
public RedisClient()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#region String
|
||||
/// <summary>
|
||||
/// 保存单个key value
|
||||
/// </summary>
|
||||
/// <param name="value">保存的值</param>
|
||||
/// <param name="expiry">过期时间</param>
|
||||
public bool SetStringKey(string key, string value, TimeSpan? expiry = default(TimeSpan?))
|
||||
{
|
||||
return db.StringSet(key, value, expiry);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取单个key的值
|
||||
/// </summary>
|
||||
public RedisValue GetStringKey(string key)
|
||||
{
|
||||
return db.StringGet(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除redis
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public bool RemoveStringKey(string key)
|
||||
{
|
||||
return db.KeyDelete(key);
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取一个key的对象
|
||||
/// </summary>
|
||||
public T GetStringKey<T>(string key)
|
||||
{
|
||||
if (db == null)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
var value = db.StringGet(key);
|
||||
if (value.IsNullOrEmpty)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
return JsonConvert.DeserializeObject<T>(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 保存一个对象
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
public bool SetStringKey<T>(string key, T obj, TimeSpan? expiry = default(TimeSpan?))
|
||||
{
|
||||
if (db == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
string json = JsonConvert.SerializeObject(obj);
|
||||
return db.StringSet(key, json, expiry);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
77
src/WMS.Web.Core/Help/ValidatetionHelper.cs
Normal file
77
src/WMS.Web.Core/Help/ValidatetionHelper.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace WMS.Web.Core.Help
|
||||
{
|
||||
public class ValidResult
|
||||
{
|
||||
public List<ErrorMember> ErrorMembers { get; set; }
|
||||
public bool IsVaild { get; set; }
|
||||
|
||||
public string GetMessage()
|
||||
{
|
||||
string msg = "";
|
||||
for (int i = 0; i < ErrorMembers.Count(); i++)
|
||||
{
|
||||
if (i == 0)
|
||||
msg = ErrorMembers[i].ErrorMessage;
|
||||
else
|
||||
msg += "\r\n" + ErrorMembers[i].ErrorMessage;
|
||||
}
|
||||
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
|
||||
public class ErrorMember
|
||||
{
|
||||
public string ErrorMessage { get; set; }
|
||||
public string ErrorMemberName { get; set; }
|
||||
}
|
||||
public static class ValidatetionHelper
|
||||
{
|
||||
public static ValidResult IsValid(object value)
|
||||
{
|
||||
ValidResult result = new ValidResult();
|
||||
try
|
||||
{
|
||||
var validationContext = new ValidationContext(value);
|
||||
var results = new List<ValidationResult>();
|
||||
var isValid = Validator.TryValidateObject(value, validationContext, results, true);
|
||||
|
||||
if (!isValid)
|
||||
{
|
||||
result.IsVaild = false;
|
||||
result.ErrorMembers = new List<ErrorMember>();
|
||||
foreach (var item in results)
|
||||
{
|
||||
result.ErrorMembers.Add(new ErrorMember()
|
||||
{
|
||||
ErrorMessage = item.ErrorMessage,
|
||||
ErrorMemberName = item.MemberNames.FirstOrDefault()
|
||||
});
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result.IsVaild = true;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
result.IsVaild = false;
|
||||
result.ErrorMembers = new List<ErrorMember>();
|
||||
result.ErrorMembers.Add(new ErrorMember()
|
||||
{
|
||||
ErrorMessage = ex.Message,
|
||||
ErrorMemberName = "Internal error"
|
||||
});
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user