init commit
This commit is contained in:
325
20220330_Vote/Ewide.RoadFlow/Utility/Config.cs
Normal file
325
20220330_Vote/Ewide.RoadFlow/Utility/Config.cs
Normal file
@@ -0,0 +1,325 @@
|
||||
using Furion;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RoadFlow.Utility
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 系统配置类
|
||||
/// </summary>
|
||||
public class Config
|
||||
{
|
||||
/// <summary>
|
||||
/// 当前系统版本
|
||||
/// </summary>
|
||||
public static string SystemVersion { get; set; }
|
||||
/// <summary>
|
||||
/// 数据库类型
|
||||
/// </summary>
|
||||
public static string DatabaseType { get; set; }
|
||||
/// <summary>
|
||||
/// SqlServer连接字符串
|
||||
/// </summary>
|
||||
public static string ConnectionString_SqlServer { get; set; }
|
||||
/// <summary>
|
||||
/// MySql连接字符串
|
||||
/// </summary>
|
||||
public static string ConnectionString_MySql { get; set; }
|
||||
/// <summary>
|
||||
/// Oracle连接字符串
|
||||
/// </summary>
|
||||
public static string ConnectionString_Oracle { get; set; }
|
||||
/// <summary>
|
||||
/// PostgreSql连接字符串
|
||||
/// </summary>
|
||||
public static string ConnectionString_PostgreSql { get; set; }
|
||||
/// <summary>
|
||||
/// 当前系统使用的连接字符串
|
||||
/// </summary>
|
||||
public static string ConnectionString
|
||||
{
|
||||
get
|
||||
{
|
||||
switch (DatabaseType)
|
||||
{
|
||||
case "sqlserver":
|
||||
return ConnectionString_SqlServer;
|
||||
case "mysql":
|
||||
return ConnectionString_MySql;
|
||||
case "oracle":
|
||||
return ConnectionString_Oracle;
|
||||
case "postgresql":
|
||||
return ConnectionString_PostgreSql;
|
||||
}
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// cookie名称
|
||||
/// </summary>
|
||||
public static string CookieName { get; set; } = "RoadFlowCore.Session";
|
||||
|
||||
/// <summary>
|
||||
/// cookie路径
|
||||
/// </summary>
|
||||
public static string CookiePath { get; set; } = "/";
|
||||
|
||||
/// <summary>
|
||||
/// 用户ID的Session Key
|
||||
/// </summary>
|
||||
public static string UserIdSessionKey { get; set; } = "RoadFlowUserId";
|
||||
|
||||
/// <summary>
|
||||
/// session过期时间
|
||||
/// </summary>
|
||||
public static int SessionTimeout { get; set; } = 20;
|
||||
|
||||
/// <summary>
|
||||
/// 系统登录地址
|
||||
/// </summary>
|
||||
public static string LoginUrl { get; set; }
|
||||
/// <summary>
|
||||
/// 根路径
|
||||
/// </summary>
|
||||
public static string RootUrl { get; set; }
|
||||
/// <summary>
|
||||
/// 人员初始密码
|
||||
/// </summary>
|
||||
public static string InitUserPassword { get; set; } = "111";
|
||||
/// <summary>
|
||||
/// 每页显示条数
|
||||
/// </summary>
|
||||
public static int PageSize { get; set; } = 15;
|
||||
/// <summary>
|
||||
/// 是否调试模式(开发模式)
|
||||
/// </summary>
|
||||
public static bool IsDebug { get; set; } = false;
|
||||
/// <summary>
|
||||
/// 是否开启单点登录(只能在一个地方登录)
|
||||
/// </summary>
|
||||
public static bool SingleLogin { get; set; } = true;
|
||||
/// <summary>
|
||||
/// 是否将错误信息显示到客户端(0不显示 1显示)
|
||||
/// </summary>
|
||||
public static int ShowError { get; set; } = 0;
|
||||
/// <summary>
|
||||
/// 调试模式时的用户ID
|
||||
/// </summary>
|
||||
public static string DebugUserId { get; set; }
|
||||
/// <summary>
|
||||
/// 附件保存路径
|
||||
/// </summary>
|
||||
public static string FilePath { get; set; }
|
||||
/// <summary>
|
||||
/// 允许上传的文件类型
|
||||
/// </summary>
|
||||
public static string UploadFileExtNames { get; set; }
|
||||
/// <summary>
|
||||
/// 是否启用动态步骤功能
|
||||
/// </summary>
|
||||
public static bool EnableDynamicStep { get; set; } = false;
|
||||
/// <summary>
|
||||
/// 是否IFRAME的方式集成(其它系统以IFRAME方式直接加载本系统页面,本系统独立部署)
|
||||
/// </summary>
|
||||
public static bool IsIntegrateIframe { get; set; } = false;
|
||||
/// <summary>
|
||||
/// IFRAME的方式集成的地址
|
||||
/// </summary>
|
||||
public static string IntegrateIframeUrl { get; set; } = "";
|
||||
|
||||
/// <summary>
|
||||
/// 前端地址
|
||||
/// </summary>
|
||||
public static string UiAddress { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否是VUE前后端分离模式
|
||||
/// </summary>
|
||||
public static bool IsVue { get { return App.GetConfig<bool>("UtilityConfig.IsVue"); } }
|
||||
|
||||
/// <summary>
|
||||
/// 站点绝对路径
|
||||
/// </summary>
|
||||
public static string ContentRootPath { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// LibreOffice program\soffice.exe所在路径
|
||||
/// </summary>
|
||||
public static string LibreOfficePath { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 公共语言包
|
||||
/// </summary>
|
||||
public static IConfigurationRoot LangConf { get; set; }
|
||||
|
||||
|
||||
|
||||
#region 企业微信相关配置
|
||||
/// <summary>
|
||||
/// 企业ID
|
||||
/// </summary>
|
||||
public static string Enterprise_WeiXin_AppId { get; set; }
|
||||
/// <summary>
|
||||
/// 外网地址
|
||||
/// </summary>
|
||||
public static string Enterprise_WeiXin_WebUrl { get; set; }
|
||||
/// <summary>
|
||||
/// 是否使用企业微信
|
||||
/// </summary>
|
||||
public static bool Enterprise_WeiXin_IsUse { get; set; } = false;
|
||||
/// <summary>
|
||||
/// 是否要同步组织架构
|
||||
/// </summary>
|
||||
public static bool Enterprise_WeiXin_IsSyncOrg { get; set; } = false;
|
||||
#endregion
|
||||
|
||||
#region 微信公众号相关配置
|
||||
/// <summary>
|
||||
/// 是否启用公众号
|
||||
/// </summary>
|
||||
public static bool WeiXin_IsUse { get; set; } = false;
|
||||
/// <summary>
|
||||
/// 公众号APPID
|
||||
/// </summary>
|
||||
public static string WeiXin_AppId { get; set; }
|
||||
/// <summary>
|
||||
/// 公众号AppSecret
|
||||
/// </summary>
|
||||
public static string WeiXin_AppSecret { get; set; }
|
||||
/// <summary>
|
||||
/// 外网地址
|
||||
/// </summary>
|
||||
public static string WeiXin_WebUrl { get; set; }
|
||||
#endregion
|
||||
|
||||
#region 引擎中心相关
|
||||
/// <summary>
|
||||
/// 是否启用引擎中心
|
||||
/// </summary>
|
||||
public static bool EngineCenter_IsUse { get; set; } = false;
|
||||
#endregion
|
||||
|
||||
#region 多语言配置
|
||||
/// <summary>
|
||||
/// 默认语言
|
||||
/// </summary>
|
||||
public static string Language_Default { get; set; }
|
||||
/// <summary>
|
||||
/// 语言列表
|
||||
/// </summary>
|
||||
public static List<CultureInfo> Language_CultureInfos
|
||||
{
|
||||
get
|
||||
{
|
||||
return new List<CultureInfo>{
|
||||
new CultureInfo("zh-CN"),
|
||||
new CultureInfo("zh"),
|
||||
new CultureInfo("en-US")
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 语言列表
|
||||
/// </summary>
|
||||
public static Dictionary<string, string> Language_Dictionary
|
||||
{
|
||||
get
|
||||
{
|
||||
return new Dictionary<string, string>
|
||||
{
|
||||
{ "zh-CN", "简体中文"},
|
||||
{ "zh", "繁體中文"},
|
||||
{ "en-US", "English"}
|
||||
};
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 语言cookie名称
|
||||
/// </summary>
|
||||
public static string Language_CookieName { get; set; } = ".AspNetCore.Culture";
|
||||
#endregion
|
||||
|
||||
#region VUE版企业微信配置
|
||||
/// <summary>
|
||||
/// VUE版是否使用企业微信
|
||||
/// </summary>
|
||||
public static bool EnterpriseWeChatIsUse { get; set; } = false;
|
||||
/// <summary>
|
||||
/// VUE版企业微信ID
|
||||
/// </summary>
|
||||
public static string EnterpriseWeChatAppId { get; set; } = string.Empty;
|
||||
/// <summary>
|
||||
/// VUE版企业微信外网地址
|
||||
/// </summary>
|
||||
public static string EnterpriseWeChatWebUrl { get; set; } = string.Empty;
|
||||
/// <summary>
|
||||
/// VUE版是否企业微信是否同步组织架构
|
||||
/// </summary>
|
||||
public static bool EnterpriseWeChatIsSyncOrganize { get; set; } = false;
|
||||
/// <summary>
|
||||
/// VUE版企业微信后台第一个应用的AgentId
|
||||
/// </summary>
|
||||
public static string EnterpriseWeChatAgentId { get; set; } = string.Empty;
|
||||
/// <summary>
|
||||
/// VUE版企业微信后台第一个应用的Secret
|
||||
/// </summary>
|
||||
public static string EnterpriseWeChatSecret { get; set; } = string.Empty;
|
||||
/// <summary>
|
||||
/// VUE版企业微信通讯录同步Secret
|
||||
/// </summary>
|
||||
public static string EnterpriseWeChatOrganizeSecret { get; set; } = string.Empty;
|
||||
/// <summary>
|
||||
/// VUE版企业微信接收消息应用AgentId
|
||||
/// </summary>
|
||||
public static string EnterpriseWeChatMessageAgentId { get; set; } = string.Empty;
|
||||
/// <summary>
|
||||
/// VUE版企业微信接收消息应用Secret
|
||||
/// </summary>
|
||||
public static string EnterpriseWeChatMessageSecret { get; set; } = string.Empty;
|
||||
|
||||
#endregion
|
||||
|
||||
# region 邮件配置
|
||||
/// <summary>
|
||||
/// 邮件服务器
|
||||
/// </summary>
|
||||
public static string MailServer { get; set; }
|
||||
/// <summary>
|
||||
/// 邮件服务器端口
|
||||
/// </summary>
|
||||
public static int MailPort { get; set; } = 587;
|
||||
/// <summary>
|
||||
/// 邮件服务器是否使用sll
|
||||
/// </summary>
|
||||
public static bool MailEnableSsl { get; set; } = false;
|
||||
/// <summary>
|
||||
/// 邮件帐号
|
||||
/// </summary>
|
||||
public static string MailAccount { get; set; }
|
||||
/// <summary>
|
||||
/// 邮件密码
|
||||
/// </summary>
|
||||
public static string MailPassword { get; set; }
|
||||
/// <summary>
|
||||
/// 邮件默认发件人
|
||||
/// </summary>
|
||||
public static string MailSenderName { get; set; }
|
||||
/// <summary>
|
||||
/// 邮件默认发件箱
|
||||
/// </summary>
|
||||
public static string MailSenderMail { get; set; }
|
||||
/// <summary>
|
||||
/// 邮件待办处理连接有效天数
|
||||
/// </summary>
|
||||
public static int MailTokenExpireDays { get; set; } = 3;
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
147
20220330_Vote/Ewide.RoadFlow/Utility/DateExtensions.cs
Normal file
147
20220330_Vote/Ewide.RoadFlow/Utility/DateExtensions.cs
Normal file
@@ -0,0 +1,147 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RoadFlow.Utility
|
||||
{
|
||||
public static class DateExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 得到当前时间
|
||||
/// </summary>
|
||||
public static DateTime Now
|
||||
{
|
||||
get
|
||||
{
|
||||
return DateTime.Now;
|
||||
}
|
||||
}
|
||||
|
||||
public static DateTime MaxValue
|
||||
{
|
||||
get
|
||||
{
|
||||
return DateTime.MaxValue;
|
||||
}
|
||||
}
|
||||
|
||||
public static DateTime MinValue
|
||||
{
|
||||
get
|
||||
{
|
||||
return DateTime.MinValue;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 格式化为长日期格式(yyyy年M月d日)
|
||||
/// </summary>
|
||||
/// <param name="date"></param>
|
||||
/// <returns></returns>
|
||||
public static string ToLongDate(this DateTime date)
|
||||
{
|
||||
return date.ToString("yyyy年M月d日");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 格式化为长日期格式(yyyy年MM月dd日)
|
||||
/// </summary>
|
||||
/// <param name="date"></param>
|
||||
/// <returns></returns>
|
||||
public static string ToLongDate1(this DateTime date)
|
||||
{
|
||||
return date.ToString("yyyy年MM月dd日");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 格式化为日期格式(yyyy-MM-dd)
|
||||
/// </summary>
|
||||
/// <param name="date"></param>
|
||||
/// <returns></returns>
|
||||
public static string ToDateString(this DateTime date)
|
||||
{
|
||||
return date.ToString("yyyy-MM-dd");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 格式化为日期时间格式(yyyy-MM-dd HH:mm:ss)
|
||||
/// </summary>
|
||||
/// <param name="date"></param>
|
||||
public static string ToDateTimeString(this DateTime date)
|
||||
{
|
||||
return date.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 格式化为长日期时间格式(yyyy-MM-dd HH:mm:ss.fff)
|
||||
/// </summary>
|
||||
/// <param name="date"></param>
|
||||
public static string ToLongDateTimeString(this DateTime date)
|
||||
{
|
||||
return date.ToString("yyyy-MM-dd HH:mm:ss.fff");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 格式化为日期时间格式(yyyy-MM-dd HH:mm)
|
||||
/// </summary>
|
||||
/// <param name="date"></param>
|
||||
public static string ToShortDateTimeString(this DateTime date)
|
||||
{
|
||||
return date.ToString("yyyy-MM-dd HH:mm");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 格式化为日期时间格式(yyyy-MM-dd HH:mm:ss)
|
||||
/// </summary>
|
||||
/// <param name="date"></param>
|
||||
public static string ToDateTimeString(this DateTime? date)
|
||||
{
|
||||
return date.HasValue ? date.Value.ToDateTimeString() : string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 格式化为日期时间格式(yyyy-MM-dd HH:mm)
|
||||
/// </summary>
|
||||
/// <param name="date"></param>
|
||||
public static string ToShortDateTimeString(this DateTime? date)
|
||||
{
|
||||
return date.HasValue ? date.Value.ToString("yyyy-MM-dd HH:mm") : string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 取日期时间的日期部分
|
||||
/// </summary>
|
||||
/// <param name="date"></param>
|
||||
/// <returns></returns>
|
||||
public static DateTime GetDate(this DateTime date)
|
||||
{
|
||||
return date.ToString("yyyy-MM-dd").ToDateTime();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将日期时间转换为INT
|
||||
/// </summary>
|
||||
/// <param name="dateTime">日期时间</param>
|
||||
/// <returns></returns>
|
||||
public static int ToInt(this DateTime dateTime)
|
||||
{
|
||||
DateTime dt1 = new DateTime(1970, 1, 1, 8, 0, 0);
|
||||
DateTime dt2 = Convert.ToDateTime(dateTime);
|
||||
return Convert.ToInt32((dt2 - dt1).TotalSeconds);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将INT转换为日期时间
|
||||
/// </summary>
|
||||
/// <param name="ticks"></param>
|
||||
/// <returns></returns>
|
||||
public static DateTime ToDateTime(this int ticks)
|
||||
{
|
||||
DateTime startTime = new DateTime(1970, 1, 1, 0, 0, 0);
|
||||
startTime = startTime.AddSeconds(ticks).ToLocalTime();
|
||||
return startTime;
|
||||
}
|
||||
}
|
||||
}
|
||||
103
20220330_Vote/Ewide.RoadFlow/Utility/Encryption.cs
Normal file
103
20220330_Vote/Ewide.RoadFlow/Utility/Encryption.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace RoadFlow.Utility
|
||||
{
|
||||
/// <summary>
|
||||
/// 加解密类
|
||||
/// </summary>
|
||||
public class Encryption
|
||||
{
|
||||
/// <summary>
|
||||
/// 字符串MD5加密
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static string MD5(string str)
|
||||
{
|
||||
var md5 = System.Security.Cryptography.MD5.Create();
|
||||
var result = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
|
||||
var strResult = BitConverter.ToString(result);
|
||||
return strResult.Replace("-", "");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 字符串SHA1加密
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static string SHA1(string str)
|
||||
{
|
||||
var sha1 = System.Security.Cryptography.SHA1.Create();
|
||||
var result = sha1.ComputeHash(Encoding.UTF8.GetBytes(str));
|
||||
var strResult = BitConverter.ToString(result);
|
||||
return strResult.Replace("-", "");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加密KEY
|
||||
/// </summary>
|
||||
private static readonly string encryptKey = "4h!@w$rn";
|
||||
|
||||
/// <summary>
|
||||
/// 加密
|
||||
/// </summary>
|
||||
/// <param name="encryptString"></param>
|
||||
/// <returns></returns>
|
||||
public static string DESEncrypt(string encryptString)
|
||||
{
|
||||
try
|
||||
{
|
||||
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
|
||||
byte[] inputByteArray;
|
||||
inputByteArray = Encoding.Default.GetBytes(encryptString);
|
||||
string md5SKey = encryptKey;
|
||||
des.Key = Encoding.ASCII.GetBytes(md5SKey);
|
||||
des.IV = Encoding.ASCII.GetBytes(md5SKey);
|
||||
MemoryStream ms = new MemoryStream();
|
||||
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
|
||||
cs.Write(inputByteArray, 0, inputByteArray.Length);
|
||||
cs.FlushFinalBlock();
|
||||
StringBuilder ret = new StringBuilder();
|
||||
foreach (byte b in ms.ToArray())
|
||||
{
|
||||
ret.AppendFormat("{0:X2}", b);
|
||||
}
|
||||
return ret.ToString();
|
||||
}
|
||||
catch { return ""; }
|
||||
}
|
||||
/// <summary>
|
||||
/// 解密
|
||||
/// </summary>
|
||||
/// <param name="decryptString"></param>
|
||||
/// <returns></returns>
|
||||
public static string DESDecrypt(string decryptString)
|
||||
{
|
||||
try
|
||||
{
|
||||
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
|
||||
int len;
|
||||
len = decryptString.Length / 2;
|
||||
byte[] inputByteArray = new byte[len];
|
||||
int x, i;
|
||||
for (x = 0; x < len; x++)
|
||||
{
|
||||
i = Convert.ToInt32(decryptString.Substring(x * 2, 2), 16);
|
||||
inputByteArray[x] = (byte)i;
|
||||
}
|
||||
string md5SKey = encryptKey;
|
||||
des.Key = Encoding.ASCII.GetBytes(md5SKey);
|
||||
des.IV = Encoding.ASCII.GetBytes(md5SKey);
|
||||
MemoryStream ms = new MemoryStream();
|
||||
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
|
||||
cs.Write(inputByteArray, 0, inputByteArray.Length);
|
||||
cs.FlushFinalBlock();
|
||||
return Encoding.Default.GetString(ms.ToArray());
|
||||
}
|
||||
catch { return ""; }
|
||||
}
|
||||
}
|
||||
}
|
||||
250
20220330_Vote/Ewide.RoadFlow/Utility/GuidExtensions.cs
Normal file
250
20220330_Vote/Ewide.RoadFlow/Utility/GuidExtensions.cs
Normal file
@@ -0,0 +1,250 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RoadFlow.Utility
|
||||
{
|
||||
public static class GuidExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 生成新的GUID(有的需要生成连续的Guid,所以统一调用这里的方法)
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static Guid NewGuid()
|
||||
{
|
||||
return Guid.NewGuid();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断为空GUID
|
||||
/// </summary>
|
||||
/// <param name="guid"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsEmptyGuid(this Guid guid)
|
||||
{
|
||||
return guid == Guid.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断为空GUID
|
||||
/// </summary>
|
||||
/// <param name="guid"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsEmptyGuid(this Guid? guid)
|
||||
{
|
||||
return !guid.HasValue || guid.Value == Guid.Empty;
|
||||
}
|
||||
/// <summary>
|
||||
/// 判断不为空GUID
|
||||
/// </summary>
|
||||
/// <param name="guid"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsNotEmptyGuid(this string guid)
|
||||
{
|
||||
if (guid.IsNullOrWhiteSpace())
|
||||
return false;
|
||||
return guid.ToGuid().IsNotEmptyGuid();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断不为空GUID
|
||||
/// </summary>
|
||||
/// <param name="guid"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsNotEmptyGuid(this Guid guid)
|
||||
{
|
||||
return guid != Guid.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断不为NULL和空GUID
|
||||
/// </summary>
|
||||
/// <param name="guid"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsNotEmptyGuid(this Guid? guid)
|
||||
{
|
||||
return guid.HasValue && guid.Value != Guid.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将GUID转换为整数
|
||||
/// </summary>
|
||||
/// <param name="guid"></param>
|
||||
/// <returns>HashCode整数</returns>
|
||||
public static int ToInt(this Guid guid)
|
||||
{
|
||||
return Math.Abs(guid.GetHashCode());
|
||||
//byte[] buffer = guid.ToByteArray();
|
||||
//return BitConverter.ToInt32(buffer, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换GUID为大写字符串
|
||||
/// </summary>
|
||||
/// <param name="guid"></param>
|
||||
/// <returns></returns>
|
||||
public static string ToUpperString(this Guid guid)
|
||||
{
|
||||
return guid.ToString().ToUpper();
|
||||
}
|
||||
/// <summary>
|
||||
/// 转换guid为小写字符串
|
||||
/// </summary>
|
||||
/// <param name="guid"></param>
|
||||
/// <returns></returns>
|
||||
public static string ToLowerString(this Guid guid)
|
||||
{
|
||||
return guid.ToString().ToLower();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 没有分隔线的字符串
|
||||
/// </summary>
|
||||
/// <param name="guid"></param>
|
||||
/// <returns></returns>
|
||||
public static string ToNString(this Guid guid)
|
||||
{
|
||||
return guid.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 没有分隔线的小写字符串
|
||||
/// </summary>
|
||||
/// <param name="guid"></param>
|
||||
/// <returns></returns>
|
||||
public static string ToLowerNString(this Guid guid)
|
||||
{
|
||||
return guid.ToString().ToLower();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 没有分隔线的大写字符串
|
||||
/// </summary>
|
||||
/// <param name="guid"></param>
|
||||
/// <returns></returns>
|
||||
public static string ToUpperNString(this Guid guid)
|
||||
{
|
||||
return guid.ToString().ToUpper();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将int转换为GUID
|
||||
/// </summary>
|
||||
/// <param name="i">111</param>
|
||||
/// <returns>00000000-0000-0000-0000-000000000111</returns>
|
||||
public static Guid ToGuid(this int i)
|
||||
{
|
||||
return i.ToString().PadLeft(32, '0').ToGuid();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将long转换为GUID
|
||||
/// </summary>
|
||||
/// <param name="i">111</param>
|
||||
/// <returns>00000000-0000-0000-0000-000000000111</returns>
|
||||
public static Guid ToGuid(this long i)
|
||||
{
|
||||
return i.ToString().PadLeft(32, '0').ToGuid();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将GUID转换为int
|
||||
/// </summary>
|
||||
/// <param name="guid">00000000-0000-0000-0000-000000000111 的GUID</param>
|
||||
/// <returns>111</returns>
|
||||
public static int ToInteger(this Guid guid)
|
||||
{
|
||||
return guid.ToNString().TrimStart('0').ToInt();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将GUID转换为long
|
||||
/// </summary>
|
||||
/// <param name="guid">00000000-0000-0000-0000-000000000111 的GUID</param>
|
||||
/// <returns>111</returns>
|
||||
public static long ToLong(this Guid guid)
|
||||
{
|
||||
return guid.ToNString().TrimStart('0').ToLong();
|
||||
}
|
||||
|
||||
#region 生成顺序GUID
|
||||
//private static readonly RandomNumberGenerator Rng = RandomNumberGenerator.Create();
|
||||
|
||||
///// <summary>
|
||||
///// 创建顺序GUID
|
||||
///// </summary>
|
||||
///// <returns></returns>
|
||||
//public static Guid NewSequentialGuid()
|
||||
//{
|
||||
// SequentialGuidType sequentialGuidType;
|
||||
// switch (Config.DatabaseType)
|
||||
// {
|
||||
// case "sqlserver":
|
||||
// sequentialGuidType = SequentialGuidType.SequentialAtEnd;
|
||||
// break;
|
||||
// case "oracle":
|
||||
// sequentialGuidType = SequentialGuidType.SequentialAsBinary;
|
||||
// break;
|
||||
// case "mySql":
|
||||
// sequentialGuidType = SequentialGuidType.SequentialAsString;
|
||||
// break;
|
||||
// default:
|
||||
// sequentialGuidType = SequentialGuidType.SequentialAsString;
|
||||
// break;
|
||||
// }
|
||||
|
||||
// var randomBytes = new byte[10];
|
||||
// Rng.Locking(r => r.GetBytes(randomBytes));
|
||||
// long timestamp = DateTime.UtcNow.Ticks / 10000L;
|
||||
// byte[] timestampBytes = BitConverter.GetBytes(timestamp);
|
||||
// if (BitConverter.IsLittleEndian)
|
||||
// {
|
||||
// Array.Reverse(timestampBytes);
|
||||
// }
|
||||
// byte[] guidBytes = new byte[16];
|
||||
// switch (sequentialGuidType)
|
||||
// {
|
||||
// case SequentialGuidType.SequentialAsString:
|
||||
// case SequentialGuidType.SequentialAsBinary:
|
||||
// Buffer.BlockCopy(timestampBytes, 2, guidBytes, 0, 6);
|
||||
// Buffer.BlockCopy(randomBytes, 0, guidBytes, 6, 10);
|
||||
// if (sequentialGuidType == SequentialGuidType.SequentialAsString && BitConverter.IsLittleEndian)
|
||||
// {
|
||||
// Array.Reverse(guidBytes, 0, 4);
|
||||
// Array.Reverse(guidBytes, 4, 2);
|
||||
// }
|
||||
// break;
|
||||
// case SequentialGuidType.SequentialAtEnd:
|
||||
// Buffer.BlockCopy(randomBytes, 0, guidBytes, 0, 10);
|
||||
// Buffer.BlockCopy(timestampBytes, 2, guidBytes, 10, 6);
|
||||
// break;
|
||||
// }
|
||||
// return new Guid(guidBytes);
|
||||
//}
|
||||
|
||||
//private enum SequentialGuidType
|
||||
//{
|
||||
// /// <summary>
|
||||
// /// The GUID should be sequential when formatted using the
|
||||
// /// <see cref="Guid.ToString()" /> method.
|
||||
// /// </summary>
|
||||
// SequentialAsString,
|
||||
|
||||
// /// <summary>
|
||||
// /// The GUID should be sequential when formatted using the
|
||||
// /// <see cref="Guid.ToByteArray" /> method.
|
||||
// /// </summary>
|
||||
// SequentialAsBinary,
|
||||
|
||||
// /// <summary>
|
||||
// /// The sequential portion of the GUID should be located at the end
|
||||
// /// of the Data4 block.
|
||||
// /// </summary>
|
||||
// SequentialAtEnd
|
||||
//}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
183
20220330_Vote/Ewide.RoadFlow/Utility/HttpHelper.cs
Normal file
183
20220330_Vote/Ewide.RoadFlow/Utility/HttpHelper.cs
Normal file
@@ -0,0 +1,183 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RoadFlow.Utility
|
||||
{
|
||||
public class HttpHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 同步GET请求
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <param name="headers"></param>
|
||||
/// <param name="timeout">请求响应超时时间,单位/s(默认100秒)</param>
|
||||
/// <returns></returns>
|
||||
public static string HttpGet(string url, Dictionary<string, string> headers = null, int timeout = 0)
|
||||
{
|
||||
using (HttpClient client = new HttpClient())
|
||||
{
|
||||
if (headers != null)
|
||||
{
|
||||
foreach (KeyValuePair<string, string> header in headers)
|
||||
{
|
||||
client.DefaultRequestHeaders.Add(header.Key, header.Value);
|
||||
}
|
||||
}
|
||||
if (timeout > 0)
|
||||
{
|
||||
client.Timeout = new TimeSpan(0, 0, timeout);
|
||||
}
|
||||
Byte[] resultBytes = client.GetByteArrayAsync(url).Result;
|
||||
return Encoding.UTF8.GetString(resultBytes);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步GET请求
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <param name="headers"></param>
|
||||
/// <param name="timeout">请求响应超时时间,单位/s(默认100秒)</param>
|
||||
/// <returns></returns>
|
||||
public static async Task<string> HttpGetAsync(string url, Dictionary<string, string> headers = null, int timeout = 0)
|
||||
{
|
||||
using (HttpClient client = new HttpClient())
|
||||
{
|
||||
if (headers != null)
|
||||
{
|
||||
foreach (KeyValuePair<string, string> header in headers)
|
||||
{
|
||||
client.DefaultRequestHeaders.Add(header.Key, header.Value);
|
||||
}
|
||||
}
|
||||
if (timeout > 0)
|
||||
{
|
||||
client.Timeout = new TimeSpan(0, 0, timeout);
|
||||
}
|
||||
Byte[] resultBytes = await client.GetByteArrayAsync(url);
|
||||
return Encoding.Default.GetString(resultBytes);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 同步POST请求
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <param name="postData"></param>
|
||||
/// <param name="headers"></param>
|
||||
/// <param name="contentType"></param>
|
||||
/// <param name="timeout">请求响应超时时间,单位/s(默认100秒)</param>
|
||||
/// <param name="encoding">默认UTF8</param>
|
||||
/// <returns></returns>
|
||||
public static string HttpPost(string url, string postData, Dictionary<string, string> headers = null, string contentType = null, int timeout = 0, Encoding encoding = null)
|
||||
{
|
||||
using (HttpClient client = new HttpClient())
|
||||
{
|
||||
if (headers != null)
|
||||
{
|
||||
foreach (KeyValuePair<string, string> header in headers)
|
||||
{
|
||||
client.DefaultRequestHeaders.Add(header.Key, header.Value);
|
||||
}
|
||||
}
|
||||
if (timeout > 0)
|
||||
{
|
||||
client.Timeout = new TimeSpan(0, 0, timeout);
|
||||
}
|
||||
using (HttpContent content = new StringContent(postData ?? "", encoding ?? Encoding.UTF8))
|
||||
{
|
||||
if (contentType != null)
|
||||
{
|
||||
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
|
||||
}
|
||||
using (HttpResponseMessage responseMessage = client.PostAsync(url, content).Result)
|
||||
{
|
||||
Byte[] resultBytes = responseMessage.Content.ReadAsByteArrayAsync().Result;
|
||||
return Encoding.UTF8.GetString(resultBytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步POST请求
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <param name="postData"></param>
|
||||
/// <param name="headers"></param>
|
||||
/// <param name="contentType"></param>
|
||||
/// <param name="timeout">请求响应超时时间,单位/s(默认100秒)</param>
|
||||
/// <param name="encoding">默认UTF8</param>
|
||||
/// <returns></returns>
|
||||
public static async Task<string> HttpPostAsync(string url, string postData, Dictionary<string, string> headers = null, string contentType = null, int timeout = 0, Encoding encoding = null)
|
||||
{
|
||||
using (HttpClient client = new HttpClient())
|
||||
{
|
||||
if (headers != null)
|
||||
{
|
||||
foreach (KeyValuePair<string, string> header in headers)
|
||||
{
|
||||
client.DefaultRequestHeaders.Add(header.Key, header.Value);
|
||||
}
|
||||
}
|
||||
if (timeout > 0)
|
||||
{
|
||||
client.Timeout = new TimeSpan(0, 0, timeout);
|
||||
}
|
||||
using (HttpContent content = new StringContent(postData ?? "", encoding ?? Encoding.UTF8))
|
||||
{
|
||||
if (contentType != null)
|
||||
{
|
||||
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
|
||||
}
|
||||
using (HttpResponseMessage responseMessage = await client.PostAsync(url, content))
|
||||
{
|
||||
Byte[] resultBytes = await responseMessage.Content.ReadAsByteArrayAsync();
|
||||
return Encoding.UTF8.GetString(resultBytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 上传文件
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <param name="data"></param>
|
||||
/// <returns></returns>
|
||||
public static string SendFile(string url, string path, byte[] bf)
|
||||
{
|
||||
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
|
||||
CookieContainer cookieContainer = new CookieContainer();
|
||||
request.CookieContainer = cookieContainer;
|
||||
request.AllowAutoRedirect = true;
|
||||
request.Method = "POST";
|
||||
string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线
|
||||
request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary;
|
||||
byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
|
||||
byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
|
||||
//int pos = path.LastIndexOf("\\");
|
||||
string fileName = Path.GetFileName(path);
|
||||
//请求头部信息
|
||||
StringBuilder sbHeader = new StringBuilder(string.Format("Content-Disposition:form-data;name=\"media\";filename=\"{0}\"\r\nContent-Type:application/octet-stream\r\n\r\n", fileName));
|
||||
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString());
|
||||
Stream postStream = request.GetRequestStream();
|
||||
postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
|
||||
postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
|
||||
postStream.Write(bf, 0, bf.Length);
|
||||
postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
|
||||
postStream.Close();
|
||||
//发送请求并获取相应回应数据
|
||||
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
|
||||
Stream instream = response.GetResponseStream();
|
||||
StreamReader sr = new StreamReader(instream, Encoding.UTF8);
|
||||
string content = sr.ReadToEnd();
|
||||
return content;
|
||||
}
|
||||
}
|
||||
}
|
||||
331
20220330_Vote/Ewide.RoadFlow/Utility/JsonExtsions.cs
Normal file
331
20220330_Vote/Ewide.RoadFlow/Utility/JsonExtsions.cs
Normal file
@@ -0,0 +1,331 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Furion.FriendlyException;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.Collections.Specialized;
|
||||
using System.Data;
|
||||
using System.Reflection;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Xml;
|
||||
using RoadFlow.Utility;
|
||||
|
||||
namespace Ewide.RoadFlowLite.Utility
|
||||
{
|
||||
public static class JsonExtsions
|
||||
{
|
||||
|
||||
|
||||
public static JObject ToJObject(this string str, bool isToLower = true)
|
||||
{
|
||||
return JObject.Parse(isToLower ? str.ToLower() : str);
|
||||
}
|
||||
public static JArray ToJArray(this string str, bool isToLower = true)
|
||||
{
|
||||
return JArray.Parse(isToLower ? str.ToLower() : str);
|
||||
}
|
||||
public static string GetJsonValue(this JObject jObject, string jsonName, bool isToLower = false, bool isThrowExp = false)
|
||||
{
|
||||
if (jObject == null)
|
||||
throw Oops.Oh("参数为空");
|
||||
JToken token = jObject[isToLower ? jsonName.ToLower() : jsonName];
|
||||
if (token == null)
|
||||
{
|
||||
foreach (var item in jObject)
|
||||
{
|
||||
if (item.Key.ToLower() == jsonName.ToLower())
|
||||
token = item.Value;
|
||||
}
|
||||
}
|
||||
if (token == null)
|
||||
{
|
||||
|
||||
if (isThrowExp)
|
||||
throw Oops.Oh(jsonName + "是必需的");
|
||||
return string.Empty;
|
||||
}
|
||||
else
|
||||
return token.ToString();
|
||||
}
|
||||
public static string GetJsonValue(this JArray jObject, string jsonName, bool isToLower = true)
|
||||
{
|
||||
JToken token = jObject[isToLower ? jsonName.ToLower() : jsonName];
|
||||
if (token == null)
|
||||
return string.Empty;
|
||||
else
|
||||
return token.ToString();
|
||||
}
|
||||
public static string GetJsonValue(this string param, string jsonName, bool isToLower = true, bool isThrowExp = false)
|
||||
{
|
||||
JObject jObject = param.ToJObject(isToLower);
|
||||
return GetJsonValue(jObject, jsonName, isToLower, isThrowExp);
|
||||
}
|
||||
public static DateTime? GetJsonDateTimeValue(this string jObjectstr, string jsonName, bool isToLower = true)
|
||||
{
|
||||
return jObjectstr.ToJObject(isToLower).GetJsonDateTimeValue(jsonName, isToLower);
|
||||
}
|
||||
public static DateTime? GetJsonDateTimeValue(this JObject jObject, string jsonName, bool isToLower = true)
|
||||
{
|
||||
string v = GetJsonValue(jObject, jsonName, isToLower);
|
||||
if (DateTime.TryParse(v, out DateTime dateTime))
|
||||
{
|
||||
return dateTime;
|
||||
}
|
||||
else
|
||||
return null;
|
||||
}
|
||||
public static decimal? GetJsonDecimalValue(this string jObjectstr, string jsonName, bool isToLower = true)
|
||||
{
|
||||
return jObjectstr.ToJObject(isToLower).GetJsonDecimalValue(jsonName, isToLower);
|
||||
}
|
||||
public static decimal? GetJsonDecimalValue(this JObject jObject, string jsonName, bool isToLower = true)
|
||||
{
|
||||
string v = GetJsonValue(jObject, jsonName, isToLower);
|
||||
if (decimal.TryParse(v, out decimal d))
|
||||
{
|
||||
return d;
|
||||
}
|
||||
else
|
||||
return null;
|
||||
}
|
||||
public static double? GetJsonDoubleValue(this string jObjectstr, string jsonName, bool isToLower = true)
|
||||
{
|
||||
return jObjectstr.ToJObject(isToLower).GetJsonDoubleValue(jsonName, isToLower);
|
||||
}
|
||||
public static double? GetJsonDoubleValue(this JObject jObject, string jsonName, bool isToLower = true)
|
||||
{
|
||||
string v = GetJsonValue(jObject, jsonName, isToLower);
|
||||
if (double.TryParse(v, out double d))
|
||||
{
|
||||
return d;
|
||||
}
|
||||
else
|
||||
return null;
|
||||
}
|
||||
public static int? GetJsonIntValue(this string jObjectstr, string jsonName, bool isToLower = true)
|
||||
{
|
||||
return jObjectstr.ToJObject(isToLower).GetJsonIntValue(jsonName, isToLower);
|
||||
}
|
||||
public static int? GetJsonIntValue(this JObject jObject, string jsonName, bool isToLower = true, bool isThrowExp = false)
|
||||
{
|
||||
string v = GetJsonValue(jObject, jsonName, isToLower, isThrowExp);
|
||||
if (int.TryParse(v, out int i))
|
||||
{
|
||||
return i;
|
||||
}
|
||||
else
|
||||
return null;
|
||||
}
|
||||
public static long? GetJsonLongValue(this string jObjectstr, string jsonName, bool isToLower = true, bool isThrowExp = false)
|
||||
{
|
||||
return jObjectstr.ToJObject(isToLower).GetJsonLongValue(jsonName, isToLower, isThrowExp);
|
||||
}
|
||||
public static long? GetJsonLongValue(this JObject jObject, string jsonName, bool isToLower = true, bool isThrowExp = false)
|
||||
{
|
||||
if (jObject == null)
|
||||
{
|
||||
if (isThrowExp)
|
||||
throw Oops.Oh(jsonName + "是必需的");
|
||||
return null;
|
||||
}
|
||||
string v = GetJsonValue(jObject, jsonName, isToLower);
|
||||
if (long.TryParse(v, out long d))
|
||||
{
|
||||
return d;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (isThrowExp)
|
||||
throw Oops.Oh(jsonName + "是必需的");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public static Guid? GetJsonGuidValue(this string jObjectstr, string jsonName, bool isToLower = true, bool isThrowExp = false)
|
||||
{
|
||||
return jObjectstr.ToJObject(isToLower).GetJsonGuidValue(jsonName, isToLower, isThrowExp);
|
||||
}
|
||||
public static Guid? GetJsonGuidValue(this JObject jObject, string jsonName, bool isToLower = true, bool isThrowExp = false)
|
||||
{
|
||||
string v = GetJsonValue(jObject, jsonName, isToLower);
|
||||
if (Guid.TryParse(v, out Guid d))
|
||||
{
|
||||
return d;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (isThrowExp)
|
||||
throw Oops.Oh(jsonName + "是必需的");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public static bool? GetJsonBoolValue(this JObject jObject, string jsonName, bool isToLower = true)
|
||||
{
|
||||
string v = jObject.GetJsonValue(jsonName, isToLower);
|
||||
if (bool.TryParse(v, out bool d))
|
||||
{
|
||||
return d;
|
||||
}
|
||||
else
|
||||
return null;
|
||||
}
|
||||
public static bool? GetJsonBoolValue(this string jObjectstr, string jsonName, bool isToLower = true)
|
||||
{
|
||||
return jObjectstr.ToJObject(isToLower).GetJsonBoolValue(jsonName, isToLower);
|
||||
}
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="jarrat"></param>
|
||||
/// <param name="index"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetArrayValue(this JArray jarrat, int index)
|
||||
{
|
||||
JToken token = jarrat[index];
|
||||
if (token == null)
|
||||
return string.Empty;
|
||||
else
|
||||
return token.ToString();
|
||||
}
|
||||
public static JArray GetJsonJArrayValue(this string jObjectstr, string jsonName, bool isToLower = true)
|
||||
{
|
||||
return jObjectstr.ToJObject(isToLower).GetJsonJArrayValue(jsonName, isToLower);
|
||||
}
|
||||
public static JArray GetJsonJArrayValue(this JObject jObject, string jsonName, bool isToLower = true)
|
||||
{
|
||||
return jObject[isToLower ? jsonName.ToLower() : jsonName] as JArray;
|
||||
}
|
||||
public static JArray GetJsonJArray(this string param, string jsonName, bool isToLower = true)
|
||||
{
|
||||
var jObject = param.ToJObject(isToLower);
|
||||
return jObject[jsonName] as JArray;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// json字符串转T
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="page"></param>
|
||||
/// <returns></returns>
|
||||
public static T ToEntity<T>(this JObject jobj, bool isToLower = true) where T : new()
|
||||
{
|
||||
var obj = new T();
|
||||
PropertyInfo[] ProList = typeof(T).GetProperties();
|
||||
foreach (PropertyInfo pro in ProList)
|
||||
{
|
||||
var propName = pro.PropertyType.Name.ToLower();
|
||||
if ("nullable`1" == propName)
|
||||
{
|
||||
propName = pro.PropertyType.GetGenericArguments()[0].Name.ToLower();
|
||||
}
|
||||
object propertyValue = pro.GetValue(obj, null);
|
||||
bool isnull = false;
|
||||
if (propertyValue == null)
|
||||
{
|
||||
isnull = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (propName)
|
||||
{
|
||||
case "guid":
|
||||
isnull = Guid.Parse(propertyValue.ToString()) == Guid.Empty;
|
||||
break;
|
||||
case "string":
|
||||
isnull = string.IsNullOrEmpty(propertyValue.ToString());
|
||||
break;
|
||||
case "int":
|
||||
case "int32":
|
||||
case "int64":
|
||||
isnull = propertyValue.ToString().ToInt(0) == 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (isnull || true)
|
||||
{
|
||||
object value = null;
|
||||
switch (propName)
|
||||
{
|
||||
case "guid":
|
||||
value = jobj.GetJsonGuidValue(pro.Name, isToLower);
|
||||
break;
|
||||
case "string":
|
||||
value = jobj.GetJsonValue(pro.Name, isToLower);
|
||||
break;
|
||||
case "int":
|
||||
case "int32":
|
||||
value = jobj.GetJsonIntValue(pro.Name, isToLower);
|
||||
break;
|
||||
case "int64":
|
||||
value = jobj.GetJsonLongValue(pro.Name, isToLower);
|
||||
break;
|
||||
case "datetime":
|
||||
value = jobj.GetJsonDateTimeValue(pro.Name, isToLower);
|
||||
break;
|
||||
case "decimal":
|
||||
value = jobj.GetJsonDecimalValue(pro.Name, isToLower);
|
||||
break;
|
||||
case "list`1":
|
||||
value = jobj.GetJsonJArrayValue(pro.Name, isToLower);
|
||||
break;
|
||||
case "boolean":
|
||||
value = jobj.GetJsonBoolValue(pro.Name, isToLower);
|
||||
break;
|
||||
default:
|
||||
throw Oops.Oh("补充类型:" + propName);
|
||||
}
|
||||
pro.SetValue(obj, value, null);
|
||||
}
|
||||
|
||||
}
|
||||
return obj;
|
||||
|
||||
|
||||
//PropertyInfo[] ProList = typeof(T).GetProperties();
|
||||
//foreach (PropertyInfo pro in ProList)
|
||||
//{
|
||||
// try
|
||||
// {
|
||||
// if (page.Request.Form.AllKeys.Contains(pro.Name))
|
||||
// {
|
||||
// string value = page.Request.Form[pro.Name];
|
||||
// string pptypeName = pro.PropertyType.Name;
|
||||
// if (pptypeName == "Nullable`1")
|
||||
// {
|
||||
// if (pro.PropertyType.GetGenericArguments()[0] == typeof(Guid))
|
||||
// {
|
||||
// pro.SetValue(obj, Guid.Parse(value), null);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// pro.SetValue(obj, Convert.ChangeType(value, pro.PropertyType.GetGenericArguments()[0]), null);
|
||||
// }
|
||||
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// pro.SetValue(obj, Convert.ChangeType(value, pro.PropertyType), null);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// catch (Exception ee)
|
||||
// {
|
||||
|
||||
// }
|
||||
//}
|
||||
//return obj;
|
||||
}
|
||||
/// <summary>
|
||||
/// json字符串转T
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="page"></param>
|
||||
/// <returns></returns>
|
||||
public static T ToEntity<T>(this string argsparam, bool isToLower = true) where T : new()
|
||||
{
|
||||
return argsparam.ToJObject(isToLower).ToEntity<T>();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
186
20220330_Vote/Ewide.RoadFlow/Utility/Mail.cs
Normal file
186
20220330_Vote/Ewide.RoadFlow/Utility/Mail.cs
Normal file
@@ -0,0 +1,186 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net.Mail;
|
||||
|
||||
namespace RoadFlow.Utility
|
||||
{
|
||||
public class Mail
|
||||
{
|
||||
/// <summary>
|
||||
/// 邮件实体
|
||||
/// </summary>
|
||||
public class MailModel
|
||||
{
|
||||
/// <summary>
|
||||
/// 接收人
|
||||
/// </summary>
|
||||
public string ReceiverName { get; set; }
|
||||
/// <summary>
|
||||
/// 接收邮件地址
|
||||
/// </summary>
|
||||
public string ReceiveMail { get; set; }
|
||||
/// <summary>
|
||||
/// 邮件主题
|
||||
/// </summary>
|
||||
public string Subject { get; set; }
|
||||
/// <summary>
|
||||
/// 邮件内容(可带HTML格式)
|
||||
/// </summary>
|
||||
public string Contents { get; set; }
|
||||
/// <summary>
|
||||
/// 邮件附件绝对地址(多个用逗号隔开)
|
||||
/// </summary>
|
||||
public string Attachments { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 邮件服务器
|
||||
/// </summary>
|
||||
private string _smtpServer;
|
||||
/// <summary>
|
||||
/// 邮件服务器端口
|
||||
/// </summary>
|
||||
private int _port;
|
||||
/// <summary>
|
||||
/// 是否使用SSL
|
||||
/// </summary>
|
||||
private bool _enableSsl;
|
||||
/// <summary>
|
||||
/// 邮件帐号
|
||||
/// </summary>
|
||||
private string _account;
|
||||
/// <summary>
|
||||
/// 邮件密码
|
||||
/// </summary>
|
||||
private string _password;
|
||||
/// <summary>
|
||||
/// 发件人
|
||||
/// </summary>
|
||||
private string _senderName;
|
||||
/// <summary>
|
||||
/// 发件人邮件地址
|
||||
/// </summary>
|
||||
private string _senderMail;
|
||||
public Mail(string smtpServer, int port, bool enableSsl, string account, string password, string senderName, string senderMail)
|
||||
{
|
||||
_smtpServer = smtpServer;
|
||||
_port = port;
|
||||
_enableSsl = enableSsl;
|
||||
_account = account;
|
||||
_password = password;
|
||||
_senderName = senderName;
|
||||
_senderMail = senderMail;
|
||||
}
|
||||
public Mail()
|
||||
{
|
||||
_smtpServer = Config.MailServer;
|
||||
_port = Config.MailPort;
|
||||
_enableSsl = Config.MailEnableSsl;
|
||||
_account = Config.MailAccount;
|
||||
_password = Config.MailPassword;
|
||||
_senderName = Config.MailSenderName;
|
||||
_senderMail = Config.MailSenderMail;
|
||||
}
|
||||
/// <summary>
|
||||
/// 发送邮件
|
||||
/// </summary>
|
||||
/// <param name="receiver">收件人姓名</param>
|
||||
/// <param name="mail">收件人邮箱地址</param>
|
||||
/// <param name="subject">邮件主题</param>
|
||||
/// <param name="content">邮件内容(可带HTML格式)</param>
|
||||
/// <param name="files">邮件附件绝对地址(多个用逗号隔开)</param>
|
||||
public void Send(string receiver, string mail, string subject, string content, string files = "")
|
||||
{
|
||||
MailModel mailModel = new MailModel()
|
||||
{
|
||||
ReceiverName = receiver,
|
||||
ReceiveMail = mail,
|
||||
Subject = subject,
|
||||
Contents = content,
|
||||
Attachments = files
|
||||
};
|
||||
Send(new List<MailModel>() { mailModel });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发送邮件
|
||||
/// </summary>
|
||||
/// <param name="mailModel"></param>
|
||||
public void Send(MailModel mailModel)
|
||||
{
|
||||
Send(mailModel);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 一次发多个邮件
|
||||
/// </summary>
|
||||
/// <param name="mails"></param>
|
||||
public void Send(List<MailModel> mails)
|
||||
{
|
||||
if (_smtpServer.IsNullOrWhiteSpace() || _account.IsNullOrWhiteSpace() || _password.IsNullOrWhiteSpace()
|
||||
|| _senderName.IsNullOrWhiteSpace() || _senderMail.IsNullOrWhiteSpace() || mails == null || mails.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// 邮件对象
|
||||
MailMessage emailMessage;
|
||||
|
||||
// smtp客户端对象
|
||||
using (SmtpClient client = new SmtpClient())
|
||||
{
|
||||
client.Host = _smtpServer;// 邮件服务器及帐户信息
|
||||
if (_port > 0)
|
||||
{
|
||||
client.Port = _port;
|
||||
}
|
||||
if (_enableSsl)
|
||||
{
|
||||
client.EnableSsl = true;
|
||||
}
|
||||
System.Net.NetworkCredential Credential = new System.Net.NetworkCredential
|
||||
{
|
||||
UserName = _account,
|
||||
Password = _password
|
||||
};
|
||||
client.Credentials = Credential;
|
||||
|
||||
// 发件人
|
||||
MailAddress sendMail = new MailAddress(_senderMail, _senderName);
|
||||
foreach (var mail in mails)
|
||||
{
|
||||
MailAddress receiveMail = new MailAddress(mail.ReceiveMail);
|
||||
emailMessage = new MailMessage(sendMail, receiveMail)
|
||||
{
|
||||
Subject = mail.Subject,
|
||||
Body = mail.Contents,
|
||||
IsBodyHtml = true,
|
||||
SubjectEncoding = System.Text.Encoding.Default,
|
||||
BodyEncoding = System.Text.Encoding.Default
|
||||
};
|
||||
if (!mail.Attachments.IsNullOrWhiteSpace())
|
||||
{
|
||||
foreach (string file in mail.Attachments.Split(','))
|
||||
{
|
||||
if (file.IsNullOrWhiteSpace() || !System.IO.File.Exists(file))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
Attachment attachment = new Attachment(File.OpenRead(file), Path.GetFileName(file));
|
||||
emailMessage.Attachments.Add(attachment);
|
||||
}
|
||||
}
|
||||
try
|
||||
{
|
||||
client.Send(emailMessage);
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
69
20220330_Vote/Ewide.RoadFlow/Utility/MathExtensions.cs
Normal file
69
20220330_Vote/Ewide.RoadFlow/Utility/MathExtensions.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
|
||||
namespace RoadFlow.Utility
|
||||
{
|
||||
public static class MathExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 判断数字是否在参数里面
|
||||
/// </summary>
|
||||
/// <param name="digit"></param>
|
||||
/// <param name="digits"></param>
|
||||
/// <returns></returns>
|
||||
public static bool In(this int digit, params int[] digits)
|
||||
{
|
||||
return digits.Contains(digit);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断数字不在参数里面
|
||||
/// </summary>
|
||||
/// <param name="digit"></param>
|
||||
/// <param name="digits"></param>
|
||||
/// <returns></returns>
|
||||
public static bool NotIn(this int digit, params int[] digits)
|
||||
{
|
||||
foreach (int i in digits)
|
||||
{
|
||||
if (i == digit)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/// <summary>
|
||||
/// 转换为文件大小显示
|
||||
/// </summary>
|
||||
/// <param name="size"></param>
|
||||
/// <returns></returns>
|
||||
public static string ToFileSize(this long size)
|
||||
{
|
||||
string fileSize;
|
||||
if(size == 0)
|
||||
{
|
||||
fileSize = "0 KB";
|
||||
}
|
||||
else if (size < 1024)
|
||||
{
|
||||
fileSize = size + " BT";
|
||||
}
|
||||
else if (size < 1048576)
|
||||
{
|
||||
fileSize = ((double)size / 1024).ToString("0.00") + " KB";
|
||||
}
|
||||
else if (size < 1073741824)
|
||||
{
|
||||
fileSize = ((double)size / 1048576).ToString("0.00") + " MB";
|
||||
}
|
||||
else
|
||||
{
|
||||
fileSize = ((double)size / 1073741824).ToString("0.00") + " GB";
|
||||
}
|
||||
return fileSize;
|
||||
}
|
||||
}
|
||||
}
|
||||
32
20220330_Vote/Ewide.RoadFlow/Utility/SMS.cs
Normal file
32
20220330_Vote/Ewide.RoadFlow/Utility/SMS.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace RoadFlow.Utility
|
||||
{
|
||||
/// <summary>
|
||||
/// 手机短信类
|
||||
/// </summary>
|
||||
public class SMS
|
||||
{
|
||||
/// <summary>
|
||||
/// 发送手机短信
|
||||
/// </summary>
|
||||
/// <param name="contents">内容</param>
|
||||
/// <param name="mobileNumber">手机号码(多个用逗号分开)</param>
|
||||
/// <returns></returns>
|
||||
public static bool SendSMS(string contents, string mobileNumbers)
|
||||
{
|
||||
if (contents.IsNullOrWhiteSpace())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
string[] numbers = mobileNumbers.Split(',');
|
||||
foreach (string number in numbers)
|
||||
{
|
||||
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
53
20220330_Vote/Ewide.RoadFlow/Utility/SignalRHub.cs
Normal file
53
20220330_Vote/Ewide.RoadFlow/Utility/SignalRHub.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using RoadFlow.Utility;
|
||||
using RoadFlow.Data;
|
||||
using Ewide.Core;
|
||||
|
||||
namespace RoadFlow.Business.SignalR
|
||||
{
|
||||
public class SignalRHub : Hub
|
||||
{
|
||||
private static IHubContext<SignalRHub> _hubContext;
|
||||
public static void Configure(IHubContext<SignalRHub> accessor)
|
||||
{
|
||||
_hubContext = accessor;
|
||||
}
|
||||
/// <summary>
|
||||
/// 发送消息
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
/// <param name="userIds">接收消息的用户ID</param>
|
||||
/// <param name="userName"></param>
|
||||
/// <returns></returns>
|
||||
public Task SendMessage(string message, List<string> userIds, string userName = "")
|
||||
{
|
||||
return _hubContext.Clients.Groups(userIds).SendAsync("SendMessage", userName.IsNullOrWhiteSpace() ? "系统" : userName, message);
|
||||
}
|
||||
public override async Task OnConnectedAsync()
|
||||
{
|
||||
string userGuid = Furion.App.GetService<IUserManager>().UserId;
|
||||
if (userGuid.IsNullOrWhiteSpace())
|
||||
{
|
||||
return;
|
||||
}
|
||||
string userId = userGuid.ToLower();
|
||||
await Groups.AddToGroupAsync(Context.ConnectionId, userId);
|
||||
await base.OnConnectedAsync();
|
||||
}
|
||||
public override async Task OnDisconnectedAsync(Exception exception)
|
||||
{
|
||||
string userGuid = Furion.App.GetService<IUserManager>().UserId;
|
||||
if (userGuid.IsNullOrWhiteSpace())
|
||||
{
|
||||
return;
|
||||
}
|
||||
string userId = userGuid.ToLower();
|
||||
await Groups.RemoveFromGroupAsync(Context.ConnectionId, userId);
|
||||
await base.OnDisconnectedAsync(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
1296
20220330_Vote/Ewide.RoadFlow/Utility/StringExtensions.cs
Normal file
1296
20220330_Vote/Ewide.RoadFlow/Utility/StringExtensions.cs
Normal file
File diff suppressed because it is too large
Load Diff
633
20220330_Vote/Ewide.RoadFlow/Utility/Tools.cs
Normal file
633
20220330_Vote/Ewide.RoadFlow/Utility/Tools.cs
Normal file
@@ -0,0 +1,633 @@
|
||||
using Furion;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RoadFlow.Utility
|
||||
{
|
||||
public class Tools
|
||||
{
|
||||
//private static HttpContext _httpContext = App.HttpContext;
|
||||
|
||||
private static IHttpContextAccessor _accessor;
|
||||
private static IWebHostEnvironment _hostingEnvironment;
|
||||
public static HttpContext HttpContext
|
||||
{
|
||||
get
|
||||
{
|
||||
return App.HttpContext;
|
||||
}
|
||||
}
|
||||
|
||||
public static void ConfigureHttpContext(IHttpContextAccessor accessor)
|
||||
{
|
||||
_accessor = accessor;
|
||||
}
|
||||
|
||||
public static void ConfigureHostingEnvironment(IWebHostEnvironment hostingEnvironment)
|
||||
{
|
||||
_hostingEnvironment = hostingEnvironment;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 验证实体
|
||||
/// </summary>
|
||||
/// <param name="modelState"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetValidateErrorMessag(ModelStateDictionary modelState)
|
||||
{
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
int i = 1;
|
||||
stringBuilder.Append("验证错误:\n");
|
||||
foreach (string key in modelState.Keys)
|
||||
{
|
||||
var state = modelState[key];
|
||||
if (state.Errors.Any())
|
||||
{
|
||||
stringBuilder.Append(i++);
|
||||
stringBuilder.Append("、");
|
||||
stringBuilder.Append(state.Errors.First().ErrorMessage);
|
||||
stringBuilder.Append("\n");
|
||||
}
|
||||
}
|
||||
return stringBuilder.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 得到当前请求的URL
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetURL(HttpRequest request = null)
|
||||
{
|
||||
if (null == request)
|
||||
{
|
||||
var context = HttpContext;
|
||||
request = null != context ? context.Request : null;
|
||||
}
|
||||
if (null == request)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
return new StringBuilder()
|
||||
.Append(request.PathBase)
|
||||
.Append(request.Path)
|
||||
.Append(request.QueryString)
|
||||
.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 得到当前请求的绝对URL
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetAbsoluteURL(HttpRequest request = null)
|
||||
{
|
||||
if (null == request)
|
||||
{
|
||||
var context = HttpContext;
|
||||
request = null != context ? context.Request : null;
|
||||
}
|
||||
if (null == request)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
return new StringBuilder()
|
||||
.Append(request.Scheme)
|
||||
.Append("://")
|
||||
.Append(request.Host)
|
||||
.Append(request.PathBase)
|
||||
.Append(request.Path)
|
||||
.Append(request.QueryString)
|
||||
.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 得到当前请求主机
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetHttpHost(HttpRequest request = null)
|
||||
{
|
||||
if (null == request)
|
||||
{
|
||||
var context = HttpContext;
|
||||
request = null != context ? context.Request : null;
|
||||
}
|
||||
if (null == request)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
return new StringBuilder()
|
||||
.Append(request.Scheme)
|
||||
.Append("://")
|
||||
.Append(request.Host)
|
||||
.Append(request.PathBase)
|
||||
.ToString();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 判断是否是ajax请求
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsAjax(HttpRequest request = null)
|
||||
{
|
||||
if (null == request)
|
||||
{
|
||||
var context = HttpContext;
|
||||
request = null != context ? context.Request : null;
|
||||
}
|
||||
if (null == request)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
bool result = false;
|
||||
var xreq = request.Headers.ContainsKey("x-requested-with");
|
||||
if (xreq)
|
||||
{
|
||||
result = request.Headers["x-requested-with"] == "XMLHttpRequest";
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 得到访问IP
|
||||
/// </summary>
|
||||
public static string GetIP(HttpContext httpContext = null)
|
||||
{
|
||||
|
||||
string result = string.Empty;
|
||||
if (httpContext == null)
|
||||
{
|
||||
httpContext = HttpContext;
|
||||
}
|
||||
if (httpContext == null)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
if (httpContext.Request.Headers.ContainsKey("X-Real-IP"))
|
||||
{
|
||||
result = httpContext.Request.Headers["X-Real-IP"].ToString();
|
||||
}
|
||||
if (httpContext.Request.Headers.ContainsKey("X-Forwarded-For"))
|
||||
{
|
||||
result = httpContext.Request.Headers["X-Forwarded-For"].ToString();
|
||||
}
|
||||
if (result.IsNullOrWhiteSpace())
|
||||
{
|
||||
|
||||
HttpContext.Connection.RemoteIpAddress.ToString();
|
||||
}
|
||||
return result.Equals("::1") ? "127.0.0.1" : result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 得到访问Agent
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static string GetBrowseAgent()
|
||||
{
|
||||
var context = HttpContext;
|
||||
if (null == context)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
return context.Request.Headers["User-Agent"];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 得到上一个请求的URL地址
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static string GetReferer()
|
||||
{
|
||||
var context = HttpContext;
|
||||
if (null == context)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
return context.Request.Headers["Referer"];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 产生随机数字
|
||||
/// </summary>
|
||||
/// <param name="start">开始数字</param>
|
||||
/// <param name="end">结束数字</param>
|
||||
/// <returns></returns>
|
||||
public static int GetRandomInt(int start, int end)
|
||||
{
|
||||
Random rd = new Random(GuidExtensions.NewGuid().ToInt()); ;
|
||||
return rd.Next(start, end);//(生成1~10之间的随机数,不包括10)
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 产生随机字符串
|
||||
/// </summary>
|
||||
/// <returns>字符串位数</returns>
|
||||
public static string GetRandomString(int length)
|
||||
{
|
||||
int number;
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
Random random = new Random(GuidExtensions.NewGuid().ToInt());
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
char code;
|
||||
number = random.Next();
|
||||
if (number % 2 == 0)
|
||||
code = (char)('0' + (char)(number % 10));
|
||||
else
|
||||
code = (char)('A' + (char)(number % 26));
|
||||
if (code.Equals('o') || code.Equals('0'))//去除O或者0
|
||||
{
|
||||
length += 1;
|
||||
continue;
|
||||
}
|
||||
stringBuilder.Append(code);
|
||||
}
|
||||
return stringBuilder.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 得到分页大小
|
||||
/// </summary>
|
||||
/// <param name="setCookie">是否写入COOKIE</param>
|
||||
/// <returns></returns>
|
||||
public static int GetPageSize(bool setCookie = true)
|
||||
{
|
||||
string size = HttpContext.Request.Forms("pagesize");
|
||||
if (!size.IsInt())
|
||||
{
|
||||
size = HttpContext.Request.Querys("pagesize");
|
||||
}
|
||||
if (!size.IsInt())
|
||||
{
|
||||
if (HttpContext.Request.Cookies.TryGetValue("roadflowcorepagesize", out string s))
|
||||
{
|
||||
size = s;
|
||||
}
|
||||
}
|
||||
int size2 = size.IsInt(out int size1) ? size1 : Config.PageSize;
|
||||
if (size2 <= 0)
|
||||
{
|
||||
size2 = Config.PageSize;
|
||||
}
|
||||
if (setCookie)
|
||||
{
|
||||
HttpContext.Response.Cookies.Append("roadflowcorepagesize", size2.ToString(), new CookieOptions() { Expires = DateExtensions.Now.AddYears(5) });
|
||||
}
|
||||
return size2;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 得到页号
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static int GetPageNumber()
|
||||
{
|
||||
string number = HttpContext.Request.Forms("pagenumber");
|
||||
if (!number.IsInt())
|
||||
{
|
||||
number = HttpContext.Request.Querys("pagenumber");
|
||||
}
|
||||
return number.IsInt(out int number1) ? number1 : 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 得到验证码图片
|
||||
/// </summary>
|
||||
/// <param name="code"></param>
|
||||
/// <param name="bgImg">背景图片,完整路径</param>
|
||||
/// <returns></returns>
|
||||
public static System.IO.MemoryStream GetValidateImg(out string code, string bgImg)
|
||||
{
|
||||
code = GetRandomString(4);
|
||||
Random rnd = new Random();
|
||||
System.Drawing.Bitmap img = new System.Drawing.Bitmap(78, 32);
|
||||
System.Drawing.Image bg = System.Drawing.Bitmap.FromFile(bgImg);
|
||||
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(img);
|
||||
System.Drawing.Font font = new System.Drawing.Font("Arial", 20, (System.Drawing.FontStyle.Regular | System.Drawing.FontStyle.Italic));
|
||||
System.Drawing.Font fontbg = new System.Drawing.Font("Arial", 20, (System.Drawing.FontStyle.Regular | System.Drawing.FontStyle.Italic));
|
||||
g.DrawImage(bg, 0, 0, new System.Drawing.Rectangle(rnd.Next(bg.Width - img.Width), rnd.Next(bg.Height - img.Height), img.Width, img.Height), System.Drawing.GraphicsUnit.Pixel);
|
||||
g.DrawString(code, fontbg, System.Drawing.Brushes.White, 0, 1);
|
||||
g.DrawString(code, font, System.Drawing.Brushes.ForestGreen, 0, 1);//字颜色
|
||||
|
||||
//画图片的背景噪音线
|
||||
int x = img.Width;
|
||||
int y1 = rnd.Next(5, img.Height);
|
||||
int y2 = rnd.Next(5, img.Height);
|
||||
g.DrawLine(new System.Drawing.Pen(System.Drawing.Color.Green, 2), 1, y1, x - 2, y2);
|
||||
g.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.Transparent), 0, 10, img.Width - 1, img.Height - 1);
|
||||
System.IO.MemoryStream ms = new System.IO.MemoryStream();
|
||||
img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
|
||||
return ms;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断当前是否是手机访问
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static bool IsPhoneAccess(HttpRequest request)
|
||||
{
|
||||
if (null == request)
|
||||
{
|
||||
var context = HttpContext;
|
||||
if (null != context)
|
||||
{
|
||||
request = context.Request;
|
||||
}
|
||||
}
|
||||
if (null == request)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
string u = request.Headers["User-Agent"];
|
||||
if (u.IsNullOrEmpty())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
Regex b = new Regex(@"(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino", RegexOptions.IgnoreCase | RegexOptions.Multiline);
|
||||
Regex v = new Regex(@"1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-", RegexOptions.IgnoreCase | RegexOptions.Multiline);
|
||||
Regex w = new Regex(@".*wechat.*(\r\n)?", RegexOptions.IgnoreCase | RegexOptions.Multiline);
|
||||
return (b.IsMatch(u) || v.IsMatch(u.Substring(0, 4)) || w.IsMatch(u));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断是否是移动端页面访问(VUE版本)
|
||||
/// </summary>
|
||||
/// <param name="httpContext"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsMobileVue(HttpContext httpContext)
|
||||
{
|
||||
if (httpContext == null)
|
||||
{
|
||||
httpContext = HttpContext;
|
||||
}
|
||||
if (httpContext == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return IsPhoneAccess(httpContext.Request);
|
||||
|
||||
//if ("1" == httpContext.Request.Querys("ismobile"))
|
||||
//{
|
||||
// return true;
|
||||
//}
|
||||
//if (httpContext.Request.Querys("loadurl").ContainsIgnoreCase("/mobile/"))
|
||||
//{
|
||||
// return true;
|
||||
//}
|
||||
//string refererString = string.Empty;
|
||||
//if (httpContext.Request.Headers.TryGetValue("Referer", out StringValues referer))
|
||||
//{
|
||||
// refererString = referer.ToString();
|
||||
//}
|
||||
//return !refererString.IsNullOrWhiteSpace()
|
||||
// && (refererString.UrlDecode().ContainsIgnoreCase("?loadurl=/mobile/")
|
||||
// || refererString.UrlDecode().ContainsIgnoreCase("?loadurl=/flow/run/index"));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断字符串表达式
|
||||
/// </summary>
|
||||
/// <param name="code"></param>
|
||||
/// <returns></returns>
|
||||
public static object ExecuteExpression(string Expression)
|
||||
{
|
||||
if (Expression.IsNullOrWhiteSpace())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
string exp = Expression.Trim();
|
||||
if (exp.EndsWith("and", StringComparison.CurrentCultureIgnoreCase))
|
||||
{
|
||||
exp = exp.TrimEnd("and".ToCharArray());
|
||||
}
|
||||
if (exp.EndsWith("or", StringComparison.CurrentCultureIgnoreCase))
|
||||
{
|
||||
exp = exp.TrimEnd("or".ToCharArray());
|
||||
}
|
||||
System.Data.DataTable dt = new System.Data.DataTable();
|
||||
return dt.Compute(exp, "");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 得到web目录绝对路径(包含wwwroot,最后没有\)
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static string GetWebRootPath()
|
||||
{
|
||||
return null == _hostingEnvironment ? string.Empty : _hostingEnvironment.WebRootPath;
|
||||
}
|
||||
/// <summary>
|
||||
/// 得到站点目录绝对路径
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static string GetContentRootPath()
|
||||
{
|
||||
return Config.ContentRootPath.IsNullOrWhiteSpace()
|
||||
? null == _hostingEnvironment ? string.Empty : _hostingEnvironment.ContentRootPath : Config.ContentRootPath;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 得到当前主题
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static string GetCurrentTheme()
|
||||
{
|
||||
var httpContext = HttpContext;
|
||||
if (null == httpContext)
|
||||
{
|
||||
return "blue";
|
||||
}
|
||||
return httpContext.Request.Cookies.TryGetValue("rf_core_theme", out string theme) ? theme : "blue";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行反射
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
/// <param name="args"></param>
|
||||
/// <returns>object 执行结果,Exception 如果执行发生错误时的错误信息,为空表示成功</returns>
|
||||
public static (object, Exception) ExecuteMethod(string method, params object[] args)
|
||||
{
|
||||
var _memoryCache = App.GetService<IMemoryCache>();
|
||||
string dllName = string.Empty;
|
||||
Assembly assembly = GetAssembly(_memoryCache, method, out dllName);
|
||||
if (null == assembly)
|
||||
{
|
||||
assembly = GetAssemblyByFile(_memoryCache, method, out dllName);
|
||||
}
|
||||
if (null == assembly)
|
||||
{
|
||||
return (null, new Exception("未能载入资源-" + dllName));
|
||||
}
|
||||
string typeName = method.Substring(0, method.LastIndexOf('.'));
|
||||
string methodName = method.Substring(method.LastIndexOf('.') + 1);
|
||||
try
|
||||
{
|
||||
Type type = assembly.GetType(typeName, true);
|
||||
object instance = Activator.CreateInstance(type, false);
|
||||
return (type.GetMethod(methodName).Invoke(instance, args), null);
|
||||
}
|
||||
catch (Exception err)
|
||||
{
|
||||
err.Source += " 类型:" + typeName + " 方法:" + methodName + " DLL:" + dllName;
|
||||
return (null, err);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过字符反射加载DLL对象
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
/// <returns></returns>
|
||||
public static Assembly GetAssembly(IMemoryCache _memoryCache, string name, out string dllName)
|
||||
{
|
||||
dllName = string.Empty;
|
||||
//检查缓存中是否有该对象
|
||||
string key = "assembly_" + name;
|
||||
var obj = _memoryCache.Get(key);
|
||||
if (null != obj)
|
||||
{
|
||||
return (Assembly)obj;
|
||||
}
|
||||
|
||||
if (name.IsNullOrWhiteSpace())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
string[] names = name.Split('.');
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
Assembly assembly = null;
|
||||
foreach (string n in names)
|
||||
{
|
||||
try
|
||||
{
|
||||
stringBuilder.Append(n);
|
||||
stringBuilder.Append(".");
|
||||
dllName = stringBuilder.ToString().TrimEnd('.');
|
||||
assembly = Assembly.Load(dllName);
|
||||
if (null != assembly)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
_memoryCache.Set(key, assembly);
|
||||
return assembly;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载程序集外的DLL
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
/// <param name="dllName"></param>
|
||||
/// <returns></returns>
|
||||
public static Assembly GetAssemblyByFile(IMemoryCache _memoryCache, string name, out string dllName)
|
||||
{
|
||||
dllName = string.Empty;
|
||||
//检查缓存中是否有该对象
|
||||
string key = "assembly_" + name;
|
||||
var obj = _memoryCache.Get(key);
|
||||
if (null != obj)
|
||||
{
|
||||
return (Assembly)obj;
|
||||
}
|
||||
|
||||
if (name.IsNullOrWhiteSpace())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
string[] names = name.Split('.');
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
Assembly assembly = null;
|
||||
string path = GetContentRootPath();
|
||||
foreach (string n in names)
|
||||
{
|
||||
try
|
||||
{
|
||||
stringBuilder.Append(n);
|
||||
stringBuilder.Append(".");
|
||||
dllName = path + "\\" + stringBuilder.ToString().TrimEnd('.') + ".dll";
|
||||
assembly = Assembly.LoadFrom(dllName);
|
||||
if (null != assembly)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
_memoryCache.Set(key, assembly);
|
||||
return assembly;
|
||||
}
|
||||
|
||||
#region 语言相关
|
||||
/// <summary>
|
||||
/// 得到当前语言
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static string GetCurrentLanguage(HttpContext context = null)
|
||||
{
|
||||
//如果默认语言选项为空,说明配置文件中没有设置语言(不是多语言版)直接返回简体中文
|
||||
if (Config.Language_Default.IsNullOrWhiteSpace())
|
||||
{
|
||||
return "zh-CN";
|
||||
}
|
||||
string language = GetCookieLanguage(context);
|
||||
return language.IsNullOrWhiteSpace() ? Config.Language_Default : language;
|
||||
}
|
||||
/// <summary>
|
||||
/// 从COOKIE得到当前语言
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static string GetCookieLanguage(HttpContext context = null)
|
||||
{
|
||||
string language = string.Empty;
|
||||
HttpContext httpContext = context ?? HttpContext;
|
||||
if (httpContext != null && httpContext.Request.Cookies.TryGetValue(Config.Language_CookieName.IsNullOrWhiteSpace() ? ".AspNetCore.Culture" : Config.Language_CookieName, out string def))
|
||||
{
|
||||
string[] defs = def.Split('|');
|
||||
if (defs.Length > 0)
|
||||
{
|
||||
language = defs[0].TrimStart('c', '=').Trim();
|
||||
}
|
||||
}
|
||||
return language;
|
||||
}
|
||||
/// <summary>
|
||||
/// 得到语言下拉选项
|
||||
/// </summary>
|
||||
/// <param name="defaultLanuage"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetLanuageOptions(string defaultLanuage = "")
|
||||
{
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
foreach (var lan in Config.Language_Dictionary)
|
||||
{
|
||||
stringBuilder.Append("<option value=\"" + lan.Key + "\"" + (defaultLanuage.Equals(lan.Key) ? " selected=\"selected\"" : "") + ">" + lan.Value + "</option>");
|
||||
}
|
||||
return stringBuilder.ToString();
|
||||
}
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user