This commit is contained in:
454313500@qq.com
2021-05-13 01:39:34 +08:00
parent 2e5b991db0
commit fe850bbc2c
53 changed files with 1318 additions and 404 deletions

View File

@@ -1,11 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Autofac.Extras.DynamicProxy" Version="6.0.0" />
<PackageReference Include="Castle.Core" Version="4.4.1" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="NLog.Web.AspNetCore" Version="4.11.0" />
<PackageReference Include="ServiceStack.Redis" Version="5.10.4" />
<PackageReference Include="System.Drawing.Common" Version="5.0.2" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,51 @@
using Autofac;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Text;
namespace CC.Yi.Common.Cache
{
public class CacheHelper
{
public static ICacheWriter CacheWriter { get; set; }
static CacheHelper()
{
CacheHelper.CacheWriter = new RedisCache();
}
public bool AddCache<T>(string key, T value, DateTime expDate)
{
return CacheWriter.AddCache<T>(key,value,expDate);
}
public bool AddCache<T>(string key, T value)
{
return CacheWriter.AddCache<T>(key, value);
}
public bool RemoveCache(string key)
{
return CacheWriter.RemoveCache(key);
}
public T GetCache<T>(string key)
{
return CacheWriter.GetCache<T>(key);
}
public bool SetCache<T>(string key, T value, DateTime expDate)
{
return CacheWriter.SetCache<T>(key,value,expDate);
}
public bool SetCache<T>(string key, T value)
{
return CacheWriter.SetCache<T>(key, value);
}
}
}

View File

@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CC.Yi.Common.Cache
{
public interface ICacheWriter
{
bool AddCache<T>(string key, T value, DateTime expDate);
bool AddCache<T>(string key, T value);
bool RemoveCache(string key);
T GetCache<T>(string key);
bool SetCache<T>(string key, T value, DateTime expDate);
bool SetCache<T>(string key, T value);
}
}

View File

@@ -0,0 +1,46 @@
using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Text;
namespace CC.Yi.Common.Cache
{
public class RedisCache : ICacheWriter
{
private RedisClient client;
public RedisCache()
{
client = new RedisClient("127.0.0.1", 6379, "52013142020.");
}
public bool AddCache<T>(string key, T value, DateTime expDate)
{
return client.Add<T>(key, value, expDate);
}
public bool AddCache<T>(string key, T value)
{
return client.Add<T>(key, value);
}
public bool RemoveCache(string key)
{
return client.Remove(key);
}
public T GetCache<T>(string key)
{
return client.Get<T>(key);
}
public bool SetCache<T>(string key,T value, DateTime expDate)
{
return client.Set<T>(key, value, expDate);
}
public bool SetCache<T>(string key, T value)
{
return client.Set<T>(key, value);
}
}
}

View File

@@ -0,0 +1,23 @@
using Castle.DynamicProxy;
using System;
using System.Collections.Generic;
using System.Text;
namespace CC.Yi.Common.Castle
{
public class CustomAutofacAop : IInterceptor
{
public void Intercept(IInvocation invocation)
{
{
//这里写执行方法前
}
invocation.Proceed();//执行具体的实例
{
//这里写执行方法后
}
}
}
}

View File

@@ -0,0 +1,76 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace CC.Yi.Common
{
public static class HttpHelper
{
public static string HttpGet(string Url, string postDataStr="")
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return retString;
}
public static bool HttpIOGet(string Url, string file, string postDataStr="")
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
FileStream writer = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Write);
byte[] buffer = new byte[1024];
int c;
while ((c = myResponseStream.Read(buffer, 0, buffer.Length)) > 0)
{
writer.Write(buffer, 0, c);
}
writer.Close();
myResponseStream.Close();
return true;
}
public static string HttpPost(string Url, string postDataStr="")
{
CookieContainer cookie = new CookieContainer();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
request.CookieContainer = cookie;
Stream myRequestStream = request.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
myStreamWriter.Write(postDataStr);
myStreamWriter.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
response.Cookies = cookie.GetCookies(response.ResponseUri);
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return retString;
}
}
}

View File

@@ -1,12 +0,0 @@
using System;
namespace CC.Yi.Common
{
public static class JsonFactory
{
public static string JsonToString(object q)
{
return Newtonsoft.Json.JsonConvert.SerializeObject(q);
}
}
}

View File

@@ -0,0 +1,24 @@
using System;
namespace CC.Yi.Common
{
public static class JsonHelper
{
public static string JsonToString(object data = null, int code = 200, bool flag = true, string message = "成功")
{
return Newtonsoft.Json.JsonConvert.SerializeObject(new { code = code, flag = flag, message = message, data = data });
}
public static string JsonToString2(object data = null, int code = 200, bool flag = true, string message = "成功", int count = 0)
{
return Newtonsoft.Json.JsonConvert.SerializeObject(new { code = code, flag = flag, message = message, count = count, data = data });
}
public static string ToString(object data)
{
return Newtonsoft.Json.JsonConvert.SerializeObject(data);
}
public static T ToJson<T>(string data)
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(data);
}
}
}

View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace CC.Yi.Common.Jwt
{
public class JwtConst
{
public const string SecurityKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDI2a2EJ7m872v0afyoSDJT2o1+SitIeJSWtLJU8/Wz2m7gStexajkeD+Lka6DSTy8gt9UwfgVQo6uKjVLG5Ex7PiGOODVqAEghBuS7JzIYU5RvI543nNDAPfnJsas96mSA7L/mD7RTE2drj6hf3oZjJpMPZUQI/B1Qjb5H3K3PNwIDAQAB";
public const string Domain = "http://localhost:5000";
}
}

40
CC.Yi.Common/Result.cs Normal file
View File

@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace CC.Yi.Common
{
/// <summary>
/// 结果数据
/// </summary>
public class Result
{
public bool status { get; set; }
public int code { get; set; }
public string msg { get; set; }
public object data { get; set; }
public static Result Instance(bool status, string msg)
{
return new Result() { status = status,code=500, msg = msg };
}
public static Result Error(string msg="fail")
{
return new Result() { status = false, code = 500, msg = msg };
}
public static Result Success(string msg= "succeed")
{
return new Result() { status = true, code = 200, msg = msg };
}
public Result SetData(object obj)
{
this.data = obj;
return this;
}
public Result SetCode(int Code)
{
this.code = Code;
return this;
}
}
}

167
CC.Yi.Common/imageHelper.cs Normal file
View File

@@ -0,0 +1,167 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.IO;
using System.IO.Compression;
using System.Drawing;
namespace CC.Yi.Common
{
public class SimilarPhoto
{
Image SourceImg;
public SimilarPhoto(string filePath)
{
SourceImg = Image.FromFile(filePath);
}
public SimilarPhoto(Stream stream)
{
SourceImg = Image.FromStream(stream);
}
public String GetHash()
{
Image image = ReduceSize();
Byte[] grayValues = ReduceColor(image);
Byte average = CalcAverage(grayValues);
String reslut = ComputeBits(grayValues, average);
return reslut;
}
// Step 1 : Reduce size to 8*8
private Image ReduceSize(int width = 8, int height = 8)
{
Image image = SourceImg.GetThumbnailImage(width, height, () => { return false; }, IntPtr.Zero);
return image;
}
// Step 2 : Reduce Color
private Byte[] ReduceColor(Image image)
{
Bitmap bitMap = new Bitmap(image);
Byte[] grayValues = new Byte[image.Width * image.Height];
for (int x = 0; x < image.Width; x++)
for (int y = 0; y < image.Height; y++)
{
Color color = bitMap.GetPixel(x, y);
byte grayValue = (byte)((color.R * 30 + color.G * 59 + color.B * 11) / 100);
grayValues[x * image.Width + y] = grayValue;
}
return grayValues;
}
// Step 3 : Average the colors
private Byte CalcAverage(byte[] values)
{
int sum = 0;
for (int i = 0; i < values.Length; i++)
sum += (int)values[i];
return Convert.ToByte(sum / values.Length);
}
// Step 4 : Compute the bits
private String ComputeBits(byte[] values, byte averageValue)
{
char[] result = new char[values.Length];
for (int i = 0; i < values.Length; i++)
{
if (values[i] < averageValue)
result[i] = '0';
else
result[i] = '1';
}
SourceImg.Dispose();
return new String(result);
}
// Compare hash
public static Int32 CalcSimilarDegree(string a, string b)
{
if (a.Length != b.Length)
throw new ArgumentException();
int count = 0;
for (int i = 0; i < a.Length; i++)
{
if (a[i] != b[i])
count++;
}
return count;
}
}
public static class imageHelper
{
public static int Compare(string filePath1, string filePath2)
{
SimilarPhoto photo1 = new SimilarPhoto(filePath1);
SimilarPhoto photo2 = new SimilarPhoto(filePath2);
return SimilarPhoto.CalcSimilarDegree(photo1.GetHash(), photo2.GetHash());
}
public static bool ByStringToSave(string name, string iss)
{
iss = iss.Replace("data:image/png;base64,", "").Replace("data:image/jgp;base64,", "")
.Replace("data:image/jpg;base64,", "").Replace("data:image/jpeg;base64,", "");
byte[] arr = Convert.FromBase64String(iss);
MemoryStream ms = new MemoryStream(arr);
Bitmap bmp = new Bitmap(ms);
string StudentWorkImages = "StudentWorkImages";
if (Directory.Exists(@"./wwwroot/" + StudentWorkImages) == false)//如果不存在就创建file文件夹
{
Directory.CreateDirectory(@"./wwwroot/" + StudentWorkImages);
}
bmp.Save(@"./wwwroot/" + StudentWorkImages + "/" + name + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
ms.Close();
return true;
}
public static bool CreateZip()
{
string file_path = @"./wwwroot/StudentWorkImages.zip";
string file_path2 = @"./wwwroot/StudentWorkImages/";
if (File.Exists(file_path))
{
File.Delete(file_path);
}
ZipFile.CreateFromDirectory(file_path2, file_path);
return true;
}
public static bool DeleteAll()
{
string file_path = @"./wwwroot/StudentWorkImages/";
if (Directory.Exists(file_path))
{
DelectDir(file_path);
return true;
}
else
return false;
}
public static bool DeleteByString(string name)
{
File.Delete(@"./wwwroot/StudentWorkImages/" + name + ".jpg");
return true;
}
public static void DelectDir(string srcPath)
{
try
{
DirectoryInfo dir = new DirectoryInfo(srcPath);
FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //返回目录中所有文件和子目录
foreach (FileSystemInfo i in fileinfo)
{
if (i is DirectoryInfo) //判断是否文件夹
{
DirectoryInfo subdir = new DirectoryInfo(i.FullName);
subdir.Delete(true); //删除子目录和文件
}
else
{
File.Delete(i.FullName); //删除指定文件
}
}
}
catch (Exception e)
{
Console.Write(e.ToString());
}
}
}
}