初始化
This commit is contained in:
85
WebAPIServer/Extensions/CaptchaHelperSkiaSharp.cs
Normal file
85
WebAPIServer/Extensions/CaptchaHelperSkiaSharp.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
using System.Text;
|
||||
using SkiaSharp;
|
||||
using static IronPython.Modules._ast;
|
||||
|
||||
namespace WebAPIServer.Extensions
|
||||
{
|
||||
public class CaptchaHelperSkiaSharp
|
||||
{
|
||||
private static string GenerateRandomText(int length)
|
||||
{
|
||||
const string valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
StringBuilder res = new StringBuilder();
|
||||
Random rnd = new Random();
|
||||
while (0 < length--)
|
||||
res.Append(valid[rnd.Next(valid.Length)]);
|
||||
return res.ToString();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取图像数字验证码
|
||||
/// </summary>
|
||||
/// <param name="text">验证码内容,如4为数字</param>
|
||||
/// <returns></returns>
|
||||
public static byte[] GetVerifyCode(string text)
|
||||
{
|
||||
|
||||
int width = 128;
|
||||
int height = 45;
|
||||
|
||||
Random random = new();
|
||||
|
||||
//创建bitmap位图
|
||||
using SKBitmap image = new(width, height, SKColorType.Bgra8888, SKAlphaType.Premul);
|
||||
//创建画笔
|
||||
using SKCanvas canvas = new(image);
|
||||
//填充背景颜色为白色
|
||||
canvas.DrawColor(SKColors.White);
|
||||
|
||||
//画图片的背景噪音线
|
||||
for (int i = 0; i < (width * height * 0.015); i++)
|
||||
{
|
||||
using SKPaint drawStyle = new();
|
||||
drawStyle.Color = new(Convert.ToUInt32(random.Next(Int32.MaxValue)));
|
||||
|
||||
canvas.DrawLine(random.Next(0, width), random.Next(0, height), random.Next(0, width), random.Next(0, height), drawStyle);
|
||||
}
|
||||
|
||||
//将文字写到画布上
|
||||
using (SKPaint drawStyle = new())
|
||||
{
|
||||
drawStyle.Color = SKColors.Red;
|
||||
drawStyle.TextSize = height;
|
||||
drawStyle.StrokeWidth = 1;
|
||||
|
||||
float emHeight = height - (float)height * (float)0.14;
|
||||
float emWidth = ((float)width / text.Length) - ((float)width * (float)0.13);
|
||||
|
||||
canvas.DrawText(text, emWidth, emHeight, drawStyle);
|
||||
}
|
||||
|
||||
//画图片的前景噪音点
|
||||
for (int i = 0; i < (width * height * 0.6); i++)
|
||||
{
|
||||
image.SetPixel(random.Next(0, width), random.Next(0, height), new SKColor(Convert.ToUInt32(random.Next(Int32.MaxValue))));
|
||||
}
|
||||
|
||||
using var img = SKImage.FromBitmap(image);
|
||||
using SKData p = img.Encode(SKEncodedImageFormat.Png, 100);
|
||||
return p.ToArray();
|
||||
}
|
||||
|
||||
public static byte[] GenerateCaptchaImage(int width = 100, int height = 40)
|
||||
{
|
||||
var random = new Random();
|
||||
string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||
string captchaText = "";
|
||||
for (int i = 0; i < 4; i++) // 生成6个字符的验证码
|
||||
{
|
||||
captchaText += chars[random.Next(chars.Length)];
|
||||
}
|
||||
return GetVerifyCode(captchaText);
|
||||
}
|
||||
}
|
||||
}
|
||||
346
WebAPIServer/Extensions/StringExtensions.cs
Normal file
346
WebAPIServer/Extensions/StringExtensions.cs
Normal file
@@ -0,0 +1,346 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Drawing.Imaging;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WebAPIServer.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 字符串扩展函数工具类
|
||||
/// </summary>
|
||||
public static class StringExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 将\r\n替换成BR
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static string ToSafeBR(this string str)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(str))
|
||||
{
|
||||
return str.Replace("\r\n", "<br>").Replace("\r", "<br>").Replace("\n", "<br>");
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 得到父部门的ID
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetDepartmentFatherID(this string str)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(str) && str.Length > 2)
|
||||
{
|
||||
str = str.Substring(0, str.Length - 3);
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 截取字符串,超过部分用...代替
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="len"></param>
|
||||
/// <returns></returns>
|
||||
public static string Substr(this string source, int len)
|
||||
{
|
||||
return source.Substr(len, "...");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 截取字符串,超过部分用自定义代替
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="len"></param>
|
||||
/// <param name="att"></param>
|
||||
/// <returns></returns>
|
||||
public static string Substr(this string source, int len, string att)
|
||||
{
|
||||
if (string.IsNullOrEmpty(source))
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
att = (att ?? string.Empty);
|
||||
Regex regex = new Regex("[\\u4e00-\\u9fa5]");
|
||||
Regex regex2 = new Regex("^[A-Za-z0-9]+$");
|
||||
if (regex.IsMatch(source))
|
||||
{
|
||||
if (source.Length <= len)
|
||||
{
|
||||
return source;
|
||||
}
|
||||
return source.Substring(0, len) + att;
|
||||
}
|
||||
else if (regex2.IsMatch(source))
|
||||
{
|
||||
if (source.Length <= len * 2)
|
||||
{
|
||||
return source;
|
||||
}
|
||||
return source.Substring(0, len * 2) + att;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (source.Length <= len)
|
||||
{
|
||||
return source;
|
||||
}
|
||||
return source.Substring(0, len) + att;
|
||||
}
|
||||
}
|
||||
|
||||
public static string InputStr(this string source)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(source))
|
||||
{
|
||||
Regex regex = new Regex("[\\u4e00-\\u9fa5]");
|
||||
Regex regex2 = new Regex("^[A-Za-z0-9]+$");
|
||||
if (regex.IsMatch(source))
|
||||
{
|
||||
if (source.Length < 3)
|
||||
{
|
||||
return string.Format("{0}**", source[0]);
|
||||
}
|
||||
if (source.Length == 3)
|
||||
{
|
||||
return string.Format("{0}*{1}", source[0], source[source.Length - 1]);
|
||||
}
|
||||
if (source.Length > 3)
|
||||
{
|
||||
return string.Format("{0}**{1}", source[0], source[source.Length - 1]);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!regex2.IsMatch(source))
|
||||
{
|
||||
return string.Format("{0}**", source.Substring(0, 2));
|
||||
}
|
||||
if (source.Length < 6)
|
||||
{
|
||||
return string.Format("{0}**", source.Substring(0, 2));
|
||||
}
|
||||
return string.Format("{0}****{1}", source.Substring(0, 2), source.Substring(source.Length - 3, 2));
|
||||
}
|
||||
}
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除掉所有的Html代码
|
||||
/// </summary>
|
||||
/// <param name="strHtml"></param>
|
||||
/// <returns></returns>
|
||||
public static string RemoveHtml(this string strHtml)
|
||||
{
|
||||
Regex regex = new Regex("<.+?>", RegexOptions.IgnoreCase);
|
||||
strHtml = regex.Replace(strHtml, "");
|
||||
strHtml = strHtml.Replace(" ", "");
|
||||
return strHtml;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成0-9随机数
|
||||
/// </summary>
|
||||
/// <param name="VcodeNum">生成长度</param>
|
||||
/// <returns></returns>
|
||||
public static string RndNum(int VcodeNum)
|
||||
{
|
||||
StringBuilder stringBuilder = new StringBuilder(VcodeNum);
|
||||
Random random = new Random();
|
||||
for (int i = 1; i < VcodeNum + 1; i++)
|
||||
{
|
||||
int num = random.Next(9);
|
||||
stringBuilder.AppendFormat("{0}", num);
|
||||
}
|
||||
return stringBuilder.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 返回星号的加密
|
||||
/// </summary>
|
||||
/// <param name="items"></param>
|
||||
/// <param name="mask"></param>
|
||||
/// <returns></returns>
|
||||
public static string MaskStar(Dictionary<string, string> items, bool mask)
|
||||
{
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
if (mask)
|
||||
{
|
||||
foreach (KeyValuePair<string, string> keyValuePair in items)
|
||||
{
|
||||
stringBuilder.Append(string.Concat(new string[]
|
||||
{
|
||||
"$('#",
|
||||
keyValuePair.Key,
|
||||
"').attr('name', '",
|
||||
keyValuePair.Key,
|
||||
"mask');"
|
||||
}));
|
||||
}
|
||||
stringBuilder.Append("$('.maskstar').attr('disabled', true);");
|
||||
stringBuilder.Append("$('.maskstar').val('***');");
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (KeyValuePair<string, string> keyValuePair2 in items)
|
||||
{
|
||||
stringBuilder.Append(string.Concat(new string[]
|
||||
{
|
||||
"$('#",
|
||||
keyValuePair2.Key,
|
||||
"').attr('name', '",
|
||||
keyValuePair2.Key,
|
||||
"');"
|
||||
}));
|
||||
stringBuilder.Append(string.Concat(new string[]
|
||||
{
|
||||
"$('#",
|
||||
keyValuePair2.Key,
|
||||
"').val('",
|
||||
keyValuePair2.Value,
|
||||
"');"
|
||||
}));
|
||||
}
|
||||
stringBuilder.Append("$('.maskstar').attr('disabled', false);");
|
||||
}
|
||||
return stringBuilder.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 给自动填充使用
|
||||
/// </summary>
|
||||
/// <param name="str1"></param>
|
||||
/// <param name="str2"></param>
|
||||
/// <returns></returns>
|
||||
public static string ToAutoComplate(this string str1, string str2)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(str1) && !string.IsNullOrEmpty(str2))
|
||||
{
|
||||
return str1 + "," + str2;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 返回红色字体
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
public static string ToRedColor(this int value)
|
||||
{
|
||||
if (value != 0)
|
||||
{
|
||||
return "<font color='red'>" + value.ToString() + "</font>";
|
||||
}
|
||||
return value.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 返回安全的字符串类型如果为NULL则返回空
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
public static string ToSafeString(this object value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
return value.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将中文转换成Unicode编码,主要用在URL传递中文
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static string GB2Unicode(this string str)
|
||||
{
|
||||
string text = "";
|
||||
Encoding encoding = Encoding.GetEncoding("GB2312");
|
||||
Encoding unicode = Encoding.Unicode;
|
||||
byte[] bytes = encoding.GetBytes(str);
|
||||
for (int i = 0; i < bytes.Length; i++)
|
||||
{
|
||||
string str2 = "%" + bytes[i].ToString("x");
|
||||
text += str2;
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将字符串转换成为大写的MD5
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static string ToMD5(this string str)
|
||||
{
|
||||
MD5CryptoServiceProvider md5CryptoServiceProvider = new MD5CryptoServiceProvider();
|
||||
byte[] array = md5CryptoServiceProvider.ComputeHash(Encoding.Default.GetBytes(str));
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
foreach (byte b in array)
|
||||
{
|
||||
stringBuilder.Append(b.ToString("x2"));
|
||||
}
|
||||
return stringBuilder.ToString().ToUpper();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 字节码长度转可读字符串 00000000 bytes 0.0GB
|
||||
/// </summary>
|
||||
/// <param name="KSize"></param>
|
||||
/// <returns></returns>
|
||||
public static string ByteSizeToString(this long KSize)
|
||||
{
|
||||
if (KSize > 0L)
|
||||
{
|
||||
string[] array = new string[]
|
||||
{
|
||||
"B",
|
||||
"KB",
|
||||
"MB",
|
||||
"GB",
|
||||
"TB"
|
||||
};
|
||||
double num = 0.0;
|
||||
int num2 = array.Length - 1;
|
||||
while (num2 >= 0 && (num = Math.Round((double)KSize / Math.Pow(1024.0, (double)num2), 2)) < 1.0)
|
||||
{
|
||||
num2--;
|
||||
}
|
||||
return string.Format("{0}{1}", num, array[num2]);
|
||||
}
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 将字符串转换成为大写的MD5
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
private static string GetMD5Code(this string str)
|
||||
{
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
using (MD5CryptoServiceProvider md5CryptoServiceProvider = new MD5CryptoServiceProvider())
|
||||
{
|
||||
byte[] array = md5CryptoServiceProvider.ComputeHash(Encoding.Default.GetBytes(str));
|
||||
foreach (byte b in array)
|
||||
{
|
||||
stringBuilder.Append(b.ToString("x2"));
|
||||
}
|
||||
}
|
||||
return stringBuilder.ToString();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
29
WebAPIServer/Extensions/TimeExtensions.cs
Normal file
29
WebAPIServer/Extensions/TimeExtensions.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WebAPIServer.Extensions
|
||||
{
|
||||
public static class TimeExtensions
|
||||
{
|
||||
public static DateTime? ToDateTime(this string timeStamp)
|
||||
{
|
||||
long ticks = 0L;
|
||||
if (long.TryParse(timeStamp, out ticks))
|
||||
{
|
||||
DateTime dateTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
|
||||
TimeSpan value = new TimeSpan(ticks);
|
||||
return new DateTime?(dateTime.Add(value));
|
||||
}
|
||||
DateTime now = DateTime.Now;
|
||||
if (DateTime.TryParse(timeStamp, out now))
|
||||
{
|
||||
return new DateTime?(now);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user