feat:furion rbac搭建

This commit is contained in:
橙子
2023-04-13 21:12:06 +08:00
parent 18696ec542
commit b9dad93c9d
194 changed files with 9557 additions and 75 deletions

View File

@@ -0,0 +1,99 @@
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.AspNetCore
{
public static class HttpContextExtensions
{
/// <summary>
/// 设置文件下载名称
/// </summary>
/// <param name="httpContext"></param>
/// <param name="fileName"></param>
public static void FileInlineHandle(this HttpContext httpContext, string fileName)
{
string encodeFilename = System.Web.HttpUtility.UrlEncode(fileName, Encoding.GetEncoding("UTF-8"));
httpContext.Response.Headers.Add("Content-Disposition", "inline;filename=" + encodeFilename);
}
/// <summary>
/// 设置文件附件名称
/// </summary>
/// <param name="httpContext"></param>
/// <param name="fileName"></param>
public static void FileAttachmentHandle(this HttpContext httpContext, string fileName)
{
string encodeFilename = System.Web.HttpUtility.UrlEncode(fileName, Encoding.GetEncoding("UTF-8"));
httpContext.Response.Headers.Add("Content-Disposition", "attachment;filename=" + encodeFilename);
}
/// <summary>
/// 获取语言种类
/// </summary>
/// <param name="httpContext"></param>
/// <returns></returns>
public static string GetLanguage(this HttpContext httpContext)
{
string res = "zh-CN";
var str = httpContext.Request.Headers["Accept-Language"].FirstOrDefault();
if (str is not null)
{
res = str.Split(",")[0];
}
return res;
}
/// <summary>
/// 判断是否为异步请求
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public static bool IsAjaxRequest(this HttpRequest request)
{
string header = request.Headers["X-Requested-With"];
return "XMLHttpRequest".Equals(header);
}
/// <summary>
/// 获取客户端IP
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public static string GetClientIp(this HttpContext context)
{
if (context == null) return "";
var result = context.Request.Headers["X-Forwarded-For"].FirstOrDefault();
if (string.IsNullOrEmpty(result))
{
result = context.Connection.RemoteIpAddress?.ToString();
}
if (string.IsNullOrEmpty(result) || result.Contains("::1"))
result = "127.0.0.1";
result = result.Replace("::ffff:", "127.0.0.1");
//Ip规则效验
var regResult = Regex.IsMatch(result, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
result = regResult ? result : "127.0.0.1";
return result;
}
/// <summary>
/// 获取浏览器标识
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public static string GetUserAgent(this HttpContext context)
{
return context.Request.Headers["User-Agent"];
}
}
}

View File

@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Data.Auditing
{
public class AuditedObject : IAuditedObject
{
public DateTime CreationTime { get; set; } = DateTime.Now;
public long? CreatorId { get; set; }
public long? LastModifierId { get; set; }
public DateTime? LastModificationTime { get; set; }
}
}

View File

@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Data.Auditing
{
public interface IAuditedObject : ICreationAuditedObject, IModificationAuditedObject
{
}
public interface IAuditedObject<TUser> : IAuditedObject, ICreationAuditedObject<TUser>, IModificationAuditedObject<TUser>
{
}
}

View File

@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Data.Auditing
{
public interface ICreationAuditedObject : IHasCreationTime, IMayHaveCreator
{
}
public interface ICreationAuditedObject<TCreator> : ICreationAuditedObject, IMayHaveCreator<TCreator>
{
}
}

View File

@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Data.Auditing
{
public interface IDeletionAuditedObject : IHasDeletionTime
{
long? DeleterId { get; }
}
public interface IDeletionAuditedObject<TUser> : IDeletionAuditedObject
{
TUser Deleter { get; }
}
}

View File

@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Data.Auditing
{
public interface IFullAuditedObject : IAuditedObject, IDeletionAuditedObject
{
}
public interface IFullAuditedObject<TUser> : IAuditedObject<TUser>, IFullAuditedObject, IDeletionAuditedObject<TUser>
{
}
}

View File

@@ -0,0 +1,10 @@
using System;
namespace Yi.Framework.Infrastructure.Data.Auditing;
public interface IHasCreationTime
{
DateTime CreationTime { get; set; }
}

View File

@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Infrastructure.Data.Entities;
namespace Yi.Framework.Infrastructure.Data.Auditing
{
public interface IHasDeletionTime : ISoftDelete
{
DateTime? DeletionTime { get; }
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Data.Auditing
{
public interface IHasEntityVersion
{
int EntityVersion { get; }
}
}

View File

@@ -0,0 +1,8 @@
namespace Yi.Framework.Infrastructure.Data.Auditing;
public interface IHasModificationTime
{
DateTime? LastModificationTime { get; }
}

View File

@@ -0,0 +1,13 @@
using System;
namespace Yi.Framework.Infrastructure.Data.Auditing;
public interface IMayHaveCreator<TCreator>
{
TCreator Creator { get; }
}
public interface IMayHaveCreator
{
long? CreatorId { get; }
}

View File

@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Data.Auditing
{
public interface IModificationAuditedObject : IHasModificationTime
{
long? LastModifierId { get; }
}
public interface IModificationAuditedObject<TUser> : IModificationAuditedObject
{
TUser LastModifier { get; }
}
}

View File

@@ -0,0 +1,13 @@
using System;
namespace Yi.Framework.Infrastructure.Data.Auditing;
public interface IMustHaveCreator<TCreator> : IMustHaveCreator
{
TCreator Creator { get; }
}
public interface IMustHaveCreator
{
long CreatorId { get; }
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Data.Entities
{
public interface IMultiTenant
{
public Guid TenantId { get; set; }
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Data.Entities
{
public interface IOrderNum
{
int OrderNum { get; set; }
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Data.Entities
{
public interface ISoftDelete
{
public bool IsDeleted { get; set; }
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Data.Entities
{
public interface IState
{
public bool State { get; set; }
}
}

View File

@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Ddd.Dtos.Abstract
{
public interface IEntityDto
{
}
public interface IEntityDto<TKey> : IEntityDto
{
TKey Id { get; set; }
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Ddd.Dtos.Abstract
{
public interface IHasTotalCount
{
long Total { get; set; }
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Ddd.Dtos.Abstract
{
public interface IListResult<T>
{
IReadOnlyList<T> Items { get; set; }
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Infrastructure.Ddd.Services.Abstract;
namespace Yi.Framework.Infrastructure.Ddd.Dtos.Abstract
{
public interface IPagedAllResultRequestDto : IPageTimeResultRequestDto, IPagedAndSortedResultRequestDto
{
}
}

View File

@@ -0,0 +1,15 @@

using Yi.Framework.Infrastructure.Enums;
namespace Yi.Framework.Infrastructure.Ddd.Dtos.Abstract
{
public interface IPagedAndSortedResultRequestDto
{
int PageNum { get; set; }
int PageSize { get; set; }
string? SortBy { get; set; }
OrderByEnum SortType { get; set; }
}
}

View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Ddd.Dtos.Abstract
{
public interface IPagedResult<T> : IListResult<T>, IHasTotalCount
{
}
}

View File

@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Infrastructure.Ddd.Dtos.Abstract;
namespace Yi.Framework.Infrastructure.Ddd.Dtos
{
[Serializable]
public abstract class EntityDto<TKey> : EntityDto, IEntityDto<TKey>, IEntityDto
{
//
// 摘要:
// Id of the entity.
public TKey Id { get; set; }
public override string ToString()
{
return $"[DTO: {GetType().Name}] Id = {Id}";
}
}
[Serializable]
public abstract class EntityDto : IEntityDto
{
public override string ToString()
{
return "[DTO: " + GetType().Name + "]";
}
}
}

View File

@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Infrastructure.Ddd.Dtos.Abstract;
namespace Yi.Framework.Infrastructure.Ddd.Dtos
{
[Serializable]
public class ListResultDto<T> : IListResult<T>
{
public IReadOnlyList<T> Items
{
get { return _items ?? (_items = new List<T>()); }
set { _items = value; }
}
private IReadOnlyList<T> _items;
public ListResultDto()
{
}
public ListResultDto(IReadOnlyList<T> items)
{
Items = items;
}
}
}

View File

@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Infrastructure.Ddd.Dtos.Abstract;
using Yi.Framework.Infrastructure.Ddd.Services.Abstract;
namespace Yi.Framework.Infrastructure.Ddd.Dtos
{
public class PagedAllResultRequestDto : PagedAndSortedResultRequestDto, IPagedAllResultRequestDto, IPagedAndSortedResultRequestDto, IPageTimeResultRequestDto
{
/// <summary>
/// 查询开始时间条件
/// </summary>
public DateTime? StartTime { get; set; }
/// <summary>
/// 查询结束时间条件
/// </summary>
public DateTime? EndTime { get; set; }
}
}

View File

@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Infrastructure.Ddd.Dtos.Abstract;
using Yi.Framework.Infrastructure.Enums;
namespace Yi.Framework.Infrastructure.Ddd.Dtos
{
public class PagedAndSortedResultRequestDto : IPagedAndSortedResultRequestDto
{
/// <summary>
/// 查询当前页条件
/// </summary>
public int PageNum { get; set; } = 1;
/// <summary>
/// 查询分页大小条件
/// </summary>
public int PageSize { get; set; } = int.MaxValue;
/// <summary>
/// 查询排序字段条件
/// </summary>
public string? SortBy { get; set; }
/// <summary>
/// 查询排序类别条件
/// </summary>
public OrderByEnum SortType { get; set; } = OrderByEnum.Desc;
}
}

View File

@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Ddd.Dtos
{
public class PagedDto<T>
{
public PagedDto(long totalCount, List<T> items)
{
Total = totalCount;
Items = items;
}
public long Total { get; set; }
public List<T> Items { get; set; }
}
}

View File

@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Infrastructure.Ddd.Dtos.Abstract;
namespace Yi.Framework.Infrastructure.Ddd.Dtos
{
public class PagedResultDto<T> : ListResultDto<T>, IPagedResult<T>
{
public long Total { get; set; }
public PagedResultDto()
{
}
public PagedResultDto(long totalCount, IReadOnlyList<T> items)
: base(items)
{
Total = totalCount;
}
}
}

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Ddd.Entities
{
public class AggregateRoot : IEntity, IAggregateRoot
{
}
public class AggregateRoot<TKey> : Entity<TKey>, IEntity<TKey>
{
}
}

View File

@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Ddd.Entities
{
[Serializable]
public abstract class Entity : IEntity
{
protected Entity()
{
}
public override string ToString()
{
return "[ENTITY: " + GetType().Name + "] Keys = " + GetKeys();
}
public abstract object[] GetKeys();
//实体比较简化
//public bool EntityEquals(IEntity other)
//{
// return this.GetKeys().Equals(other.GetKeys());
//}
}
[Serializable]
public abstract class Entity<TKey> : Entity, IEntity<TKey>, IEntity
{
public virtual TKey Id { get; set; }
protected Entity()
{
}
protected Entity(TKey id)
{
Id = id;
}
public override object[] GetKeys()
{
return new object[1] { Id };
}
public override string ToString()
{
return $"[ENTITY: {GetType().Name}] Id = {Id}";
}
}
}

View File

@@ -0,0 +1,16 @@
using Microsoft.AspNetCore.DataProtection.KeyManagement;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Ddd.Entities
{
public interface IAggregateRoot : IEntity
{
}
public interface IAggregateRoot<TKey> : IEntity<TKey>
{
}
}

View File

@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Ddd.Entities
{
public interface IEntity
{
//
// 摘要:
// Returns an array of ordered keys for this entity.
}
public interface IEntity<TKey> : IEntity
{
//
// 摘要:
// Unique identifier for this entity.
TKey Id { get; set; }
}
}

View File

@@ -0,0 +1,57 @@
using System.Linq.Expressions;
using SqlSugar;
using Yi.Framework.Infrastructure.Ddd.Dtos.Abstract;
using Yi.Framework.Infrastructure.Enums;
namespace Yi.Framework.Infrastructure.Ddd.Repositories
{
public interface IRepository<T>
{
/// <summary>
/// 注释一下严格意义这里应该protected但是我认为 简易程度 与 耦合程度 中是需要进行衡量的
/// </summary>
ISugarQueryable<T> _DbQueryable { get; }
//单查
Task<T> GetByIdAsync(dynamic id);
Task<T> GetSingleAsync(Expression<Func<T, bool>> whereExpression);
Task<T> GetFirstAsync(Expression<Func<T, bool>> whereExpression);
Task<bool> IsAnyAsync(Expression<Func<T, bool>> whereExpression);
Task<int> CountAsync(Expression<Func<T, bool>> whereExpression);
//多查
Task<List<T>> GetListAsync();
Task<List<T>> GetListAsync(Expression<Func<T, bool>> whereExpression);
//分页查
Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, int pageNum, int pageSize);
Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, int pageNum, int pageSize, Expression<Func<T, object>>? orderByExpression = null, OrderByEnum orderByType = OrderByEnum.Asc);
Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, int pageNum, int pageSize, string? orderBy, OrderByEnum orderByType = OrderByEnum.Asc);
Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, IPagedAndSortedResultRequestDto page);
Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, IPagedAndSortedResultRequestDto page, Expression<Func<T, object>>? orderByExpression = null, OrderByEnum orderByType = OrderByEnum.Asc);
Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, IPagedAndSortedResultRequestDto page, string? orderBy, OrderByEnum orderByType = OrderByEnum.Asc);
//插入
Task<bool> InsertAsync(T insertObj);
Task<bool> InsertOrUpdateAsync(T data);
Task<bool> InsertOrUpdateAsync(List<T> datas);
Task<int> InsertReturnIdentityAsync(T insertObj);
Task<long> InsertReturnBigIdentityAsync(T insertObj);
Task<long> InsertReturnSnowflakeIdAsync(T insertObj);
Task<T> InsertReturnEntityAsync(T insertObj);
Task<bool> InsertRangeAsync(List<T> insertObjs);
//更新
Task<bool> UpdateAsync(T updateObj);
Task<bool> UpdateRangeAsync(List<T> updateObjs);
Task<bool> UpdateAsync(Expression<Func<T, T>> columns, Expression<Func<T, bool>> whereExpression);
Task<bool> UpdateIgnoreNullAsync(T updateObj);
//删除
Task<bool> DeleteAsync(T deleteObj);
Task<bool> DeleteAsync(List<T> deleteObjs);
Task<bool> DeleteAsync(Expression<Func<T, bool>> whereExpression);
Task<bool> DeleteByIdAsync(dynamic id);
Task<bool> DeleteByIdsAsync(dynamic[] ids);
}
}

View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Ddd.Services.Abstract
{
public interface IApplicationService
{
}
}

View File

@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Ddd.Services.Abstract
{
public interface ICreateAppService<TEntityDto>
: ICreateAppService<TEntityDto, TEntityDto>
{
}
public interface ICreateAppService<TGetOutputDto, in TCreateInput>
: IApplicationService
{
Task<TGetOutputDto> CreateAsync(TCreateInput input);
}
}

View File

@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Ddd.Services.Abstract
{
public interface ICreateUpdateAppService<TEntityDto, in TKey>
: ICreateUpdateAppService<TEntityDto, TKey, TEntityDto, TEntityDto>
{
}
public interface ICreateUpdateAppService<TEntityDto, in TKey, in TCreateUpdateInput>
: ICreateUpdateAppService<TEntityDto, TKey, TCreateUpdateInput, TCreateUpdateInput>
{
}
public interface ICreateUpdateAppService<TGetOutputDto, in TKey, in TCreateUpdateInput, in TUpdateInput>
: ICreateAppService<TGetOutputDto, TCreateUpdateInput>,
IUpdateAppService<TGetOutputDto, TKey, TUpdateInput>
{
}
}

View File

@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Infrastructure.Ddd.Dtos;
namespace Yi.Framework.Infrastructure.Ddd.Services.Abstract
{
public interface ICrudAppService<TEntityDto, in TKey>
: ICrudAppService<TEntityDto, TKey, PagedAndSortedResultRequestDto>
{
}
public interface ICrudAppService<TEntityDto, in TKey, in TGetListInput>
: ICrudAppService<TEntityDto, TKey, TGetListInput, TEntityDto>
{
}
public interface ICrudAppService<TEntityDto, in TKey, in TGetListInput, in TCreateInput>
: ICrudAppService<TEntityDto, TKey, TGetListInput, TCreateInput, TCreateInput>
{
}
public interface ICrudAppService<TEntityDto, in TKey, in TGetListInput, in TCreateInput, in TUpdateInput>
: ICrudAppService<TEntityDto, TEntityDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
{
}
public interface ICrudAppService<TGetOutputDto, TGetListOutputDto, in TKey, in TGetListInput, in TCreateInput, in TUpdateInput>
: IReadOnlyAppService<TGetOutputDto, TGetListOutputDto, TKey, TGetListInput>,
ICreateUpdateAppService<TGetOutputDto, TKey, TCreateInput, TUpdateInput>,
IDeleteAppService<TKey>
{
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Ddd.Services.Abstract
{
public interface IDeleteAppService<in TKey> : IApplicationService
{
Task<bool> DeleteAsync(string id);
}
}

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Infrastructure.Ddd.Dtos.Abstract;
namespace Yi.Framework.Infrastructure.Ddd.Services.Abstract
{
public interface IPageTimeResultRequestDto : IPagedAndSortedResultRequestDto
{
DateTime? StartTime { get; set; }
DateTime? EndTime { get; set; }
}
}

View File

@@ -0,0 +1,24 @@
using Yi.Framework.Infrastructure.Ddd.Dtos;
namespace Yi.Framework.Infrastructure.Ddd.Services.Abstract
{
public interface IReadOnlyAppService<TEntityDto, in TKey>
: IReadOnlyAppService<TEntityDto, TEntityDto, TKey, PagedAndSortedResultRequestDto>
{
}
public interface IReadOnlyAppService<TEntityDto, in TKey, in TGetListInput>
: IReadOnlyAppService<TEntityDto, TEntityDto, TKey, TGetListInput>
{
}
public interface IReadOnlyAppService<TGetOutputDto, TGetListOutputDto, in TKey, in TGetListInput>
: IApplicationService
{
Task<TGetOutputDto> GetAsync(TKey id);
Task<PagedResultDto<TGetListOutputDto>> GetListAsync(TGetListInput input);
}
}

View File

@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Ddd.Services.Abstract
{
public interface IUpdateAppService<TEntityDto, in TKey>
: IUpdateAppService<TEntityDto, TKey, TEntityDto>
{
}
public interface IUpdateAppService<TGetOutputDto, in TKey, in TUpdateInput>
: IApplicationService
{
Task<TGetOutputDto> UpdateAsync(TKey id, TUpdateInput input);
}
}

View File

@@ -0,0 +1,11 @@
using Furion;
using MapsterMapper;
using Microsoft.Extensions.DependencyInjection;
namespace Yi.Framework.Infrastructure.Ddd.Services
{
public abstract class ApplicationService
{
public IMapper _mapper { get => App.GetRequiredService<IMapper>(); }
}
}

View File

@@ -0,0 +1,176 @@
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Infrastructure.Ddd.Dtos;
using Yi.Framework.Infrastructure.Ddd.Dtos.Abstract;
using Yi.Framework.Infrastructure.Ddd.Entities;
using Yi.Framework.Infrastructure.Ddd.Services.Abstract;
using Yi.Framework.Infrastructure.Helper;
namespace Yi.Framework.Infrastructure.Ddd.Services
{
public abstract class CrudAppService<TEntity, TEntityDto, TKey>
: CrudAppService<TEntity, TEntityDto, TKey, PagedAndSortedResultRequestDto>
where TEntity : class, IEntity<TKey>
where TEntityDto : IEntityDto<TKey>
{
}
public abstract class CrudAppService<TEntity, TEntityDto, TKey, TGetListInput>
: CrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TEntityDto>
where TEntity : class, IEntity<TKey>
where TEntityDto : IEntityDto<TKey>
{
}
public abstract class CrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TCreateInput>
: CrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TCreateInput, TCreateInput>
where TEntity : class, IEntity<TKey>
where TEntityDto : IEntityDto<TKey>
{
}
public abstract class CrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
: CrudAppService<TEntity, TEntityDto, TEntityDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
where TEntity : class, IEntity<TKey>
where TEntityDto : IEntityDto<TKey>
{
protected override Task<TEntityDto> MapToGetListOutputDtoAsync(TEntity entity)
{
return MapToGetOutputDtoAsync(entity);
}
}
public abstract class CrudAppService<TEntity, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
: ReadOnlyAppService<TEntity, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput>,
ICrudAppService<TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
where TEntity : class, IEntity<TKey>
where TGetOutputDto : IEntityDto<TKey>
where TGetListOutputDto : IEntityDto<TKey>
{
protected virtual Task<TEntity> MapToEntityAsync(TGetListInput getListinput)
{
return Task.FromResult(_mapper.Map<TEntity>(getListinput));
}
protected virtual Task<TEntity> MapToEntityAsync(TCreateInput createInput)
{
var entity = _mapper.Map<TEntity>(createInput);
//这里判断实体的T给id赋值
//雪花id
if (entity is IEntity<long> entityForlongId)
{
if (entityForlongId.Id is default(long))
{
//使用反射暂时先使用sqlsuga的雪花id提供
//ps: linshi
ReflexHelper.SetModelValue(nameof(IEntity<long>.Id), SnowflakeHelper.NextId, entity);
}
}
if (entity is IEntity<Guid> entityForGuidId)
{
if (entityForGuidId.Id == Guid.Empty)
{
ReflexHelper.SetModelValue(nameof(IEntity<long>.Id), new Guid(), entity);
}
}
return Task.FromResult(entity);
}
protected virtual Task MapToEntityAsync(TUpdateInput updateInput, TEntity entity)
{
_mapper.Map(updateInput, entity);
return Task.CompletedTask;
}
protected virtual Task<TEntity> MapToEntityAsync(TUpdateInput updateInput)
{
var entity = _mapper.Map<TEntity>(updateInput);
return Task.FromResult(entity);
}
/// <summary>
/// 增
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public virtual async Task<TGetOutputDto> CreateAsync(TCreateInput input)
{
var entity = await MapToEntityAsync(input);
//这里还可以设置租户
await _repository.InsertAsync(entity);
return await MapToGetOutputDtoAsync(entity);
}
/// <summary>
/// 单、多删
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public virtual async Task<bool> DeleteAsync(string id)
{
if (id is null)
{
throw new ArgumentNullException(nameof(id));
}
var idsValue = id.Split(',');
if (idsValue is null || idsValue.Length == 0)
{
throw new ArgumentNullException(nameof(id));
}
return await _repository.DeleteByIdsAsync(idsValue.Select(x => (object)x!).ToArray());
}
///// <summary>
///// 删
///// </summary>
///// <param name="id"></param>
///// <returns></returns>
///// <exception cref="ArgumentNullException"></exception>
//public async Task<bool> DeleteAsync(TKey id)
//{
// if (id is null)
// {
// throw new ArgumentNullException(nameof(id));
// }
// return await _repository.DeleteByIdAsync(id);
//}
/// <summary>
/// 改
/// </summary>
/// <param name="id"></param>
/// <param name="input"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public virtual async Task<TGetOutputDto> UpdateAsync(TKey id, TUpdateInput input)
{
if (id is null)
{
throw new ArgumentNullException(nameof(id));
}
var entity = await _repository.GetByIdAsync(id);
await MapToEntityAsync(input, entity);
await _repository.UpdateAsync(entity);
return await MapToGetOutputDtoAsync(entity);
}
}
}

View File

@@ -0,0 +1,128 @@
using Furion;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using Yi.Framework.Infrastructure.Ddd.Dtos;
using Yi.Framework.Infrastructure.Ddd.Dtos.Abstract;
using Yi.Framework.Infrastructure.Ddd.Entities;
using Yi.Framework.Infrastructure.Ddd.Repositories;
using Yi.Framework.Infrastructure.Ddd.Services.Abstract;
namespace Yi.Framework.Infrastructure.Ddd.Services
{
public abstract class ReadOnlyAppService<TEntity, TEntityDto, TKey>
: ReadOnlyAppService<TEntity, TEntityDto, TEntityDto, TKey, PagedAndSortedResultRequestDto>
where TEntity : class, IEntity<TKey>
where TEntityDto : IEntityDto<TKey>
{
}
public abstract class ReadOnlyAppService<TEntity, TEntityDto, TKey, TGetListInput>
: ReadOnlyAppService<TEntity, TEntityDto, TEntityDto, TKey, TGetListInput>
where TEntity : class, IEntity<TKey>
where TEntityDto : IEntityDto<TKey>
{
}
public abstract class ReadOnlyAppService<TEntity, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput> : ApplicationService,
IReadOnlyAppService<TGetOutputDto, TGetListOutputDto, TKey, TGetListInput>
where TEntity : class, IEntity
{
/// <summary>
/// 先暂时用服务定位的方式,之后将更改为属性注入
/// </summary>
protected IRepository<TEntity> _repository { get => App.GetRequiredService<IRepository<TEntity>>(); }
protected ISugarQueryable<TEntity> _DbQueryable => _repository._DbQueryable;
//Mapper
protected virtual Task<TGetOutputDto> MapToGetOutputDtoAsync(TEntity entity)
{
return Task.FromResult(_mapper.Map<TEntity, TGetOutputDto>(entity));
}
protected virtual Task<List<TGetListOutputDto>> MapToGetListOutputDtosAsync(List<TEntity> entities)
{
var dtos = _mapper.Map<List<TGetListOutputDto>>(entities);
return Task.FromResult(dtos);
}
protected virtual Task<TGetListOutputDto> MapToGetListOutputDtoAsync(TEntity entity)
{
var dto = _mapper.Map<TEntity, TGetListOutputDto>(entity);
return Task.FromResult(dto);
}
/// <summary>
/// 单查
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public virtual async Task<TGetOutputDto> GetAsync(TKey id)
{
if (id is null)
{
throw new ArgumentNullException(nameof(id));
}
var entity = await _repository.GetByIdAsync(id);
return await MapToGetOutputDtoAsync(entity);
}
/// <summary>
/// 多查
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public virtual async Task<PagedResultDto<TGetListOutputDto>> GetListAsync(TGetListInput input)
{
var totalCount = -1;
var entities = new List<TEntity>();
var entityDtos = new List<TGetListOutputDto>();
bool isPageList = true;
//if (totalCount > 0)
//{
//这里还可以追加如果是审计日志,继续拼接条件即可
if (input is IPageTimeResultRequestDto timeInput)
{
if (timeInput.StartTime is not null)
{
timeInput.EndTime = timeInput.EndTime ?? DateTime.Now;
}
}
if (input is IPagedAndSortedResultRequestDto sortInput)
{
entities = await _repository.GetPageListAsync(_ => true, sortInput, sortInput.SortBy, sortInput.SortType);
}
else
{
isPageList = false;
entities = await _repository.GetListAsync();
}
entityDtos = await MapToGetListOutputDtosAsync(entities);
//}
//如果是分页查询,还需要统计数量
if (isPageList)
{
totalCount = await _repository.CountAsync(_ => true);
}
return new PagedResultDto<TGetListOutputDto>(
totalCount,
entityDtos
);
}
}
}

View File

@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Enums
{
/// <summary>
/// 定义公共文件路径
/// </summary>
public enum FileTypeEnum
{
File,
Image,
Thumbnail,
Excel,
Temp
}
}

View File

@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Enums
{
public enum OrderByEnum
{
Asc,
Desc
}
}

View File

@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Enums
{
public enum QueryOperatorEnum
{
/// <summary>
/// 相等
/// </summary>
Equal,
/// <summary>
/// 匹配
/// </summary>
Like,
/// <summary>
/// 大于
/// </summary>
GreaterThan,
/// <summary>
/// 大于或等于
/// </summary>
GreaterThanOrEqual,
/// <summary>
/// 小于
/// </summary>
LessThan,
/// <summary>
/// 小于或等于
/// </summary>
LessThanOrEqual,
/// <summary>
/// 等于集合
/// </summary>
In,
/// <summary>
/// 不等于集合
/// </summary>
NotIn,
/// <summary>
/// 左边匹配
/// </summary>
LikeLeft,
/// <summary>
/// 右边匹配
/// </summary>
LikeRight,
/// <summary>
/// 不相等
/// </summary>
NoEqual,
/// <summary>
/// 为空或空
/// </summary>
IsNullOrEmpty,
/// <summary>
/// 不为空
/// </summary>
IsNot,
/// <summary>
/// 不匹配
/// </summary>
NoLike,
/// <summary>
/// 时间段 值用 "|" 隔开
/// </summary>
DateRange
}
}

View File

@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Enums
{
public enum ResultCodeEnum
{
/// <summary>
/// 操作成功。
/// </summary>
Success = 200,
/// <summary>
/// 操作不成功
/// </summary>
NotSuccess = 500,
/// <summary>
/// 无权限
/// </summary>
NoPermission = 401,
/// <summary>
/// 被拒绝
/// </summary>
Denied = 403
}
}

View File

@@ -0,0 +1,48 @@
using System.Runtime.Serialization;
using Microsoft.Extensions.Logging;
using Yi.Framework.Infrastructure.Enums;
namespace Yi.Framework.Infrastructure.Exceptions
{
public class AuthException : Exception,
IHasErrorCode,
IHasErrorDetails,
IHasLogLevel
{
public int Code { get; set; }
public string? Details { get; set; }
public LogLevel LogLevel { get; set; }
public AuthException(
string? message = null,
ResultCodeEnum code = ResultCodeEnum.NoPermission,
string? details = null,
Exception? innerException = null,
LogLevel logLevel = LogLevel.Warning)
: base(message, innerException)
{
Code = (int)code;
Details = details;
LogLevel = logLevel;
}
/// <summary>
/// 序列化参数的构造函数
/// </summary>
public AuthException(SerializationInfo serializationInfo, StreamingContext context)
: base(serializationInfo, context)
{
}
public AuthException WithData(string name, object value)
{
Data[name] = value;
return this;
}
}
}

View File

@@ -0,0 +1,47 @@
using System.Runtime.Serialization;
using Microsoft.Extensions.Logging;
using Yi.Framework.Infrastructure.Enums;
namespace Yi.Framework.Infrastructure.Exceptions
{
public class BusinessException : Exception,
IHasErrorCode,
IHasErrorDetails,
IHasLogLevel
{
public int Code { get; set; }
public string? Details { get; set; }
public LogLevel LogLevel { get; set; }
public BusinessException(
int code = (int)ResultCodeEnum.Denied,
string? message = null,
string? details = null,
Exception? innerException = null,
LogLevel logLevel = LogLevel.Warning)
: base(message, innerException)
{
Code = code;
Details = details;
LogLevel = logLevel;
}
/// <summary>
/// 序列化参数的构造函数
/// </summary>
public BusinessException(SerializationInfo serializationInfo, StreamingContext context)
: base(serializationInfo, context)
{
}
public BusinessException WithData(string name, object value)
{
Data[name] = value;
return this;
}
}
}

View File

@@ -0,0 +1,41 @@
using System.Runtime.ExceptionServices;
using Microsoft.Extensions.Logging;
using Yi.Framework.Infrastructure.Enums;
namespace Yi.Framework.Infrastructure.Exceptions;
/// <summary>
/// <see cref="Exception"/><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>չ<EFBFBD><D5B9><EFBFBD><EFBFBD>
/// </summary>
public static class ExceptionExtensions
{
/// <summary>
/// ʹ<><CAB9> <see cref="ExceptionDispatchInfo.Capture"/> <20>ٴ<EFBFBD><D9B4>׳<EFBFBD><D7B3>
/// </summary>
/// <param name="exception">Exception to be re-thrown</param>
public static void ReThrow(this Exception exception)
{
ExceptionDispatchInfo.Capture(exception).Throw();
}
/// <summary>
/// <20><>ȡ<EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD>־<EFBFBD>ȼ<EFBFBD>
/// </summary>
/// <param name="exception"></param>
/// <param name="defaultLevel"></param>
/// <returns></returns>
public static LogLevel GetLogLevel(this Exception exception, LogLevel defaultLevel = LogLevel.Error)
{
return (exception as IHasLogLevel)?.LogLevel ?? defaultLevel;
}
/// <summary>
/// <20><>ȡ<EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD>־<EFBFBD><D6BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
/// <param name="exception"></param>
/// <param name="defaultLevel"></param>
/// <returns></returns>
public static int GetLogErrorCode(this Exception exception, ResultCodeEnum defaultCode = ResultCodeEnum.NotSuccess)
{
return (exception as IHasErrorCode)?.Code ?? (int)defaultCode;
}
}

View File

@@ -0,0 +1,7 @@
namespace Yi.Framework.Infrastructure.Exceptions
{
internal interface IHasErrorCode
{
int Code { get; }
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Exceptions
{
public interface IHasErrorDetails
{
string? Details { get; }
}
}

View File

@@ -0,0 +1,14 @@
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Exceptions
{
public interface IHasLogLevel
{
LogLevel LogLevel { get; set; }
}
}

View File

@@ -0,0 +1,34 @@
using System.Runtime.Serialization;
using Microsoft.Extensions.Logging;
using Yi.Framework.Infrastructure.Enums;
namespace Yi.Framework.Infrastructure.Exceptions
{
public class UserFriendlyException : BusinessException
{
public UserFriendlyException(
string message,
int code = (int)ResultCodeEnum.Denied,
string? details = null,
Exception? innerException = null,
LogLevel logLevel = LogLevel.Warning)
: base(
code,
message,
details,
innerException,
logLevel)
{
Details = details;
}
/// <summary>
/// 序列化参数的构造函数
/// </summary>
public UserFriendlyException(SerializationInfo serializationInfo, StreamingContext context)
: base(serializationInfo, context)
{
}
}
}

View File

@@ -0,0 +1,94 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Helper
{
public static class AssemblyHelper
{
/// <summary>
/// 此处统一获取程序集,排除微软内部相关
/// </summary>
/// <returns></returns>
public static Assembly[] GetAllLoadAssembly()
{
return AppDomain.CurrentDomain.GetAssemblies();
}
public static List<Assembly> GetReferanceAssemblies(this AppDomain domain)
{
var list = new List<Assembly>();
domain.GetAssemblies().ToList().ForEach(i =>
{
GetReferanceAssemblies(i, list);
});
return list;
}
private static void GetReferanceAssemblies(Assembly assembly, List<Assembly> list)
{
assembly.GetReferencedAssemblies().ToList().ForEach(i =>
{
var ass = Assembly.Load(i);
if (!list.Contains(ass))
{
list.Add(ass);
GetReferanceAssemblies(ass, list);
}
});
}
public static List<Type> GetClass(string assemblyFile, string? className = null, string? spaceName = null)
{
Assembly assembly = Assembly.Load(assemblyFile);
return assembly.GetTypes().Where(m => m.IsClass
&& className == null ? true : m.Name == className
&& spaceName == null ? true : m.Namespace == spaceName
&& !m.Name.StartsWith("<>")
).ToList();
}
public static List<Type> GetClassByParentClass(string assemblyFile, Type type)
{
Assembly assembly = Assembly.Load(assemblyFile);
List<Type> resList = new List<Type>();
List<Type> typeList = assembly.GetTypes().Where(m => m.IsClass).ToList();
foreach (var t in typeList)
{
var data = t.BaseType;
if (data == type)
{
resList.Add(t);
}
}
return resList;
}
public static List<Type> GetClassByInterfaces(string assemblyFile, Type type)
{
Assembly assembly = Assembly.Load(assemblyFile);
List<Type> resList = new List<Type>();
List<Type> typeList = assembly.GetTypes().Where(m => m.IsClass).ToList();
foreach (var t in typeList)
{
var data = t.GetInterfaces();
if (data.Contains(type))
{
resList.Add(t);
}
}
return resList;
}
}
}

View File

@@ -0,0 +1,101 @@
using System;
using System.Text;
namespace Yi.Framework.Infrastructure.Helper
{
public sealed class Base32Helper
{
// the valid chars for the encoding
private static string ValidChars = "QAZ2WSX3" + "EDC4RFV5" + "TGB6YHN7" + "UJM8K9LP";
/// <summary>
/// Converts an array of bytes to a Base32-k string.
/// </summary>
public static string ToString(byte[] bytes)
{
StringBuilder sb = new StringBuilder(); // holds the base32 chars
byte index;
int hi = 5;
int currentByte = 0;
while (currentByte < bytes.Length)
{
// do we need to use the next byte?
if (hi > 8)
{
// get the last piece from the current byte, shift it to the right
// and increment the byte counter
index = (byte)(bytes[currentByte++] >> hi - 5);
if (currentByte != bytes.Length)
{
// if we are not at the end, get the first piece from
// the next byte, clear it and shift it to the left
index = (byte)((byte)(bytes[currentByte] << 16 - hi) >> 3 | index);
}
hi -= 3;
}
else if (hi == 8)
{
index = (byte)(bytes[currentByte++] >> 3);
hi -= 3;
}
else
{
// simply get the stuff from the current byte
index = (byte)((byte)(bytes[currentByte] << 8 - hi) >> 3);
hi += 5;
}
sb.Append(ValidChars[index]);
}
return sb.ToString();
}
/// <summary>
/// Converts a Base32-k string into an array of bytes.
/// </summary>
/// <exception cref="ArgumentException">
/// Input string <paramref name="s">s</paramref> contains invalid Base32-k characters.
/// </exception>
public static byte[] FromBase32String(string str)
{
int numBytes = str.Length * 5 / 8;
byte[] bytes = new byte[numBytes];
// all UPPERCASE chars
str = str.ToUpper();
int bit_buffer;
int currentCharIndex;
int bits_in_buffer;
if (str.Length < 3)
{
bytes[0] = (byte)(ValidChars.IndexOf(str[0]) | ValidChars.IndexOf(str[1]) << 5);
return bytes;
}
bit_buffer = ValidChars.IndexOf(str[0]) | ValidChars.IndexOf(str[1]) << 5;
bits_in_buffer = 10;
currentCharIndex = 2;
for (int i = 0; i < bytes.Length; i++)
{
bytes[i] = (byte)bit_buffer;
bit_buffer >>= 8;
bits_in_buffer -= 8;
while (bits_in_buffer < 8 && currentCharIndex < str.Length)
{
bit_buffer |= ValidChars.IndexOf(str[currentCharIndex++]) << bits_in_buffer;
bits_in_buffer += 5;
}
}
return bytes;
}
}
}

View File

@@ -0,0 +1,54 @@
using System;
namespace Yi.Framework.Infrastructure.Helper
{
public static class ConsoleHelper
{
public static void WriteColorLine(string str, ConsoleColor color)
{
ConsoleColor currentForeColor = Console.ForegroundColor;
Console.ForegroundColor = color;
Console.WriteLine(str);
Console.ForegroundColor = currentForeColor;
}
/// <summary>
/// 打印错误信息
/// </summary>
/// <param name="str">待打印的字符串</param>
/// <param name="color">想要打印的颜色</param>
public static void WriteErrorLine(this string str, ConsoleColor color = ConsoleColor.Red)
{
WriteColorLine(str, color);
}
/// <summary>
/// 打印警告信息
/// </summary>
/// <param name="str">待打印的字符串</param>
/// <param name="color">想要打印的颜色</param>
public static void WriteWarningLine(this string str, ConsoleColor color = ConsoleColor.Yellow)
{
WriteColorLine(str, color);
}
/// <summary>
/// 打印正常信息
/// </summary>
/// <param name="str">待打印的字符串</param>
/// <param name="color">想要打印的颜色</param>
public static void WriteInfoLine(this string str, ConsoleColor color = ConsoleColor.White)
{
WriteColorLine(str, color);
}
/// <summary>
/// 打印成功的信息
/// </summary>
/// <param name="str">待打印的字符串</param>
/// <param name="color">想要打印的颜色</param>
public static void WriteSuccessLine(this string str, ConsoleColor color = ConsoleColor.Green)
{
WriteColorLine(str, color);
}
}
}

View File

@@ -0,0 +1,58 @@
using System;
namespace Yi.Framework.Infrastructure.Helper
{
public class DateHelper
{
public static DateTime StampToDateTime(string time)
{
time = time.Substring(0, 10);
double timestamp = Convert.ToInt64(time);
DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
dateTime = dateTime.AddSeconds(timestamp).ToLocalTime();
return dateTime;
}
public static string TimeSubTract(DateTime time1, DateTime time2)
{
TimeSpan subTract = time1.Subtract(time2);
return $"{subTract.Days} 天 {subTract.Hours} 时 {subTract.Minutes} 分 ";
}
/// <summary>
/// 时间戳转本地时间-时间戳精确到秒
/// </summary>
public static DateTime ToLocalTimeDateBySeconds(long unix)
{
var dto = DateTimeOffset.FromUnixTimeSeconds(unix);
return dto.ToLocalTime().DateTime;
}
/// <summary>
/// 时间转时间戳Unix-时间戳精确到秒
/// </summary>
public static long ToUnixTimestampBySeconds(DateTime dt)
{
DateTimeOffset dto = new DateTimeOffset(dt);
return dto.ToUnixTimeSeconds();
}
/// <summary>
/// 时间戳转本地时间-时间戳精确到毫秒
/// </summary>
public static DateTime ToLocalTimeDateByMilliseconds(long unix)
{
var dto = DateTimeOffset.FromUnixTimeMilliseconds(unix);
return dto.ToLocalTime().DateTime;
}
/// <summary>
/// 时间转时间戳Unix-时间戳精确到毫秒
/// </summary>
public static long ToUnixTimestampByMilliseconds(DateTime dt)
{
DateTimeOffset dto = new DateTimeOffset(dt);
return dto.ToUnixTimeMilliseconds();
}
}
}

View File

@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Helper
{
public class Compare<T, C> : IEqualityComparer<T>
{
private Func<T, C> _getField;
public Compare(Func<T, C> getfield)
{
_getField = getfield;
}
public bool Equals(T? x, T? y)
{
return EqualityComparer<C>.Default.Equals(_getField(x!), _getField(y!));
}
public int GetHashCode(T obj)
{
return EqualityComparer<C>.Default.GetHashCode(_getField(obj)!);
}
}
public static class DistinctHelper
{
/// <summary>
/// 自定义Distinct扩展方法
/// </summary>
/// <typeparam name="T">要去重的对象类</typeparam>
/// <typeparam name="C">自定义去重的字段类型</typeparam>
/// <param name="source">要去重的对象</param>
/// <param name="getfield">获取自定义去重字段的委托</param>
/// <returns></returns>
public static IEnumerable<T> DistinctNew<T, C>(this IEnumerable<T> source, Func<T, C> getfield)
{
return source.Distinct(new Compare<T, C>(getfield));
}
}
}

View File

@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Helper
{
public static class EnumHelper
{
public static New EnumToEnum<New>(this object oldEnum)
{
if (oldEnum is null)
{
throw new ArgumentNullException(nameof(oldEnum));
}
return (New)Enum.ToObject(typeof(New), oldEnum.GetHashCode());
}
public static TEnum StringToEnum<TEnum>(this string str)
{
return (TEnum)Enum.Parse(typeof(TEnum), str);
}
}
}

View File

@@ -0,0 +1,98 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Helper
{
public static class ExpressionHelper
{
/// <summary>
/// Expression表达式树lambda参数拼接组合
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="first"></param>
/// <param name="second"></param>
/// <param name="merge"></param>
/// <returns></returns>
public static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge)
{
var parameterMap = first.Parameters.Select((f, i) => new { f, s = second.Parameters[i] }).ToDictionary(p => p.s, p => p.f);
var secondBody = LambdaParameteRebinder.ReplaceParameter(parameterMap, second.Body);
return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters);
}
/// <summary>
/// Expression表达式树lambda参数拼接--false
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static Expression<Func<T, bool>> False<T>() => f => false;
/// <summary>
/// Expression表达式树lambda参数拼接-true
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static Expression<Func<T, bool>> True<T>() => f => true;
/// <summary>
/// Expression表达式树lambda参数拼接--and
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="first"></param>
/// <param name="second"></param>
/// <returns></returns>
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second) => first.Compose(second, Expression.And);
/// <summary>
/// Expression表达式树lambda参数拼接--or
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="first"></param>
/// <param name="second"></param>
/// <returns></returns>
public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second) => first.Compose(second, Expression.Or);
}
public class LambdaParameteRebinder : ExpressionVisitor
{
/// <summary>
/// 存放表达式树的参数的字典
/// </summary>
private readonly Dictionary<ParameterExpression, ParameterExpression> map;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="map"></param>
public LambdaParameteRebinder(Dictionary<ParameterExpression, ParameterExpression> map)
{
this.map = map ?? new Dictionary<ParameterExpression, ParameterExpression>();
}
/// <summary>
/// 重载参数访问的方法,访问表达式树参数,如果字典中包含,则取出
/// </summary>
/// <param name="node">表达式树参数</param>
/// <returns></returns>
protected override Expression VisitParameter(ParameterExpression node)
{
if (map.TryGetValue(node, out ParameterExpression expression))
{
node = expression;
}
return base.VisitParameter(node);
}
public static Expression ReplaceParameter(Dictionary<ParameterExpression, ParameterExpression> map, Expression exp)
{
return new LambdaParameteRebinder(map).Visit(exp);
}
}
}

View File

@@ -0,0 +1,490 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace Yi.Framework.Infrastructure.Helper
{
public class FileHelper : IDisposable
{
private bool _alreadyDispose = false;
#region
public FileHelper()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
~FileHelper()
{
Dispose(); ;
}
protected virtual void Dispose(bool isDisposing)
{
if (_alreadyDispose) return;
_alreadyDispose = true;
}
#endregion
#region IDisposable
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
#region
/****************************************
* 函数名称GetPostfixStr
* 功能说明:取得文件后缀名
* 参 数filename:文件名称
* 调用示列:
* string filename = "aaa.aspx";
* string s = EC.FileObj.GetPostfixStr(filename);
*****************************************/
/// <summary>
/// 取后缀名
/// </summary>
/// <param name="filename">文件名</param>
/// <returns>.gif|.html格式</returns>
public static string GetPostfixStr(string filename)
{
int start = filename.LastIndexOf(".");
int length = filename.Length;
string postfix = filename.Substring(start, length - start);
return postfix;
}
#endregion
#region
/// <summary>
/// 根据文件大小获取指定前缀的可用文件名
/// </summary>
/// <param name="folderPath">文件夹</param>
/// <param name="prefix">文件前缀</param>
/// <param name="size">文件大小(1m)</param>
/// <param name="ext">文件后缀(.log)</param>
/// <returns>可用文件名</returns>
//public static string GetAvailableFileWithPrefixOrderSize(string folderPath, string prefix, int size = 1 * 1024 * 1024, string ext = ".log")
//{
// var allFiles = new DirectoryInfo(folderPath);
// var selectFiles = allFiles.GetFiles().Where(fi => fi.Name.ToLower().Contains(prefix.ToLower()) && fi.Extension.ToLower() == ext.ToLower() && fi.Length < size).OrderByDescending(d=>d.Name).ToList();
// if (selectFiles.Count > 0)
// {
// return selectFiles.FirstOrDefault().FullName;
// }
// return Path.Combine(folderPath, $@"{prefix}_{DateTime.Now.DateToTimeStamp()}.log");
//}
//public static string GetAvailableFileNameWithPrefixOrderSize(string _contentRoot, string prefix, int size = 1 * 1024 * 1024, string ext = ".log")
//{
// var folderPath = Path.Combine(_contentRoot, "Log");
// if (!Directory.Exists(folderPath))
// {
// Directory.CreateDirectory(folderPath);
// }
// var allFiles = new DirectoryInfo(folderPath);
// var selectFiles = allFiles.GetFiles().Where(fi => fi.Name.ToLower().Contains(prefix.ToLower()) && fi.Extension.ToLower() == ext.ToLower() && fi.Length < size).OrderByDescending(d => d.Name).ToList();
// if (selectFiles.Count > 0)
// {
// return selectFiles.FirstOrDefault().Name.Replace(".log","");
// }
// return $@"{prefix}_{DateTime.Now.DateToTimeStamp()}";
//}
#endregion
#region
/****************************************
* 函数名称WriteFile
* 功能说明:写文件,会覆盖掉以前的内容
* 参 数Path:文件路径,Strings:文本内容
* 调用示列:
* string Path = Server.MapPath("Default2.aspx");
* string Strings = "这是我写的内容啊";
* EC.FileObj.WriteFile(Path,Strings);
*****************************************/
/// <summary>
/// 写文件
/// </summary>
/// <param name="Path">文件路径</param>
/// <param name="Strings">文件内容</param>
public static void WriteFile(string Path, string Strings)
{
if (!File.Exists(Path))
{
FileStream f = File.Create(Path);
f.Close();
}
StreamWriter f2 = new StreamWriter(Path, false, Encoding.GetEncoding("gb2312"));
f2.Write(Strings);
f2.Close();
f2.Dispose();
}
/// <summary>
/// 写文件
/// </summary>
/// <param name="Path">文件路径</param>
/// <param name="Strings">文件内容</param>
/// <param name="encode">编码格式</param>
public static void WriteFile(string Path, string Strings, Encoding encode)
{
if (!File.Exists(Path))
{
FileStream f = File.Create(Path);
f.Close();
}
StreamWriter f2 = new StreamWriter(Path, false, encode);
f2.Write(Strings);
f2.Close();
f2.Dispose();
}
#endregion
#region
/****************************************
* 函数名称ReadFile
* 功能说明:读取文本内容
* 参 数Path:文件路径
* 调用示列:
* string Path = Server.MapPath("Default2.aspx");
* string s = EC.FileObj.ReadFile(Path);
*****************************************/
/// <summary>
/// 读文件
/// </summary>
/// <param name="Path">文件路径</param>
/// <returns></returns>
public static string ReadFile(string Path)
{
string s = "";
if (!File.Exists(Path))
s = "不存在相应的目录";
else
{
StreamReader f2 = new StreamReader(Path, Encoding.GetEncoding("gb2312"));
s = f2.ReadToEnd();
f2.Close();
f2.Dispose();
}
return s;
}
/// <summary>
/// 读文件
/// </summary>
/// <param name="Path">文件路径</param>
/// <param name="encode">编码格式</param>
/// <returns></returns>
public static string ReadFile(string Path, Encoding encode)
{
string s = "";
if (!File.Exists(Path))
s = "不存在相应的目录";
else
{
StreamReader f2 = new StreamReader(Path, encode);
s = f2.ReadToEnd();
f2.Close();
f2.Dispose();
}
return s;
}
#endregion
#region
/****************************************
* 函数名称FileAdd
* 功能说明:追加文件内容
* 参 数Path:文件路径,strings:内容
* 调用示列:
* string Path = Server.MapPath("Default2.aspx");
* string Strings = "新追加内容";
* EC.FileObj.FileAdd(Path, Strings);
*****************************************/
/// <summary>
/// 追加文件
/// </summary>
/// <param name="Path">文件路径</param>
/// <param name="strings">内容</param>
public static void FileAdd(string Path, string strings)
{
StreamWriter sw = File.AppendText(Path);
sw.Write(strings);
sw.Flush();
sw.Close();
}
#endregion
#region
/****************************************
* 函数名称FileCoppy
* 功能说明:拷贝文件
* 参 数OrignFile:原始文件,NewFile:新文件路径
* 调用示列:
* string orignFile = Server.MapPath("Default2.aspx");
* string NewFile = Server.MapPath("Default3.aspx");
* EC.FileObj.FileCoppy(OrignFile, NewFile);
*****************************************/
/// <summary>
/// 拷贝文件
/// </summary>
/// <param name="OrignFile">原始文件</param>
/// <param name="NewFile">新文件路径</param>
public static void FileCoppy(string orignFile, string NewFile)
{
File.Copy(orignFile, NewFile, true);
}
#endregion
#region
/****************************************
* 函数名称FileDel
* 功能说明:删除文件
* 参 数Path:文件路径
* 调用示列:
* string Path = Server.MapPath("Default3.aspx");
* EC.FileObj.FileDel(Path);
*****************************************/
/// <summary>
/// 删除文件
/// </summary>
/// <param name="Path">路径</param>
public static void FileDel(string Path)
{
File.Delete(Path);
}
#endregion
#region
/****************************************
* 函数名称FileMove
* 功能说明:移动文件
* 参 数OrignFile:原始路径,NewFile:新文件路径
* 调用示列:
* string orignFile = Server.MapPath("../说明.txt");
* string NewFile = Server.MapPath("http://www.cnblogs.com/说明.txt");
* EC.FileObj.FileMove(OrignFile, NewFile);
*****************************************/
/// <summary>
/// 移动文件
/// </summary>
/// <param name="OrignFile">原始路径</param>
/// <param name="NewFile">新路径</param>
public static void FileMove(string orignFile, string NewFile)
{
File.Move(orignFile, NewFile);
}
#endregion
#region
/****************************************
* 函数名称FolderCreate
* 功能说明:在当前目录下创建目录
* 参 数OrignFolder:当前目录,NewFloder:新目录
* 调用示列:
* string orignFolder = Server.MapPath("test/");
* string NewFloder = "new";
* EC.FileObj.FolderCreate(OrignFolder, NewFloder);
*****************************************/
/// <summary>
/// 在当前目录下创建目录
/// </summary>
/// <param name="OrignFolder">当前目录</param>
/// <param name="NewFloder">新目录</param>
public static void FolderCreate(string orignFolder, string NewFloder)
{
Directory.SetCurrentDirectory(orignFolder);
Directory.CreateDirectory(NewFloder);
}
#endregion
#region
/****************************************
* 函数名称DeleteFolder
* 功能说明:递归删除文件夹目录及文件
* 参 数dir:文件夹路径
* 调用示列:
* string dir = Server.MapPath("test/");
* EC.FileObj.DeleteFolder(dir);
*****************************************/
/// <summary>
/// 递归删除文件夹目录及文件
/// </summary>
/// <param name="dir"></param>
/// <returns></returns>
public static void DeleteFolder(string dir)
{
if (Directory.Exists(dir)) //如果存在这个文件夹删除之
{
foreach (string d in Directory.GetFileSystemEntries(dir))
{
if (File.Exists(d))
File.Delete(d); //直接删除其中的文件
else
DeleteFolder(d); //递归删除子文件夹
}
Directory.Delete(dir); //删除已空文件夹
}
}
#endregion
#region copy到目标文件夹下面
/****************************************
* 函数名称CopyDir
* 功能说明将指定文件夹下面的所有内容copy到目标文件夹下面 果目标文件夹为只读属性就会报错。
* 参 数srcPath:原始路径,aimPath:目标文件夹
* 调用示列:
* string srcPath = Server.MapPath("test/");
* string aimPath = Server.MapPath("test1/");
* EC.FileObj.CopyDir(srcPath,aimPath);
*****************************************/
/// <summary>
/// 指定文件夹下面的所有内容copy到目标文件夹下面
/// </summary>
/// <param name="srcPath">原始路径</param>
/// <param name="aimPath">目标文件夹</param>
public static void CopyDir(string srcPath, string aimPath)
{
try
{
// 检查目标目录是否以目录分割字符结束如果不是则添加之
if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)
aimPath += Path.DirectorySeparatorChar;
// 判断目标目录是否存在如果不存在则新建之
if (!Directory.Exists(aimPath))
Directory.CreateDirectory(aimPath);
// 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
//如果你指向copy目标文件下面的文件而不包含目录请使用下面的方法
//string[] fileList = Directory.GetFiles(srcPath);
string[] fileList = Directory.GetFileSystemEntries(srcPath);
//遍历所有的文件和目录
foreach (string file in fileList)
{
//先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
if (Directory.Exists(file))
CopyDir(file, aimPath + Path.GetFileName(file));
//否则直接Copy文件
else
File.Copy(file, aimPath + Path.GetFileName(file), true);
}
}
catch (Exception ee)
{
throw new Exception(ee.ToString());
}
}
#endregion
/// <summary>
/// 获取目录下全部文件名
/// </summary>
/// <param name="path"></param>
/// <param name="pattern"></param>
/// <returns></returns>
public static List<string> GetAllFileNames(string path, string pattern = "*")
{
List<FileInfo> folder = new DirectoryInfo(path).GetFiles(pattern).ToList();
return folder.Select(x => x.Name).ToList();
}
/// <summary>
/// 文件内容替换
/// </summary>
public static string FileContentReplace(string path, string oldStr, string newStr)
{
var content = File.ReadAllText(path);
if (content.Contains(oldStr))
{
File.Delete(path);
File.WriteAllText(path, content.Replace(oldStr, newStr));
}
return path;
}
/// <summary>
/// 文件名称
/// </summary>
public static string FileNameReplace(string path, string oldStr, string newStr)
{
string fileName = Path.GetFileName(path);
if (!fileName.Contains(oldStr))
{
return path;
}
string? directoryName = Path.GetDirectoryName(path);
string newFileName = fileName.Replace(oldStr, newStr);
string newPath = Path.Combine(directoryName ?? "", newFileName);
File.Move(path, newPath);
return newPath;
}
/// <summary>
/// 目录名替换
/// </summary>
public static string DirectoryNameReplace(string path, string oldStr, string newStr)
{
string fileName = Path.GetFileName(path);
if (!fileName.Contains(oldStr))
{
return path;
}
string? directoryName = Path.GetDirectoryName(path);
string newFileName = fileName.Replace(oldStr, newStr);
string newPath = Path.Combine(directoryName ?? "", newFileName);
Directory.Move(path, newPath);
return newPath;
}
/// <summary>
/// 全部信息递归替换
/// </summary>
/// <param name="dirPath"></param>
/// <param name="oldStr"></param>
/// <param name="newStr"></param>
public static void AllInfoReplace(string dirPath, string oldStr, string newStr)
{
var path = DirectoryNameReplace(dirPath, oldStr, newStr);
var dirInfo = new DirectoryInfo(path);
var files = dirInfo.GetFiles();
var dirs = dirInfo.GetDirectories();
if (files.Length > 0)
{
foreach (var f in files)
{
FileContentReplace(f.FullName, oldStr, newStr);
FileNameReplace(f.FullName, oldStr, newStr);
}
}
if (dirs.Length > 0)
{
foreach (var d in dirs)
{
AllInfoReplace(d.FullName, oldStr, newStr);
}
}
}
}
}

View File

@@ -0,0 +1,24 @@
namespace Yi.Framework.Infrastructure.Helper
{
public static class HtmlHelper
{
#region HTML标签
/// <summary>
/// 去除富文本中的HTML标签
/// </summary>
/// <param name="html"></param>
/// <param name="length"></param>
/// <returns></returns>
public static string ReplaceHtmlTag(string html, int length = 0)
{
string strText = System.Text.RegularExpressions.Regex.Replace(html, "<[^>]+>", "");
strText = System.Text.RegularExpressions.Regex.Replace(strText, "&[^;]+;", "");
if (length > 0 && strText.Length > length)
return strText.Substring(0, length);
return strText;
}
#endregion
}
}

View File

@@ -0,0 +1,122 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Mime;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Helper
{
public static class HttpHelper
{
public static HttpClient Client { get; set; } = new HttpClient();
public static async Task<string> Get(string url)
{
return await Client.GetStringAsync(url);
}
public static async Task<Stream> GetIO(string url)
{
return await Client.GetStreamAsync(url);
}
public static async Task<string> Post(string url, object? item = null, Dictionary<string, string>? head = null)
{
using StringContent json = new(JsonSerializer.Serialize(item), Encoding.UTF8, MediaTypeNames.Application.Json);
if (head is not null)
{
foreach (var d in head)
{
json.Headers.Add(d.Key, d.Value);
}
}
var httpResponse = await Client.PostAsync(url, json);
httpResponse.EnsureSuccessStatusCode();
var content = httpResponse.Content;
return await content.ReadAsStringAsync();
}
// public static string HttpGet(string Url, string postDataStr="")
// {
//#pragma warning disable SYSLIB0014 // 类型或成员已过时
// HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
//#pragma warning restore SYSLIB0014 // 类型或成员已过时
// 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();
//#pragma warning disable SYSLIB0014 // 类型或成员已过时
// HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
//#pragma warning restore SYSLIB0014 // 类型或成员已过时
// 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

@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Helper
{
public static class IdHelper
{
public static dynamic[] ToDynamicArray(this IEnumerable<long> ids)
{
return ids.Select(id => (dynamic)id).ToArray();
}
}
}

View File

@@ -0,0 +1,56 @@
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
namespace Yi.Framework.Infrastructure.Helper
{
public class IpHelper
{
/// <summary>
/// 获取当前IP地址
/// </summary>
/// <param name="preferredNetworks"></param>
/// <returns></returns>
public static string GetCurrentIp(string preferredNetworks)
{
var instanceIp = "127.0.0.1";
try
{
// 获取可用网卡
var nics = NetworkInterface.GetAllNetworkInterfaces()?.Where(network => network.OperationalStatus == OperationalStatus.Up);
// 获取所有可用网卡IP信息
var ipCollection = nics?.Select(x => x.GetIPProperties())?.SelectMany(x => x.UnicastAddresses);
if (ipCollection is null)
{
return instanceIp;
}
foreach (var ipadd in ipCollection)
{
if (!IPAddress.IsLoopback(ipadd.Address) && ipadd.Address.AddressFamily == AddressFamily.InterNetwork)
{
if (string.IsNullOrEmpty(preferredNetworks))
{
instanceIp = ipadd.Address.ToString();
break;
}
if (!ipadd.Address.ToString().StartsWith(preferredNetworks)) continue;
instanceIp = ipadd.Address.ToString();
break;
}
}
}
catch
{
// ignored
}
return instanceIp;
}
}
}

View File

@@ -0,0 +1,518 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Text.Json;
namespace Yi.Framework.Infrastructure.Helper
{
public class JsonHelper
{
public static string ObjToStr<T>(T obj, string dateTimeFormat)
{
IsoDateTimeConverter timeConverter = new IsoDateTimeConverter()
{
DateTimeFormat = dateTimeFormat
};
return JsonConvert.SerializeObject(obj, Formatting.Indented, timeConverter);
}
public static string ObjToStr<T>(T obj)
{
return JsonConvert.SerializeObject(obj);
}
public static T StrToObj<T>(string str)
{
return JsonConvert.DeserializeObject<T>(str)!;
}
/// <summary>
/// 转换对象为JSON格式数据
/// </summary>
/// <typeparam name="T">类</typeparam>
/// <param name="obj">对象</param>
/// <returns>字符格式的JSON数据</returns>
public static string GetJSON<T>(object obj)
{
string result = string.Empty;
try
{
System.Text.Json.JsonSerializer.Serialize("");
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer =
new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(T));
using (MemoryStream ms = new MemoryStream())
{
serializer.WriteObject(ms, obj);
result = System.Text.Encoding.UTF8.GetString(ms.ToArray());
}
}
catch (Exception)
{
throw;
}
return result;
}
/// <summary>
/// 转换List<T>的数据为JSON格式
/// </summary>
/// <typeparam name="T">类</typeparam>
/// <param name="vals">列表值</param>
/// <returns>JSON格式数据</returns>
public string JSON<T>(List<T> vals)
{
System.Text.StringBuilder st = new System.Text.StringBuilder();
try
{
System.Runtime.Serialization.Json.DataContractJsonSerializer s = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(T));
foreach (T city in vals)
{
using (MemoryStream ms = new MemoryStream())
{
s.WriteObject(ms, city);
st.Append(System.Text.Encoding.UTF8.GetString(ms.ToArray()));
}
}
}
catch (Exception)
{
}
return st.ToString();
}
/// <summary>
/// JSON格式字符转换为T类型的对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="jsonStr"></param>
/// <returns></returns>
public static T ParseFormByJson<T>(string jsonStr)
{
T obj = Activator.CreateInstance<T>();
using (MemoryStream ms =
new MemoryStream(System.Text.Encoding.UTF8.GetBytes(jsonStr)))
{
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer =
new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(T));
return (T)serializer.ReadObject(ms)!;
}
}
public string JSON1<SendData>(List<SendData> vals)
{
System.Text.StringBuilder st = new System.Text.StringBuilder();
try
{
System.Runtime.Serialization.Json.DataContractJsonSerializer s = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(SendData));
foreach (SendData city in vals)
{
using (MemoryStream ms = new MemoryStream())
{
s.WriteObject(ms, city);
st.Append(System.Text.Encoding.UTF8.GetString(ms.ToArray()));
}
}
}
catch (Exception)
{
}
return st.ToString();
}
private static bool IsJsonStart(ref string json)
{
if (!string.IsNullOrEmpty(json))
{
json = json.Trim('\r', '\n', ' ');
if (json.Length > 1)
{
char s = json[0];
char e = json[json.Length - 1];
return s == '{' && e == '}' || s == '[' && e == ']';
}
}
return false;
}
public static bool IsJson(string json)
{
int errIndex;
return IsJson(json, out errIndex);
}
public static bool IsJson(string json, out int errIndex)
{
errIndex = 0;
if (IsJsonStart(ref json))
{
CharState cs = new CharState();
char c;
for (int i = 0; i < json.Length; i++)
{
c = json[i];
if (SetCharState(c, ref cs) && cs.childrenStart)//设置关键符号状态。
{
string item = json.Substring(i);
int err;
int length = GetValueLength(item, true, out err);
cs.childrenStart = false;
if (err > 0)
{
errIndex = i + err;
return false;
}
i = i + length - 1;
}
if (cs.isError)
{
errIndex = i;
return false;
}
}
return !cs.arrayStart && !cs.jsonStart;
}
return false;
}
/// <summary>
/// 获取值的长度当Json值嵌套以"{"或"["开头时)
/// </summary>
private static int GetValueLength(string json, bool breakOnErr, out int errIndex)
{
errIndex = 0;
int len = 0;
if (!string.IsNullOrEmpty(json))
{
CharState cs = new CharState();
char c;
for (int i = 0; i < json.Length; i++)
{
c = json[i];
if (!SetCharState(c, ref cs))//设置关键符号状态。
{
if (!cs.jsonStart && !cs.arrayStart)//json结束又不是数组则退出。
{
break;
}
}
else if (cs.childrenStart)//正常字符,值状态下。
{
int length = GetValueLength(json.Substring(i), breakOnErr, out errIndex);//递归子值,返回一个长度。。。
cs.childrenStart = false;
cs.valueStart = 0;
//cs.state = 0;
i = i + length - 1;
}
if (breakOnErr && cs.isError)
{
errIndex = i;
return i;
}
if (!cs.jsonStart && !cs.arrayStart)//记录当前结束位置。
{
len = i + 1;//长度比索引+1
break;
}
}
}
return len;
}
/// <summary>
/// 设置字符状态(返回true则为关键词返回false则当为普通字符处理
/// </summary>
private static bool SetCharState(char c, ref CharState cs)
{
cs.CheckIsError(c);
switch (c)
{
case '{'://[{ "[{A}]":[{"[{B}]":3,"m":"C"}]}]
#region
if (cs.keyStart <= 0 && cs.valueStart <= 0)
{
cs.keyStart = 0;
cs.valueStart = 0;
if (cs.jsonStart && cs.state == 1)
{
cs.childrenStart = true;
}
else
{
cs.state = 0;
}
cs.jsonStart = true;//开始。
return true;
}
#endregion
break;
case '}':
#region
if (cs.keyStart <= 0 && cs.valueStart < 2 && cs.jsonStart)
{
cs.jsonStart = false;//正常结束。
cs.state = 0;
cs.keyStart = 0;
cs.valueStart = 0;
cs.setDicValue = true;
return true;
}
// cs.isError = !cs.jsonStart && cs.state == 0;
#endregion
break;
case '[':
#region
if (!cs.jsonStart)
{
cs.arrayStart = true;
return true;
}
else if (cs.jsonStart && cs.state == 1)
{
cs.childrenStart = true;
return true;
}
#endregion
break;
case ']':
#region
if (cs.arrayStart && !cs.jsonStart && cs.keyStart <= 2 && cs.valueStart <= 0)//[{},333]//这样结束。
{
cs.keyStart = 0;
cs.valueStart = 0;
cs.arrayStart = false;
return true;
}
#endregion
break;
case '"':
case '\'':
#region
if (cs.jsonStart || cs.arrayStart)
{
if (cs.state == 0)//key阶段,有可能是数组["aa",{}]
{
if (cs.keyStart <= 0)
{
cs.keyStart = c == '"' ? 3 : 2;
return true;
}
else if (cs.keyStart == 2 && c == '\'' || cs.keyStart == 3 && c == '"')
{
if (!cs.escapeChar)
{
cs.keyStart = -1;
return true;
}
else
{
cs.escapeChar = false;
}
}
}
else if (cs.state == 1 && cs.jsonStart)//值阶段必须是Json开始了。
{
if (cs.valueStart <= 0)
{
cs.valueStart = c == '"' ? 3 : 2;
return true;
}
else if (cs.valueStart == 2 && c == '\'' || cs.valueStart == 3 && c == '"')
{
if (!cs.escapeChar)
{
cs.valueStart = -1;
return true;
}
else
{
cs.escapeChar = false;
}
}
}
}
#endregion
break;
case ':':
#region
if (cs.jsonStart && cs.keyStart < 2 && cs.valueStart < 2 && cs.state == 0)
{
if (cs.keyStart == 1)
{
cs.keyStart = -1;
}
cs.state = 1;
return true;
}
// cs.isError = !cs.jsonStart || (cs.keyStart < 2 && cs.valueStart < 2 && cs.state == 1);
#endregion
break;
case ',':
#region //["aa",{aa:12,}]
if (cs.jsonStart)
{
if (cs.keyStart < 2 && cs.valueStart < 2 && cs.state == 1)
{
cs.state = 0;
cs.keyStart = 0;
cs.valueStart = 0;
//if (cs.valueStart == 1)
//{
// cs.valueStart = 0;
//}
cs.setDicValue = true;
return true;
}
}
else if (cs.arrayStart && cs.keyStart <= 2)
{
cs.keyStart = 0;
//if (cs.keyStart == 1)
//{
// cs.keyStart = -1;
//}
return true;
}
#endregion
break;
case ' ':
case '\r':
case '\n'://[ "a",\r\n{} ]
case '\0':
case '\t':
if (cs.keyStart <= 0 && cs.valueStart <= 0) //cs.jsonStart &&
{
return true;//跳过空格。
}
break;
default: //值开头。。
if (c == '\\') //转义符号
{
if (cs.escapeChar)
{
cs.escapeChar = false;
}
else
{
cs.escapeChar = true;
return true;
}
}
else
{
cs.escapeChar = false;
}
if (cs.jsonStart || cs.arrayStart) // Json 或数组开始了。
{
if (cs.keyStart <= 0 && cs.state == 0)
{
cs.keyStart = 1;//无引号的
}
else if (cs.valueStart <= 0 && cs.state == 1 && cs.jsonStart)//只有Json开始才有值。
{
cs.valueStart = 1;//无引号的
}
}
break;
}
return false;
}
}
/// <summary>
/// 字符状态
/// </summary>
public class CharState
{
internal bool jsonStart = false;//以 "{"开始了...
internal bool setDicValue = false;// 可以设置字典值了。
internal bool escapeChar = false;//以"\"转义符号开始了
/// <summary>
/// 数组开始【仅第一开头才算】值嵌套的以【childrenStart】来标识。
/// </summary>
internal bool arrayStart = false;//以"[" 符号开始了
internal bool childrenStart = false;//子级嵌套开始了。
/// <summary>
/// 【0 初始状态,或 遇到“,”逗号】【1 遇到“:”冒号】
/// </summary>
internal int state = 0;
/// <summary>
/// 【-1 取值结束】【0 未开始】【1 无引号开始】【2 单引号开始】【3 双引号开始】
/// </summary>
internal int keyStart = 0;
/// <summary>
/// 【-1 取值结束】【0 未开始】【1 无引号开始】【2 单引号开始】【3 双引号开始】
/// </summary>
internal int valueStart = 0;
internal bool isError = false;//是否语法错误。
internal void CheckIsError(char c)//只当成一级处理因为GetLength会递归到每一个子项处理
{
if (keyStart > 1 || valueStart > 1)
{
return;
}
//示例 ["aa",{"bbbb":123,"fff","ddd"}]
switch (c)
{
case '{'://[{ "[{A}]":[{"[{B}]":3,"m":"C"}]}]
isError = jsonStart && state == 0;//重复开始错误 同时不是值处理。
break;
case '}':
isError = !jsonStart || keyStart != 0 && state == 0;//重复结束错误 或者 提前结束{"aa"}。正常的有{}
break;
case '[':
isError = arrayStart && state == 0;//重复开始错误
break;
case ']':
isError = !arrayStart || jsonStart;//重复开始错误 或者 Json 未结束
break;
case '"':
case '\'':
isError = !(jsonStart || arrayStart); //json 或数组开始。
if (!isError)
{
//重复开始 [""",{"" "}]
isError = state == 0 && keyStart == -1 || state == 1 && valueStart == -1;
}
if (!isError && arrayStart && !jsonStart && c == '\'')//['aa',{}]
{
isError = true;
}
break;
case ':':
isError = !jsonStart || state == 1;//重复出现。
break;
case ',':
isError = !(jsonStart || arrayStart); //json 或数组开始。
if (!isError)
{
if (jsonStart)
{
isError = state == 0 || state == 1 && valueStart > 1;//重复出现。
}
else if (arrayStart)//["aa,] [,] [{},{}]
{
isError = keyStart == 0 && !setDicValue;
}
}
break;
case ' ':
case '\r':
case '\n'://[ "a",\r\n{} ]
case '\0':
case '\t':
break;
default: //值开头。。
isError = !jsonStart && !arrayStart || state == 0 && keyStart == -1 || valueStart == -1 && state == 1;//
break;
}
//if (isError)
//{
//}
}
}
}

View File

@@ -0,0 +1,132 @@
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace Yi.Framework.Infrastructure.Helper
{
public class MD5Helper
{
/// <summary>
/// 生成PasswordSalt
/// </summary>
/// <returns>返回string</returns>
public static string GenerateSalt()
{
byte[] buf = new byte[16];
#pragma warning disable SYSLIB0023 // 类型或成员已过时
new RNGCryptoServiceProvider().GetBytes(buf);
#pragma warning restore SYSLIB0023 // 类型或成员已过时
return Convert.ToBase64String(buf);
}
/// <summary>
/// 加密密码
/// </summary>
/// <param name="pass">密码</param>
/// <param name="passwordFormat">加密类型</param>
/// <param name="salt">PasswordSalt</param>
/// <returns>加密后的密码</returns>
public static string SHA2Encode(string pass, string salt, int passwordFormat = 1)
{
if (passwordFormat == 0) // MembershipPasswordFormat.Clear
return pass;
byte[] bIn = Encoding.Unicode.GetBytes(pass);
byte[] bSalt = Convert.FromBase64String(salt);
byte[] bAll = new byte[bSalt.Length + bIn.Length];
byte[]? bRet = null;
Buffer.BlockCopy(bSalt, 0, bAll, 0, bSalt.Length);
Buffer.BlockCopy(bIn, 0, bAll, bSalt.Length, bIn.Length);
#pragma warning disable SYSLIB0021 // 类型或成员已过时
var s = SHA512.Create();
#pragma warning restore SYSLIB0021 // 类型或成员已过时
bRet = s.ComputeHash(bAll);
return ConvertEx.ToUrlBase64String(bRet);
}
/// <summary>
/// 16位MD5加密
/// </summary>
/// <param name="password"></param>
/// <returns></returns>
public static string MD5Encrypt16(string password)
{
var md5 = MD5.Create();
string t2 = BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(password)), 4, 8);
t2 = t2.Replace("-", string.Empty);
return t2;
}
/// <summary>
/// 32位MD5加密
/// </summary>
/// <param name="password"></param>
/// <returns></returns>
public static string MD5Encrypt32(string password = "")
{
string pwd = string.Empty;
try
{
if (!string.IsNullOrEmpty(password) && !string.IsNullOrWhiteSpace(password))
{
MD5 md5 = MD5.Create(); //实例化一个md5对像
// 加密后是一个字节类型的数组这里要注意编码UTF8/Unicode等的选择 
byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(password));
// 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得
foreach (var item in s)
{
// 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母如果使用大写X则格式后的字符是大写字符
pwd = string.Concat(pwd, item.ToString("X2"));
}
}
}
catch
{
throw new Exception($"错误的 password 字符串:【{password}】");
}
return pwd;
}
/// <summary>
/// 64位MD5加密
/// </summary>
/// <param name="password"></param>
/// <returns></returns>
public static string MD5Encrypt64(string password)
{
// 实例化一个md5对像
// 加密后是一个字节类型的数组这里要注意编码UTF8/Unicode等的选择 
MD5 md5 = MD5.Create();
byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(password));
return Convert.ToBase64String(s);
}
}
public class ConvertEx
{
static readonly char[] padding = { '=' };
public static string ToUrlBase64String(byte[] inArray)
{
var str = Convert.ToBase64String(inArray);
str = str.TrimEnd(padding).Replace('+', '-').Replace('/', '_');
return str;
}
public static byte[] FromUrlBase64String(string s)
{
string incoming = s.Replace('_', '/').Replace('-', '+');
switch (s.Length % 4)
{
case 2: incoming += "=="; break;
case 3: incoming += "="; break;
}
byte[] bytes = Convert.FromBase64String(incoming);
return bytes;
}
}
}

View File

@@ -0,0 +1,260 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Infrastructure.Enums;
namespace Yi.Framework.Infrastructure.Helper
{
public static class MimeHelper
{
// 通过自己定义一个静态类
// 将所有的Content Type都扔进去吧
// 调用的时候直接调用静态方法即可。
public static List<string> ImageType { get; set; } = new List<string>
{
".jpg",".png",".jpge"
};
private static Hashtable _mimeMappingTable;
private static void AddMimeMapping(string extension, string MimeType)
{
_mimeMappingTable.Add(extension, MimeType);
}
public static string GetMimeMapping(string FileName)
{
string text = null!;
int num = FileName.LastIndexOf('.');
if (0 < num && num > FileName.LastIndexOf('\\'))
{
text = (string)_mimeMappingTable[FileName.Substring(num)]!;
}
if (text == null)
{
text = (string)_mimeMappingTable[".*"]!;
}
return text;
}
public static FileTypeEnum GetFileType(string fileName)
{
var extension = Path.GetExtension(fileName);
if (ImageType.Contains(extension.ToLower()))
return FileTypeEnum.Image;
return FileTypeEnum.File;
}
static MimeHelper()
{
_mimeMappingTable = new Hashtable(190, StringComparer.CurrentCultureIgnoreCase);
AddMimeMapping(".323", "text/h323");
AddMimeMapping(".asx", "video/x-ms-asf");
AddMimeMapping(".acx", "application/internet-property-stream");
AddMimeMapping(".ai", "application/postscript");
AddMimeMapping(".aif", "audio/x-aiff");
AddMimeMapping(".aiff", "audio/aiff");
AddMimeMapping(".axs", "application/olescript");
AddMimeMapping(".aifc", "audio/aiff");
AddMimeMapping(".asr", "video/x-ms-asf");
AddMimeMapping(".avi", "video/x-msvideo");
AddMimeMapping(".asf", "video/x-ms-asf");
AddMimeMapping(".au", "audio/basic");
AddMimeMapping(".application", "application/x-ms-application");
AddMimeMapping(".bin", "application/octet-stream");
AddMimeMapping(".bas", "text/plain");
AddMimeMapping(".bcpio", "application/x-bcpio");
AddMimeMapping(".bmp", "image/bmp");
AddMimeMapping(".cdf", "application/x-cdf");
AddMimeMapping(".cat", "application/vndms-pkiseccat");
AddMimeMapping(".crt", "application/x-x509-ca-cert");
AddMimeMapping(".c", "text/plain");
AddMimeMapping(".css", "text/css");
AddMimeMapping(".cer", "application/x-x509-ca-cert");
AddMimeMapping(".crl", "application/pkix-crl");
AddMimeMapping(".cmx", "image/x-cmx");
AddMimeMapping(".csh", "application/x-csh");
AddMimeMapping(".cod", "image/cis-cod");
AddMimeMapping(".cpio", "application/x-cpio");
AddMimeMapping(".clp", "application/x-msclip");
AddMimeMapping(".crd", "application/x-mscardfile");
AddMimeMapping(".deploy", "application/octet-stream");
AddMimeMapping(".dll", "application/x-msdownload");
AddMimeMapping(".dot", "application/msword");
AddMimeMapping(".doc", "application/msword");
AddMimeMapping(".dvi", "application/x-dvi");
AddMimeMapping(".dir", "application/x-director");
AddMimeMapping(".dxr", "application/x-director");
AddMimeMapping(".der", "application/x-x509-ca-cert");
AddMimeMapping(".dib", "image/bmp");
AddMimeMapping(".dcr", "application/x-director");
AddMimeMapping(".disco", "text/xml");
AddMimeMapping(".exe", "application/octet-stream");
AddMimeMapping(".etx", "text/x-setext");
AddMimeMapping(".evy", "application/envoy");
AddMimeMapping(".eml", "message/rfc822");
AddMimeMapping(".eps", "application/postscript");
AddMimeMapping(".flr", "x-world/x-vrml");
AddMimeMapping(".fif", "application/fractals");
AddMimeMapping(".gtar", "application/x-gtar");
AddMimeMapping(".gif", "image/gif");
AddMimeMapping(".gz", "application/x-gzip");
AddMimeMapping(".hta", "application/hta");
AddMimeMapping(".htc", "text/x-component");
AddMimeMapping(".htt", "text/webviewhtml");
AddMimeMapping(".h", "text/plain");
AddMimeMapping(".hdf", "application/x-hdf");
AddMimeMapping(".hlp", "application/winhlp");
AddMimeMapping(".html", "text/html");
AddMimeMapping(".htm", "text/html");
AddMimeMapping(".hqx", "application/mac-binhex40");
AddMimeMapping(".isp", "application/x-internet-signup");
AddMimeMapping(".iii", "application/x-iphone");
AddMimeMapping(".ief", "image/ief");
AddMimeMapping(".ivf", "video/x-ivf");
AddMimeMapping(".ins", "application/x-internet-signup");
AddMimeMapping(".ico", "image/x-icon");
AddMimeMapping(".jpg", "image/jpeg");
AddMimeMapping(".jfif", "image/pjpeg");
AddMimeMapping(".jpe", "image/jpeg");
AddMimeMapping(".jpeg", "image/jpeg");
AddMimeMapping(".js", "application/x-javascript");
AddMimeMapping(".lsx", "video/x-la-asf");
AddMimeMapping(".latex", "application/x-latex");
AddMimeMapping(".lsf", "video/x-la-asf");
AddMimeMapping(".manifest", "application/x-ms-manifest");
AddMimeMapping(".mhtml", "message/rfc822");
AddMimeMapping(".mny", "application/x-msmoney");
AddMimeMapping(".mht", "message/rfc822");
AddMimeMapping(".mid", "audio/mid");
AddMimeMapping(".mpv2", "video/mpeg");
AddMimeMapping(".man", "application/x-troff-man");
AddMimeMapping(".mvb", "application/x-msmediaview");
AddMimeMapping(".mpeg", "video/mpeg");
AddMimeMapping(".m3u", "audio/x-mpegurl");
AddMimeMapping(".mdb", "application/x-msaccess");
AddMimeMapping(".mpp", "application/vnd.ms-project");
AddMimeMapping(".m1v", "video/mpeg");
AddMimeMapping(".mpa", "video/mpeg");
AddMimeMapping(".me", "application/x-troff-me");
AddMimeMapping(".m13", "application/x-msmediaview");
AddMimeMapping(".movie", "video/x-sgi-movie");
AddMimeMapping(".m14", "application/x-msmediaview");
AddMimeMapping(".mpe", "video/mpeg");
AddMimeMapping(".mp2", "video/mpeg");
AddMimeMapping(".mov", "video/quicktime");
AddMimeMapping(".mp3", "audio/mpeg");
AddMimeMapping(".mpg", "video/mpeg");
AddMimeMapping(".ms", "application/x-troff-ms");
AddMimeMapping(".nc", "application/x-netcdf");
AddMimeMapping(".nws", "message/rfc822");
AddMimeMapping(".oda", "application/oda");
AddMimeMapping(".ods", "application/oleobject");
AddMimeMapping(".pmc", "application/x-perfmon");
AddMimeMapping(".p7r", "application/x-pkcs7-certreqresp");
AddMimeMapping(".p7b", "application/x-pkcs7-certificates");
AddMimeMapping(".p7s", "application/pkcs7-signature");
AddMimeMapping(".pmw", "application/x-perfmon");
AddMimeMapping(".ps", "application/postscript");
AddMimeMapping(".p7c", "application/pkcs7-mime");
AddMimeMapping(".pbm", "image/x-portable-bitmap");
AddMimeMapping(".ppm", "image/x-portable-pixmap");
AddMimeMapping(".pub", "application/x-mspublisher");
AddMimeMapping(".pnm", "image/x-portable-anymap");
AddMimeMapping(".png", "image/png");
AddMimeMapping(".pml", "application/x-perfmon");
AddMimeMapping(".p10", "application/pkcs10");
AddMimeMapping(".pfx", "application/x-pkcs12");
AddMimeMapping(".p12", "application/x-pkcs12");
AddMimeMapping(".pdf", "application/pdf");
AddMimeMapping(".pps", "application/vnd.ms-powerpoint");
AddMimeMapping(".p7m", "application/pkcs7-mime");
AddMimeMapping(".pko", "application/vndms-pkipko");
AddMimeMapping(".ppt", "application/vnd.ms-powerpoint");
AddMimeMapping(".pmr", "application/x-perfmon");
AddMimeMapping(".pma", "application/x-perfmon");
AddMimeMapping(".pot", "application/vnd.ms-powerpoint");
AddMimeMapping(".prf", "application/pics-rules");
AddMimeMapping(".pgm", "image/x-portable-graymap");
AddMimeMapping(".qt", "video/quicktime");
AddMimeMapping(".ra", "audio/x-pn-realaudio");
AddMimeMapping(".rgb", "image/x-rgb");
AddMimeMapping(".ram", "audio/x-pn-realaudio");
AddMimeMapping(".rmi", "audio/mid");
AddMimeMapping(".ras", "image/x-cmu-raster");
AddMimeMapping(".roff", "application/x-troff");
AddMimeMapping(".rtf", "application/rtf");
AddMimeMapping(".rtx", "text/richtext");
AddMimeMapping(".sv4crc", "application/x-sv4crc");
AddMimeMapping(".spc", "application/x-pkcs7-certificates");
AddMimeMapping(".setreg", "application/set-registration-initiation");
AddMimeMapping(".snd", "audio/basic");
AddMimeMapping(".stl", "application/vndms-pkistl");
AddMimeMapping(".setpay", "application/set-payment-initiation");
AddMimeMapping(".stm", "text/html");
AddMimeMapping(".shar", "application/x-shar");
AddMimeMapping(".sh", "application/x-sh");
AddMimeMapping(".sit", "application/x-stuffit");
AddMimeMapping(".spl", "application/futuresplash");
AddMimeMapping(".sct", "text/scriptlet");
AddMimeMapping(".scd", "application/x-msschedule");
AddMimeMapping(".sst", "application/vndms-pkicertstore");
AddMimeMapping(".src", "application/x-wais-source");
AddMimeMapping(".sv4cpio", "application/x-sv4cpio");
AddMimeMapping(".tex", "application/x-tex");
AddMimeMapping(".tgz", "application/x-compressed");
AddMimeMapping(".t", "application/x-troff");
AddMimeMapping(".tar", "application/x-tar");
AddMimeMapping(".tr", "application/x-troff");
AddMimeMapping(".tif", "image/tiff");
AddMimeMapping(".txt", "text/plain");
AddMimeMapping(".texinfo", "application/x-texinfo");
AddMimeMapping(".trm", "application/x-msterminal");
AddMimeMapping(".tiff", "image/tiff");
AddMimeMapping(".tcl", "application/x-tcl");
AddMimeMapping(".texi", "application/x-texinfo");
AddMimeMapping(".tsv", "text/tab-separated-values");
AddMimeMapping(".ustar", "application/x-ustar");
AddMimeMapping(".uls", "text/iuls");
AddMimeMapping(".vcf", "text/x-vcard");
AddMimeMapping(".wps", "application/vnd.ms-works");
AddMimeMapping(".wav", "audio/wav");
AddMimeMapping(".wrz", "x-world/x-vrml");
AddMimeMapping(".wri", "application/x-mswrite");
AddMimeMapping(".wks", "application/vnd.ms-works");
AddMimeMapping(".wmf", "application/x-msmetafile");
AddMimeMapping(".wcm", "application/vnd.ms-works");
AddMimeMapping(".wrl", "x-world/x-vrml");
AddMimeMapping(".wdb", "application/vnd.ms-works");
AddMimeMapping(".wsdl", "text/xml");
AddMimeMapping(".xap", "application/x-silverlight-app");
AddMimeMapping(".xml", "text/xml");
AddMimeMapping(".xlm", "application/vnd.ms-excel");
AddMimeMapping(".xaf", "x-world/x-vrml");
AddMimeMapping(".xla", "application/vnd.ms-excel");
AddMimeMapping(".xls", "application/vnd.ms-excel");
AddMimeMapping(".xlsx", "application/vnd.ms-excel");
AddMimeMapping(".xof", "x-world/x-vrml");
AddMimeMapping(".xlt", "application/vnd.ms-excel");
AddMimeMapping(".xlc", "application/vnd.ms-excel");
AddMimeMapping(".xsl", "text/xml");
AddMimeMapping(".xbm", "image/x-xbitmap");
AddMimeMapping(".xlw", "application/vnd.ms-excel");
AddMimeMapping(".xpm", "image/x-xpixmap");
AddMimeMapping(".xwd", "image/x-xwindowdump");
AddMimeMapping(".xsd", "text/xml");
AddMimeMapping(".z", "application/x-compress");
AddMimeMapping(".zip", "application/x-zip-compressed");
AddMimeMapping(".*", "application/octet-stream");
}
}
}

View File

@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Helper
{
public class RSAFileHelper
{
public static RSA GetKey()
{
return GetRSA("key.pem");
}
public static RSA GetPublicKey()
{
return GetRSA("public.pem");
}
private static RSA GetRSA(string fileName)
{
string rootPath = Directory.GetCurrentDirectory();
string filePath = Path.Combine(rootPath, fileName);
if (!File.Exists(filePath))
throw new Exception("文件不存在");
string key = File.ReadAllText(filePath);
var rsa = RSA.Create();
rsa.ImportFromPem(key.AsSpan());
return rsa;
}
}
}

View File

@@ -0,0 +1,390 @@
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace Yi.Framework.Infrastructure.Helper
{
/// <summary>
/// RSA加解密 使用OpenSSL的公钥加密/私钥解密
/// 公私钥请使用openssl生成
/// </summary>
public class RSAHelper
{
public readonly RSA? _privateKeyRsaProvider;
public readonly RSA? _publicKeyRsaProvider;
private readonly HashAlgorithmName _hashAlgorithmName;
private readonly Encoding _encoding;
/// <summary>
/// 实例化RSAHelper
/// </summary>
/// <param name="rsaType">加密算法类型 RSA SHA1;RSA2 SHA256 密钥长度至少为2048</param>
/// <param name="encoding">编码类型</param>
/// <param name="privateKey">私钥</param>
/// <param name="publicKey">公钥</param>
public RSAHelper(RSAType rsaType, Encoding encoding, string privateKey, string? publicKey = null)
{
_encoding = encoding;
if (!string.IsNullOrEmpty(privateKey))
{
_privateKeyRsaProvider = CreateRsaProviderFromPrivateKey(privateKey);
}
if (!string.IsNullOrEmpty(publicKey))
{
_publicKeyRsaProvider = CreateRsaProviderFromPublicKey(publicKey);
}
_hashAlgorithmName = rsaType == RSAType.RSA ? HashAlgorithmName.SHA1 : HashAlgorithmName.SHA256;
}
#region 使
/// <summary>
/// 使用私钥签名
/// </summary>
/// <param name="data">原始数据</param>
/// <returns></returns>
public string Sign(string data)
{
byte[] dataBytes = _encoding.GetBytes(data);
var signatureBytes = _privateKeyRsaProvider!.SignData(dataBytes, _hashAlgorithmName, RSASignaturePadding.Pkcs1);
return Convert.ToBase64String(signatureBytes);
}
#endregion
#region 使
/// <summary>
/// 使用公钥验签
/// </summary>
/// <param name="data">原始数据</param>
/// <param name="sign">签名</param>
/// <returns></returns>
public bool Verify(string data, string sign)
{
byte[] dataBytes = _encoding.GetBytes(data);
byte[] signBytes = Convert.FromBase64String(sign);
var verify = _publicKeyRsaProvider!.VerifyData(dataBytes, signBytes, _hashAlgorithmName, RSASignaturePadding.Pkcs1);
return verify;
}
#endregion
#region
/// <summary>
/// 私钥解密(原)
/// </summary>
/// <param name="cipherText">解密字符串(base64)</param>
/// <returns></returns>
//public string Decrypt(string cipherText)
//{
// if (_privateKeyRsaProvider == null)
// {
// throw new Exception("_privateKeyRsaProvider is null");
// }
// return _encoding.GetString(_privateKeyRsaProvider.Decrypt(Convert.FromBase64String(cipherText), RSAEncryptionPadding.Pkcs1));
//}
/// <summary>
/// 私钥解密(支持大量数据)
/// </summary>
/// <param name="cipherText"></param>
/// <returns></returns>
public string Decrypt(string cipherText)
{
if (_privateKeyRsaProvider == null)
{
throw new Exception("_privateKeyRsaProvider is null");
}
var bufferSize = _privateKeyRsaProvider.KeySize / 8;
byte[] buffer = new byte[bufferSize];//待解密块
using (MemoryStream msInput = new MemoryStream(Convert.FromBase64String(cipherText)))
{
using (MemoryStream msOutput = new MemoryStream())
{
int readLen; while ((readLen = msInput.Read(buffer, 0, bufferSize)) > 0)
{
byte[] dataToEnc = new byte[readLen];
Array.Copy(buffer, 0, dataToEnc, 0, readLen); byte[] encData = _privateKeyRsaProvider.Decrypt(dataToEnc, RSAEncryptionPadding.Pkcs1);
msOutput.Write(encData, 0, encData.Length);
}
byte[] result = msOutput.ToArray();
return _encoding.GetString(result);
}
}
}
#endregion
#region
/// <summary>
/// 公钥加密(原)
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
//public string Encrypt(string text)
//{
// if (_publicKeyRsaProvider == null)
// {
// throw new Exception("_publicKeyRsaProvider is null");
// }
// return Convert.ToBase64String(_publicKeyRsaProvider.Encrypt(Encoding.UTF8.GetBytes(text), RSAEncryptionPadding.Pkcs1));
//}
/// <summary>
/// 公钥加密(支持大量数据)
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public string Encrypt(string text)
{
if (_publicKeyRsaProvider == null)
{
throw new Exception("_publicKeyRsaProvider is null");
}
var bufferSize = _publicKeyRsaProvider.KeySize / 8 - 11;
byte[] buffer = new byte[bufferSize];//待加密块
using (MemoryStream msInput = new MemoryStream(_encoding.GetBytes(text)))
{
using (MemoryStream msOutput = new MemoryStream())
{
int readLen; while ((readLen = msInput.Read(buffer, 0, bufferSize)) > 0)
{
byte[] dataToEnc = new byte[readLen];
Array.Copy(buffer, 0, dataToEnc, 0, readLen); byte[] encData = _publicKeyRsaProvider.Encrypt(dataToEnc, RSAEncryptionPadding.Pkcs1);
msOutput.Write(encData, 0, encData.Length);
}
byte[] result = msOutput.ToArray();
return Convert.ToBase64String(result);
}
}
}
#endregion
#region 使RSA实例
/// <summary>
/// 使用私钥创建RSA实例
/// </summary>
/// <param name="privateKey"></param>
/// <returns></returns>
private RSA CreateRsaProviderFromPrivateKey(string privateKey)
{
var privateKeyBits = Convert.FromBase64String(privateKey);
var rsa = RSA.Create();
var rsaParameters = new RSAParameters();
using (BinaryReader binr = new BinaryReader(new MemoryStream(privateKeyBits)))
{
byte bt = 0;
ushort twobytes = 0;
twobytes = binr.ReadUInt16();
if (twobytes == 0x8130)
binr.ReadByte();
else if (twobytes == 0x8230)
binr.ReadInt16();
else
throw new Exception("Unexpected value read binr.ReadUInt16()");
twobytes = binr.ReadUInt16();
if (twobytes != 0x0102)
throw new Exception("Unexpected version");
bt = binr.ReadByte();
if (bt != 0x00)
throw new Exception("Unexpected value read binr.ReadByte()");
rsaParameters.Modulus = binr.ReadBytes(GetIntegerSize(binr));
rsaParameters.Exponent = binr.ReadBytes(GetIntegerSize(binr));
rsaParameters.D = binr.ReadBytes(GetIntegerSize(binr));
rsaParameters.P = binr.ReadBytes(GetIntegerSize(binr));
rsaParameters.Q = binr.ReadBytes(GetIntegerSize(binr));
rsaParameters.DP = binr.ReadBytes(GetIntegerSize(binr));
rsaParameters.DQ = binr.ReadBytes(GetIntegerSize(binr));
rsaParameters.InverseQ = binr.ReadBytes(GetIntegerSize(binr));
}
rsa.ImportParameters(rsaParameters);
return rsa;
}
#endregion
#region 使RSA实例
/// <summary>
/// 使用公钥创建RSA实例
/// </summary>
/// <param name="publicKeyString"></param>
/// <returns></returns>
public RSA? CreateRsaProviderFromPublicKey(string publicKeyString)
{
// encoded OID sequence for PKCS #1 rsaEncryption szOID_RSA_RSA = "1.2.840.113549.1.1.1"
byte[] seqOid = { 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00 };
byte[] seq = new byte[15];
var x509Key = Convert.FromBase64String(publicKeyString);
// --------- Set up stream to read the asn.1 encoded SubjectPublicKeyInfo blob ------
using (MemoryStream mem = new MemoryStream(x509Key))
{
using (BinaryReader binr = new BinaryReader(mem)) //wrap Memory Stream with BinaryReader for easy reading
{
byte bt = 0;
ushort twobytes = 0;
twobytes = binr.ReadUInt16();
if (twobytes == 0x8130) //data read as little endian order (actual data order for Sequence is 30 81)
binr.ReadByte(); //advance 1 byte
else if (twobytes == 0x8230)
binr.ReadInt16(); //advance 2 bytes
else
return null;
seq = binr.ReadBytes(15); //read the Sequence OID
if (!CompareBytearrays(seq, seqOid)) //make sure Sequence for OID is correct
return null;
twobytes = binr.ReadUInt16();
if (twobytes == 0x8103) //data read as little endian order (actual data order for Bit String is 03 81)
binr.ReadByte(); //advance 1 byte
else if (twobytes == 0x8203)
binr.ReadInt16(); //advance 2 bytes
else
return null;
bt = binr.ReadByte();
if (bt != 0x00) //expect null byte next
return null;
twobytes = binr.ReadUInt16();
if (twobytes == 0x8130) //data read as little endian order (actual data order for Sequence is 30 81)
binr.ReadByte(); //advance 1 byte
else if (twobytes == 0x8230)
binr.ReadInt16(); //advance 2 bytes
else
return null;
twobytes = binr.ReadUInt16();
byte lowbyte = 0x00;
byte highbyte = 0x00;
if (twobytes == 0x8102) //data read as little endian order (actual data order for Integer is 02 81)
lowbyte = binr.ReadByte(); // read next bytes which is bytes in modulus
else if (twobytes == 0x8202)
{
highbyte = binr.ReadByte(); //advance 2 bytes
lowbyte = binr.ReadByte();
}
else
return null;
byte[] modint = { lowbyte, highbyte, 0x00, 0x00 }; //reverse byte order since asn.1 key uses big endian order
int modsize = BitConverter.ToInt32(modint, 0);
int firstbyte = binr.PeekChar();
if (firstbyte == 0x00)
{ //if first byte (highest order) of modulus is zero, don't include it
binr.ReadByte(); //skip this null byte
modsize -= 1; //reduce modulus buffer size by 1
}
byte[] modulus = binr.ReadBytes(modsize); //read the modulus bytes
if (binr.ReadByte() != 0x02) //expect an Integer for the exponent data
return null;
int expbytes = binr.ReadByte(); // should only need one byte for actual exponent data (for all useful values)
byte[] exponent = binr.ReadBytes(expbytes);
// ------- create RSACryptoServiceProvider instance and initialize with public key -----
var rsa = RSA.Create();
RSAParameters rsaKeyInfo = new RSAParameters
{
Modulus = modulus,
Exponent = exponent
};
rsa.ImportParameters(rsaKeyInfo);
return rsa;
}
}
}
#endregion
#region
private int GetIntegerSize(BinaryReader binr)
{
byte bt = 0;
int count = 0;
bt = binr.ReadByte();
if (bt != 0x02)
return 0;
bt = binr.ReadByte();
if (bt == 0x81)
count = binr.ReadByte();
else
if (bt == 0x82)
{
var highbyte = binr.ReadByte();
var lowbyte = binr.ReadByte();
byte[] modint = { lowbyte, highbyte, 0x00, 0x00 };
count = BitConverter.ToInt32(modint, 0);
}
else
{
count = bt;
}
while (binr.ReadByte() == 0x00)
{
count -= 1;
}
binr.BaseStream.Seek(-1, SeekOrigin.Current);
return count;
}
private bool CompareBytearrays(byte[] a, byte[] b)
{
if (a.Length != b.Length)
return false;
int i = 0;
foreach (byte c in a)
{
if (c != b[i])
return false;
i++;
}
return true;
}
#endregion
}
/// <summary>
/// RSA算法类型
/// </summary>
public enum RSAType
{
/// <summary>
/// SHA1
/// </summary>
RSA = 0,
/// <summary>
/// RSA2 密钥长度至少为2048
/// SHA256
/// </summary>
RSA2
}
}

View File

@@ -0,0 +1,99 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Yi.Framework.Infrastructure.Helper
{
public class RandomHelper
{
public static string replaceBianLiang(string content)
{
content = content.Replace("{当前时间}", DateTime.Now.TimeOfDay.ToString());
string[] bianliang = new string[] { "{随机字母}", "{随机数字}", "{随机汉字}" };
Regex r;
int count;
string readstr = "";
foreach (string str in bianliang)
{
count = (content.Length - content.Replace(str, "").Length) / str.Length;
if (str == "{随机汉字}") readstr = RandChina(count);
if (str == "{随机数字}") readstr = GenerateCheckCodeNum(count);
if (str == "{随机字母}") readstr = GenerateRandomLetter(count);
if (count > readstr.Length) count = readstr.Length;
r = new Regex(str.Replace("{", "\\{").Replace("}", "\\}"));
for (int i = 0; i < count; i++)
{
content = r.Replace(content, readstr.Substring(i, 1), 1);
}
}
return content;
}
/// <summary>
/// 随机生成字母
/// </summary>
/// <param name="Length"></param>
/// <returns></returns>
public static string GenerateRandomLetter(int Length)
{
char[] Pattern = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
string result = "";
int n = Pattern.Length;
Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
for (int i = 0; i < Length; i++)
{
int rnd = random.Next(0, n);
result += Pattern[rnd];
}
return result;
}
/// <summary>
/// 随机生成数字
/// </summary>
/// <param name="codeCount"></param>
/// <returns></returns>
public static string GenerateCheckCodeNum(int codeCount)
{
int rep = 0;
string str = string.Empty;
long num2 = DateTime.Now.Ticks + rep;
rep++;
Random random = new Random((int)((ulong)num2 & 0xffffffffL) | (int)(num2 >> rep));
for (int i = 0; i < codeCount; i++)
{
int num = random.Next();
str = str + ((char)(0x30 + (ushort)(num % 10))).ToString();
}
return str;
}
/// <summary>
/// 此函数为生成指定数目的汉字
/// </summary>
/// <param name="charLen">汉字数目</param>
/// <returns>所有汉字</returns>
public static string RandChina(int charLen)
{
int area, code;//汉字由区位和码位组成(都为0-94,其中区位16-55为一级汉字区,56-87为二级汉字区,1-9为特殊字符区)
StringBuilder strtem = new StringBuilder();
Random rand = new Random();
for (int i = 0; i < charLen; i++)
{
area = rand.Next(16, 88);
if (area == 55)//第55区只有89个字符
{
code = rand.Next(1, 90);
}
else
{
code = rand.Next(1, 94);
}
strtem.Append(Encoding.GetEncoding("GB2312").GetString(new byte[] { Convert.ToByte(area + 160), Convert.ToByte(code + 160) }));
}
return strtem.ToString();
}
}
}

View File

@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Helper
{
public static class ReflexHelper
{
#region
/// <summary>
/// 取对象属性值
/// </summary>
/// <param name="FieldName"></param>
/// <param name="obj"></param>
/// <returns></returns>
public static string GetModelValue(string FieldName, object obj)
{
try
{
Type Ts = obj.GetType();
object o = Ts.GetProperty(FieldName).GetValue(obj, null);
if (null == o)
return null;
string Value = Convert.ToString(o);
if (string.IsNullOrEmpty(Value))
return null;
return Value;
}
catch (Exception ex)
{
throw ex;
}
return null;
}
/// <summary>
/// 设置对象属性值
/// </summary>
/// <param name="FieldName"></param>
/// <param name="Value"></param>
/// <param name="obj"></param>
/// <returns></returns>
public static bool SetModelValue(string FieldName, object Value, object obj)
{
try
{
Type Ts = obj.GetType();
Ts.GetProperty(FieldName).SetValue(obj, Value, null);
return true;
}
catch (Exception ex)
{
throw ex;
}
return false;
}
#endregion
}
}

View File

@@ -0,0 +1,102 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Helper
{
public static class SnowflakeHelper
{
public static long NextId { get => SnowFlakeSingle.Instance.NextId(); }
public static long Next()
{
SnowflakeTool snowflakeTool = new SnowflakeTool(1);
return snowflakeTool.NextId();
}
private class SnowflakeTool
{
//机器ID
private static long nodeId;
private static long twepoch = 687888001020L; //唯一时间,这是一个避免重复的随机量,自行设定不要大于当前时间戳
private static long sequence = 0L;
private static int workerIdBits = 4; //机器码字节数。4个字节用来保存机器码(定义为Long类型会出现最大偏移64位所以左移64位没有意义)
public static long maxWorkerId = -1L ^ -1L << workerIdBits; //最大机器ID
private static int sequenceBits = 10; //计数器字节数10个字节用来保存计数码
private static int workerIdShift = sequenceBits; //机器码数据左移位数,就是后面计数器占用的位数
private static int timestampLeftShift = sequenceBits + workerIdBits; //时间戳左移动位数就是机器码和计数器总字节数
public static long sequenceMask = -1L ^ -1L << sequenceBits; //一微秒内可以产生计数,如果达到该值则等到下一微妙在进行生成
private long lastTimestamp = -1L;
/// <summary>
/// 机器码
/// </summary>
/// <param name="workerId"></param>
public SnowflakeTool(long workerId)
{
if (workerId > maxWorkerId || workerId < 0)
throw new Exception(string.Format("节点id 不能大于 {0} 或者 小于 0 ", workerId));
nodeId = workerId;
}
public long NextId()
{
lock (this)
{
long timestamp = TimeGen();
if (lastTimestamp == timestamp)
{ //同一微妙中生成ID
sequence = sequence + 1 & sequenceMask; //用&运算计算该微秒内产生的计数是否已经到达上限
if (sequence == 0)
{
//一微妙内产生的ID计数已达上限等待下一微妙
timestamp = TillNextMillis(lastTimestamp);
}
}
else
{ //不同微秒生成ID
sequence = 0; //计数清0
}
if (timestamp < lastTimestamp)
{ //如果当前时间戳比上一次生成ID时时间戳还小抛出异常因为不能保证现在生成的ID之前没有生成过
throw new Exception(string.Format("Clock moved backwards. Refusing to generate id for {0} milliseconds",
lastTimestamp - timestamp));
}
lastTimestamp = timestamp; //把当前时间戳保存为最后生成ID的时间戳
long nextId = timestamp - twepoch << timestampLeftShift | nodeId << workerIdShift | sequence;
return nextId;
}
}
/// <summary>
/// 获取下一微秒时间戳
/// </summary>
/// <param name="lastTimestamp"></param>
/// <returns></returns>
private long TillNextMillis(long lastTimestamp)
{
long timestamp = TimeGen();
while (timestamp <= lastTimestamp)
{
timestamp = TimeGen();
}
return timestamp;
}
/// <summary>
/// 生成当前时间戳
/// </summary>
/// <returns></returns>
private long TimeGen()
{
return (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
}
}
}
}

View File

@@ -0,0 +1,113 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace Yi.Framework.Infrastructure.Helper
{
public class StringHelper
{
/// <summary>
/// 根据分隔符返回前n条数据
/// </summary>
/// <param name="content">数据内容</param>
/// <param name="separator">分隔符</param>
/// <param name="top">前n条</param>
/// <param name="isDesc">是否倒序默认false</param>
/// <returns></returns>
public static List<string> GetTopDataBySeparator(string content, string separator, int top, bool isDesc = false)
{
if (string.IsNullOrEmpty(content))
{
return new List<string>() { };
}
if (string.IsNullOrEmpty(separator))
{
throw new ArgumentException("message", nameof(separator));
}
var dataArray = content.Split(separator).Where(d => !string.IsNullOrEmpty(d)).ToArray();
if (isDesc)
{
Array.Reverse(dataArray);
}
if (top > 0)
{
dataArray = dataArray.Take(top).ToArray();
}
return dataArray.ToList();
}
/// <summary>
/// 根据字段拼接get参数
/// </summary>
/// <param name="dic"></param>
/// <returns></returns>
public static string GetPars(Dictionary<string, object> dic)
{
StringBuilder sb = new StringBuilder();
string? urlPars = null;
bool isEnter = false;
foreach (var item in dic)
{
sb.Append($"{(isEnter ? "&" : "")}{item.Key}={item.Value}");
isEnter = true;
}
urlPars = sb.ToString();
return urlPars;
}
/// <summary>
/// 根据字段拼接get参数
/// </summary>
/// <param name="dic"></param>
/// <returns></returns>
public static string GetPars(Dictionary<string, string> dic)
{
StringBuilder sb = new StringBuilder();
string? urlPars = null;
bool isEnter = false;
foreach (var item in dic)
{
sb.Append($"{(isEnter ? "&" : "")}{item.Key}={item.Value}");
isEnter = true;
}
urlPars = sb.ToString();
return urlPars;
}
/// <summary>
/// 获取一个GUID
/// </summary>
/// <param name="format">格式-默认为N</param>
/// <returns></returns>
public static string GetGUID(string format = "N")
{
return Guid.NewGuid().ToString(format);
}
/// <summary>
/// 根据GUID获取19位的唯一数字序列
/// </summary>
/// <returns></returns>
public static long GetGuidToLongID()
{
byte[] buffer = Guid.NewGuid().ToByteArray();
return BitConverter.ToInt64(buffer, 0);
}
/// <summary>
/// 获取字符串最后X行
/// </summary>
/// <param name="resourceStr"></param>
/// <param name="length"></param>
/// <returns></returns>
public static string GetCusLine(string resourceStr, int length)
{
string[] arrStr = resourceStr.Split("\r\n");
return string.Join("", (from q in arrStr select q).Skip(arrStr.Length - length + 1).Take(length).ToArray());
}
}
}

View File

@@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Helper
{
public static class TreeHelper
{
public static List<T> SetTree<T>(List<T> list, Action<T> action = null!)
{
if (list is not null && list.Count > 0)
{
IList<T> result = new List<T>();
long pid = list.Min(m => (m as ITreeModel<T>)!.ParentId);
IList<T> t = list.Where(m => (m as ITreeModel<T>)!.ParentId == pid).ToList();
foreach (T model in t)
{
if (action is not null)
{
action(model);
}
result.Add(model);
var item = model as ITreeModel<T>;
IList<T> children = list.Where(m => (m as ITreeModel<T>)!.ParentId == item!.Id).ToList();
if (children.Count > 0)
{
SetTreeChildren(list, children, model, action!);
}
}
return result.OrderByDescending(m => (m as ITreeModel<T>)!.OrderNum).ToList();
}
return null!;
}
private static void SetTreeChildren<T>(IList<T> list, IList<T> children, T model, Action<T> action = null!)
{
var mm = model as ITreeModel<T>;
mm!.Children = new List<T>();
foreach (T item in children)
{
if (action is not null)
{
action(item);
}
mm.Children.Add(item);
var _item = item as ITreeModel<T>;
IList<T> _children = list.Where(m => (m as ITreeModel<T>)!.ParentId == _item!.Id).ToList();
if (_children.Count > 0)
{
SetTreeChildren(list, _children, item, action!);
}
}
mm.Children = mm.Children.OrderByDescending(m => (m as ITreeModel<T>)!.OrderNum).ToList();
}
public interface ITreeModel<T>
{
public long Id { get; set; }
public long ParentId { get; set; }
public int OrderNum { get; set; }
public List<T>? Children { get; set; }
}
}
}

View File

@@ -0,0 +1,47 @@
using System;
using System.Text;
using System.Text.RegularExpressions;
namespace Yi.Framework.Infrastructure.Helper
{
public static class UnicodeHelper
{
/// <summary>
/// 字符串转Unicode码
/// </summary>
/// <returns>The to unicode.</returns>
/// <param name="value">Value.</param>
public static string StringToUnicode(string value)
{
byte[] bytes = Encoding.Unicode.GetBytes(value);
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < bytes.Length; i += 2)
{
// 取两个字符,每个字符都是右对齐。
stringBuilder.AppendFormat("u{0}{1}", bytes[i + 1].ToString("x").PadLeft(2, '0'), bytes[i].ToString("x").PadLeft(2, '0'));
}
return stringBuilder.ToString();
}
/// <summary>
/// Unicode转字符串
/// </summary>
/// <returns>The to string.</returns>
/// <param name="unicode">Unicode.</param>
public static string UnicodeToString(string unicode)
{
unicode = unicode.Replace("%", "\\");
return new Regex(@"\\u([0-9A-F]{4})", RegexOptions.IgnoreCase | RegexOptions.Compiled).Replace(
unicode, x => string.Empty + Convert.ToChar(Convert.ToUInt16(x.Result("$1"), 16)));
//string resultStr = "";
//string[] strList = unicode.Split('u');
//for (int i = 1; i < strList.Length; i++)
//{
// resultStr += (char)int.Parse(strList[i], System.Globalization.NumberStyles.HexNumber);
//}
//return resultStr;
}
}
}

View File

@@ -0,0 +1,24 @@
namespace Yi.Framework.Infrastructure.Helper
{
public class UrlHelper
{
/// <summary>
/// UrlEncode编码
/// </summary>
/// <param name="url">url</param>
/// <returns></returns>
public static string UrlEncode(string url)
{
return System.Web.HttpUtility.UrlEncode(url, System.Text.Encoding.UTF8);
}
/// <summary>
/// UrlEncode解码
/// </summary>
/// <param name="data">数据</param>
/// <returns></returns>
public static string UrlDecode(string data)
{
return System.Web.HttpUtility.UrlDecode(data, System.Text.Encoding.UTF8);
}
}
}

View File

@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
namespace Yi.Framework.Infrastructure.Helper
{
public class XmlHelper
{
/// <summary>
/// 转换对象为JSON格式数据
/// </summary>
/// <typeparam name="T">类</typeparam>
/// <param name="obj">对象</param>
/// <returns>字符格式的JSON数据</returns>
public static string? GetXML<T>(object obj)
{
try
{
XmlSerializer xs = new XmlSerializer(typeof(T));
using (TextWriter tw = new StringWriter())
{
xs.Serialize(tw, obj);
return tw.ToString();
}
}
catch (Exception)
{
return string.Empty;
}
}
/// <summary>
/// Xml格式字符转换为T类型的对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="xml"></param>
/// <returns></returns>
public static T ParseFormByXml<T>(string xml, string rootName = "root")
{
XmlSerializer serializer = new XmlSerializer(typeof(T), new XmlRootAttribute(rootName));
StringReader reader = new StringReader(xml);
T res = (T)serializer.Deserialize(reader)!;
reader.Close();
reader.Dispose();
return res;
}
}
}

View File

@@ -0,0 +1,147 @@
using System.Linq.Expressions;
using Furion.DependencyInjection;
using SqlSugar;
using Yi.Framework.Infrastructure.Data.Entities;
using Yi.Framework.Infrastructure.Ddd.Dtos.Abstract;
using Yi.Framework.Infrastructure.Ddd.Repositories;
using Yi.Framework.Infrastructure.Enums;
using Yi.Framework.Infrastructure.Helper;
namespace Yi.Framework.Infrastructure.Sqlsugar.Repositories
{
public class SqlsugarRepository<T> : SimpleClient<T>, IRepository<T> ,ITransient where T : class, new()
{
public SqlsugarRepository(ISqlSugarClient context) : base(context)
{
}
/// <summary>
/// 注释一下严格意义这里应该protected但是我认为 简易程度 与 耦合程度 中是需要进行衡量的
/// </summary>
public ISugarQueryable<T> _DbQueryable => AsQueryable();
protected ISqlSugarClient _Db { get { return Context; } set { } }
public async Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, IPagedAndSortedResultRequestDto page)
{
return await base.GetPageListAsync(whereExpression, new PageModel { PageIndex = page.PageNum, PageSize = page.PageSize });
}
public async Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, IPagedAndSortedResultRequestDto page, Expression<Func<T, object>>? orderByExpression = null, OrderByEnum orderByType = OrderByEnum.Asc)
{
return await base.GetPageListAsync(whereExpression, new PageModel { PageIndex = page.PageNum, PageSize = page.PageSize }, orderByExpression, orderByType.EnumToEnum<OrderByType>());
}
public async Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, IPagedAndSortedResultRequestDto page, string? orderBy, OrderByEnum orderByType = OrderByEnum.Asc)
{
return await _DbQueryable.Where(whereExpression).OrderByIF(orderBy is not null, orderBy + " " + orderByType.ToString().ToLower()).ToPageListAsync(page.PageNum, page.PageSize);
}
public async Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, int pageNum, int pageSize)
{
return await base.GetPageListAsync(whereExpression, new PageModel { PageIndex = pageNum, PageSize = pageSize });
}
public async Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, int pageNum, int pageSize, Expression<Func<T, object>>? orderByExpression = null, OrderByEnum orderByType = OrderByEnum.Asc)
{
return await base.GetPageListAsync(whereExpression, new PageModel { PageIndex = pageNum, PageSize = pageSize }, orderByExpression, orderByType.EnumToEnum<OrderByType>());
}
public async Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, int pageNum, int pageSize, string? orderBy, OrderByEnum orderByType = OrderByEnum.Asc)
{
return await _DbQueryable.Where(whereExpression).OrderByIF(orderBy is not null, orderBy + " " + orderByType.ToString().ToLower()).ToPageListAsync(pageNum, pageSize);
}
public async Task<bool> UpdateIgnoreNullAsync(T updateObj)
{
return await _Db.Updateable(updateObj).IgnoreColumns(true).ExecuteCommandAsync() > 0;
}
public override async Task<bool> DeleteAsync(T deleteObj)
{
//逻辑删除
if (deleteObj is ISoftDelete)
{
//反射赋值
ReflexHelper.SetModelValue(nameof(ISoftDelete.IsDeleted), true, deleteObj);
return await UpdateAsync(deleteObj);
}
else
{
return await base.DeleteAsync(deleteObj);
}
}
public override async Task<bool> DeleteAsync(List<T> deleteObjs)
{
if (typeof(ISoftDelete).IsAssignableFrom(typeof(T)))
{
//反射赋值
deleteObjs.ForEach(e => ReflexHelper.SetModelValue(nameof(ISoftDelete.IsDeleted), true, e));
return await UpdateRangeAsync(deleteObjs);
}
else
{
return await base.DeleteAsync(deleteObjs);
}
}
public override async Task<bool> DeleteAsync(Expression<Func<T, bool>> whereExpression)
{
if (typeof(ISoftDelete).IsAssignableFrom(typeof(T)))
{
var entities = await GetListAsync(whereExpression);
//反射赋值
entities.ForEach(e => ReflexHelper.SetModelValue(nameof(ISoftDelete.IsDeleted), true, e));
return await UpdateRangeAsync(entities);
}
else
{
return await base.DeleteAsync(whereExpression);
}
}
public override async Task<bool> DeleteByIdAsync(dynamic id)
{
if (typeof(ISoftDelete).IsAssignableFrom(typeof(T)))
{
var entity = await GetByIdAsync(id);
//反射赋值
ReflexHelper.SetModelValue(nameof(ISoftDelete.IsDeleted), true, entity);
return await UpdateAsync(entity);
}
else
{
return await _Db.Deleteable<T>().In(id).ExecuteCommandAsync() > 0;
}
}
public override async Task<bool> DeleteByIdsAsync(dynamic[] ids)
{
if (typeof(ISoftDelete).IsAssignableFrom(typeof(T)))
{
var entities = await _DbQueryable.In(ids).ToListAsync();
if (entities.Count == 0)
{
return false;
}
//反射赋值
entities.ForEach(e => ReflexHelper.SetModelValue(nameof(ISoftDelete.IsDeleted), true, e));
return await UpdateRangeAsync(entities);
}
else
{
return await base.DeleteByIdsAsync(ids);
}
}
}
}

View File

@@ -0,0 +1,51 @@
using Furion;
using Furion.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using Yi.Framework.Infrastructure.Ddd.Repositories;
using Yi.Framework.Infrastructure.Uow;
namespace Yi.Framework.Infrastructure.Sqlsugar.Uow
{
public class SqlsugarUnitOfWork : IUnitOfWork, ISingleton
{
public ISqlSugarClient Db { get; set; }
public ITenant Tenant { get; set; }
public bool IsTran { get; set; }
public bool IsCommit { get; set; }
public bool IsClose { get; set; }
public void Dispose()
{
if (IsTran && IsCommit == false)
{
Tenant.RollbackTran();
}
if (Db.Ado.Transaction == null && IsClose == false)
{
Db.Close();
}
}
public bool Commit()
{
if (IsTran && IsCommit == false)
{
Tenant.CommitTran();
IsCommit = true;
}
if (Db.Ado.Transaction == null && IsClose == false)
{
Db.Close();
IsClose = true;
}
return IsCommit;
}
public IRepository<T> GetRepository<T>()
{
return App.GetRequiredService<IRepository<T>>();
}
}
}

View File

@@ -0,0 +1,38 @@
using Furion.DependencyInjection;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Infrastructure.Uow;
namespace Yi.Framework.Infrastructure.Sqlsugar.Uow
{
/// <summary>
/// 此部分为sqlsugr的魔改版本
/// </summary>
internal class SqlsugarUnitOfWorkManager : IUnitOfWorkManager,ISingleton
{
public SqlsugarUnitOfWorkManager(ISqlSugarClient db)
{
Db = db;
}
public ISqlSugarClient Db { get; set; }
public IUnitOfWork CreateContext(bool isTran = true)
{
SqlsugarUnitOfWork uow = new SqlsugarUnitOfWork();
return CreateContext(isTran, uow);
}
private IUnitOfWork CreateContext(bool isTran, SqlsugarUnitOfWork sugarUnitOf)
{
sugarUnitOf.Db = Db;
sugarUnitOf.Tenant = Db.AsTenant();
sugarUnitOf.IsTran = isTran;
Db.Open();
if (isTran)
Db.AsTenant().BeginTran();
return sugarUnitOf;
}
}
}

View File

@@ -0,0 +1,26 @@
using Yi.Framework.Infrastructure.Ddd.Repositories;
namespace Yi.Framework.Infrastructure.Uow
{
internal class DefaultUnitOfWork : IUnitOfWork
{
public DefaultUnitOfWork() { }
public bool IsTran { get; set; }
public bool IsCommit { get; set; }
public bool IsClose { get; set; }
public bool Commit()
{
return true;
}
public void Dispose()
{
}
public IRepository<T> GetRepository<T>()
{
throw new NotImplementedException();
}
}
}

View File

@@ -0,0 +1,10 @@
namespace Yi.Framework.Infrastructure.Uow
{
internal class DefaultUnitOfWorkManager : IUnitOfWorkManager
{
public IUnitOfWork CreateContext(bool isTran = true)
{
return new DefaultUnitOfWork();
}
}
}

View File

@@ -0,0 +1,14 @@
using Yi.Framework.Infrastructure.Ddd.Repositories;
namespace Yi.Framework.Infrastructure.Uow
{
public interface IUnitOfWork : IDisposable
{
bool IsTran { get; set; }
bool IsCommit { get; set; }
bool IsClose { get; set; }
IRepository<T> GetRepository<T>();
bool Commit();
}
}

View File

@@ -0,0 +1,8 @@
namespace Yi.Framework.Infrastructure.Uow
{
public interface IUnitOfWorkManager
{
IUnitOfWork CreateContext(bool isTran = true);
}
}

View File

@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
@@ -6,8 +6,14 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Yi.Furion.Rbac.Core\Yi.Furion.Rbac.Core.csproj" />
</ItemGroup> <PackageReference Include="Furion.Extras.Authentication.JwtBearer" Version="4.8.7.41" />
<PackageReference Include="Furion.Extras.ObjectMapper.Mapster" Version="4.8.7.41" />
<PackageReference Include="Furion.Pure" Version="4.8.7.45" />
<PackageReference Include="SqlSugarCore" Version="5.1.4.67-preview06" />
<PackageReference Include="IPTools.China" Version="1.6.0" />
</ItemGroup>
</Project> </Project>

View File

@@ -0,0 +1,26 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Text;
namespace Yi.Framework.Module.ImageSharp.HeiCaptcha
{
public static class HeiCaptchaExtension
{
/// <summary>
/// 启用HeiCaptcha
/// </summary>
/// <param name="services"></param>
/// <returns></returns>
public static IServiceCollection AddHeiCaptcha(this IServiceCollection services)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
services.AddScoped<SecurityCodeHelper>();
return services;
}
}
}

View File

@@ -0,0 +1,32 @@
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Gif;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.PixelFormats;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace Yi.Framework.Module.ImageSharp.HeiCaptcha
{
public static class ImageRgba32Extension
{
public static byte[] ToPngArray<TPixel>(this Image<TPixel> img) where TPixel : unmanaged, IPixel<TPixel>
{
using (var ms = new MemoryStream())
{
img.Save(ms, PngFormat.Instance);
return ms.ToArray();
}
}
public static byte[] ToGifArray<TPixel>(this Image<TPixel> img) where TPixel : unmanaged, IPixel<TPixel>
{
using (var ms = new MemoryStream())
{
img.Save(ms, new GifEncoder());
return ms.ToArray();
}
}
}
}

View File

@@ -0,0 +1,216 @@
//using SixLabors.Fonts;
//using SixLabors.ImageSharp;
//using SixLabors.ImageSharp.PixelFormats;
//using SixLabors.ImageSharp.Processing;
//using System;
//using System.Collections.Generic;
//namespace Hei.Captcha
//{
// public static class ImageSharpExtension
// {
// / <summary>
// / 绘制中文字符(可以绘制字母数字,但样式可能需要改)
// / </summary>
// / <typeparam name = "TPixel" ></ typeparam >
// / < param name="processingContext"></param>
// / <param name = "containerWidth" ></ param >
// / < param name="containerHeight"></param>
// / <param name = "text" ></ param >
// / < param name="color"></param>
// / <param name = "font" ></ param >
// / < returns ></ returns >
// public static IImageProcessingContext<TPixel> DrawingCnText<TPixel>(this IImageProcessingContext<TPixel> processingContext, int containerWidth, int containerHeight, string text, Rgba32 color, Font font)
// where TPixel : struct, IPixel<TPixel>
// {
// return processingContext.Apply(img =>
// {
// if (string.IsNullOrEmpty(text) == false)
// {
// Random random = new Random();
// var textWidth = (img.Width / text.Length);
// var img2Size = Math.Min(textWidth, img.Height);
// var fontMiniSize = (int)(img2Size * 0.6);
// var fontMaxSize = (int)(img2Size * 0.95);
// for (int i = 0; i < text.Length; i++)
// {
// using (Image<Rgba32> img2 = new Image<Rgba32>(img2Size, img2Size))
// {
// Font scaledFont = new Font(font, random.Next(fontMiniSize, fontMaxSize));
// var point = new Point(i * textWidth, (containerHeight - img2.Height) / 2);
// var textGraphicsOptions = new TextGraphicsOptions(true)
// {
// HorizontalAlignment = HorizontalAlignment.Left,
// VerticalAlignment = VerticalAlignment.Top
// };
// img2.Mutate(ctx => ctx
// .DrawText(textGraphicsOptions, text[i].ToString(), scaledFont, color, new Point(0, 0))
// .Rotate(random.Next(-45, 45))
// );
// img.Mutate(ctx => ctx.DrawImage(img2, point, 1));
// }
// }
// }
// });
// }
// public static IImageProcessingContext<TPixel> DrawingEnText<TPixel>(this IImageProcessingContext<TPixel> processingContext, int containerWidth, int containerHeight, string text, string[] colorHexArr, Font[] fonts)
// where TPixel : struct, IPixel<TPixel>
// {
// return processingContext.Apply(img =>
// {
// if (string.IsNullOrEmpty(text) == false)
// {
// Random random = new Random();
// var textWidth = (img.Width / text.Length);
// var img2Size = Math.Min(textWidth, img.Height);
// var fontMiniSize = (int)(img2Size * 0.9);
// var fontMaxSize = (int)(img2Size * 1.37);
// Array fontStyleArr = Enum.GetValues(typeof(FontStyle));
// for (int i = 0; i < text.Length; i++)
// {
// using (Image<Rgba32> img2 = new Image<Rgba32>(img2Size, img2Size))
// {
// Font scaledFont = new Font(fonts[random.Next(0, fonts.Length)], random.Next(fontMiniSize, fontMaxSize), (FontStyle)fontStyleArr.GetValue(random.Next(fontStyleArr.Length)));
// var point = new Point(i * textWidth, (containerHeight - img2.Height) / 2);
// var colorHex = colorHexArr[random.Next(0, colorHexArr.Length)];
// var textGraphicsOptions = new TextGraphicsOptions(true)
// {
// HorizontalAlignment = HorizontalAlignment.Left,
// VerticalAlignment = VerticalAlignment.Top
// };
// img2.Mutate(ctx => ctx
// .DrawText(textGraphicsOptions, text[i].ToString(), scaledFont, Rgba32.FromHex(colorHex), new Point(0, 0))
// .DrawingGrid(containerWidth, containerHeight, Rgba32.FromHex(colorHex), 6, 1)
// .Rotate(random.Next(-45, 45))
// );
// img.Mutate(ctx => ctx.DrawImage(img2, point, 1));
// }
// }
// }
// });
// }
// / <summary>
// / 画圆圈(泡泡)
// / </summary>
// / <typeparam name = "TPixel" ></ typeparam >
// / < param name="processingContext"></param>
// / <param name = "containerWidth" ></ param >
// / < param name="containerHeight"></param>
// / <param name = "count" ></ param >
// / < param name="miniR"></param>
// / <param name = "maxR" ></ param >
// / < param name="color"></param>
// / <param name = "canOverlap" ></ param >
// / < returns ></ returns >
// public static IImageProcessingContext<TPixel> DrawingCircles<TPixel>(this IImageProcessingContext<TPixel> processingContext, int containerWidth, int containerHeight, int count, int miniR, int maxR, TPixel color, bool canOverlap = false)
// where TPixel : struct, IPixel<TPixel>
// {
// return processingContext.Apply(img =>
// {
// EllipsePolygon ep = null;
// Random random = new Random();
// PointF tempPoint = new PointF();
// List<PointF> points = new List<PointF>();
// if (count > 0)
// {
// for (int i = 0; i < count; i++)
// {
// if (canOverlap)
// {
// tempPoint = new PointF(random.Next(0, containerWidth), random.Next(0, containerHeight));
// }
// else
// {
// tempPoint = getCirclePoginF(containerWidth, containerHeight, (miniR + maxR), ref points);
// }
// ep = new EllipsePolygon(tempPoint, random.Next(miniR, maxR));
// img.Mutate(ctx => ctx
// .Draw(color, (float)(random.Next(94, 145) / 100.0), ep.Clip())
// );
// }
// }
// });
// }
// / <summary>
// / 画杂线
// / </summary>
// / <typeparam name = "TPixel" ></ typeparam >
// / < param name="processingContext"></param>
// / <param name = "containerWidth" ></ param >
// / < param name="containerHeight"></param>
// / <param name = "color" ></ param >
// / < param name="count"></param>
// / <param name = "thickness" ></ param >
// / < returns ></ returns >
// public static IImageProcessingContext<TPixel> DrawingGrid<TPixel>(this IImageProcessingContext<TPixel> processingContext, int containerWidth, int containerHeight, TPixel color, int count, float thickness)
// where TPixel : struct, IPixel<TPixel>
// {
// return processingContext.Apply(img =>
// {
// var points = new List<PointF> { new PointF(0, 0) };
// for (int i = 0; i < count; i++)
// {
// getCirclePoginF(containerWidth, containerHeight, 9, ref points);
// }
// points.Add(new PointF(containerWidth, containerHeight));
// img.Mutate(ctx => ctx
// .DrawLines(color, thickness, points.ToArray())
// );
// });
// }
// / <summary>
// / 散 随机点
// / </summary>
// / <param name = "containerWidth" ></ param >
// / < param name="containerHeight"></param>
// / <param name = "lapR" ></ param >
// / < param name="list"></param>
// / <returns></returns>
// private static PointF getCirclePoginF(int containerWidth, int containerHeight, double lapR, ref List<PointF> list)
// {
// Random random = new Random();
// PointF newPoint = new PointF();
// int retryTimes = 10;
// double tempDistance = 0;
// do
// {
// newPoint.X = random.Next(0, containerWidth);
// newPoint.Y = random.Next(0, containerHeight);
// bool tooClose = false;
// foreach (var p in list)
// {
// tooClose = false;
// tempDistance = Math.Sqrt((Math.Pow((p.X - newPoint.X), 2) + Math.Pow((p.Y - newPoint.Y), 2)));
// if (tempDistance < lapR)
// {
// tooClose = true;
// break;
// }
// }
// if (tooClose == false)
// {
// list.Add(newPoint);
// break;
// }
// }
// while (retryTimes-- > 0);
// if (retryTimes <= 0)
// {
// list.Add(newPoint);
// }
// return newPoint;
// }
// }
//}

View File

@@ -0,0 +1,182 @@
using SixLabors.Fonts;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.Formats.Gif;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
namespace Yi.Framework.Module.ImageSharp.HeiCaptcha
{
/// <summary>
/// 验证码配置和绘制逻辑
/// </summary>
public class SecurityCodeHelper
{
/// <summary>
/// 验证码文本池
/// </summary>
private static readonly string[] _cnTextArr = new string[] { "的", "一", "国", "在", "人", "了", "有", "中", "是", "年", "和", "大", "业", "不", "为", "发", "会", "工", "经", "上", "地", "市", "要", "个", "产", "这", "出", "行", "作", "生", "家", "以", "成", "到", "日", "民", "来", "我", "部", "对", "进", "多", "全", "建", "他", "公", "开", "们", "场", "展", "时", "理", "新", "方", "主", "企", "资", "实", "学", "报", "制", "政", "济", "用", "同", "于", "法", "高", "长", "现", "本", "月", "定", "化", "加", "动", "合", "品", "重", "关", "机", "分", "力", "自", "外", "者", "区", "能", "设", "后", "就", "等", "体", "下", "万", "元", "社", "过", "前", "面", "农", "也", "得", "与", "说", "之", "员", "而", "务", "利", "电", "文", "事", "可", "种", "总", "改", "三", "各", "好", "金", "第", "司", "其", "从", "平", "代", "当", "天", "水", "省", "提", "商", "十", "管", "内", "小", "技", "位", "目", "起", "海", "所", "立", "已", "通", "入", "量", "子", "问", "度", "北", "保", "心", "还", "科", "委", "都", "术", "使", "明", "着", "次", "将", "增", "基", "名", "向", "门", "应", "里", "美", "由", "规", "今", "题", "记", "点", "计", "去", "强", "两", "些", "表", "系", "办", "教 正", "条", "最", "达", "特", "革", "收", "二", "期", "并", "程", "厂", "如", "道", "际 及", "西", "口", "京", "华", "任", "调", "性", "导", "组", "东", "路", "活", "广", "意", "比", "投", "决", "交", "统", "党", "南", "安", "此", "领", "结", "营", "项", "情", "解", "议", "义", "山", "先", "车", "然", "价", "放", "世", "间", "因", "共", "院", "步", "物", "界", "集", "把", "持", "无", "但", "城", "相", "书", "村", "求", "治", "取", "原", "处", "府", "研", "质", "信", "四", "运", "县", "军", "件", "育", "局", "干", "队", "团", "又", "造", "形", "级", "标", "联", "专", "少", "费", "效", "据", "手", "施", "权", "江", "近", "深", "更", "认", "果", "格", "几", "看", "没", "职", "服", "台", "式", "益", "想", "数", "单", "样", "只", "被", "亿", "老", "受", "优", "常", "销", "志", "战", "流", "很", "接", "乡", "头", "给", "至", "难", "观", "指", "创", "证", "织", "论", "别", "五", "协", "变", "风", "批", "见", "究", "支", "那", "查", "张", "精", "每", "林", "转", "划", "准", "做", "需", "传", "争", "税", "构", "具", "百", "或", "才", "积", "势", "举", "必", "型", "易", "视", "快", "李", "参", "回", "引", "镇", "首", "推", "思", "完", "消", "值", "该", "走", "装", "众", "责", "备", "州", "供", "包", "副", "极", "整", "确", "知", "贸", "己", "环", "话", "反", "身", "选", "亚", "么", "带", "采", "王", "策", "真", "女", "谈", "严", "斯", "况", "色", "打", "德", "告", "仅", "它", "气", "料", "神", "率", "识", "劳", "境", "源", "青", "护", "列", "兴", "许", "户", "马", "港", "则", "节", "款", "拉", "直", "案", "股", "光", "较", "河", "花", "根", "布", "线", "土", "克", "再", "群", "医", "清", "速", "律", "她", "族", "历", "非", "感", "占", "续", "师", "何", "影", "功", "负", "验", "望", "财", "类", "货", "约", "艺", "售", "连", "纪", "按", "讯", "史", "示", "象", "养", "获", "石", "食", "抓", "富", "模", "始", "住", "赛", "客", "越", "闻", "央", "席", "坚" };
private static readonly string[] _enTextArr = new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "k", "m", "n", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
/// <summary>
/// 验证码图片宽高
/// </summary>
private readonly int _imageWidth = 120;
private readonly int _imageHeight = 50;
/// <summary>
/// 泡泡数量
/// </summary>
private int _circleCount = 14;
/// <summary>
/// 泡泡半径范围
/// </summary>
private readonly int _miniCircleR = 2;
private readonly int _maxCircleR = 8;
/// <summary>
/// 颜色池,较深的颜色
/// https://tool.oschina.net/commons?type=3
/// </summary>
private static readonly string[] _colorHexArr = new string[] { "#00E5EE", "#000000", "#2F4F4F", "#000000", "#43CD80", "#191970", "#006400", "#458B00", "#8B7765", "#CD5B45" };
///较浅的颜色
private static readonly string[] _lightColorHexArr = new string[] { "#FFFACD", "#FDF5E6", "#F0FFFF", "#BBFFFF", "#FAFAD2", "#FFE4E1", "#DCDCDC", "#F0E68C" };
private static readonly Random _random = new Random();
/// <summary>
/// 字体池
/// </summary>
private static Font[] _fontArr;
public SecurityCodeHelper()
{
initFonts(_imageHeight);
}
/// <summary>
/// 生成随机中文字符串
/// </summary>
/// <param name="length"></param>
/// <returns></returns>
public string GetRandomCnText(int length)
{
StringBuilder sb = new StringBuilder();
if (length > 0)
{
do
{
sb.Append(_cnTextArr[_random.Next(0, _cnTextArr.Length)]);
}
while (--length > 0);
}
return sb.ToString();
}
/// <summary>
/// 生成随机英文字母/数字组合字符串
/// </summary>
/// <param name="length"></param>
/// <returns></returns>
public string GetRandomEnDigitalText(int length)
{
StringBuilder sb = new StringBuilder();
if (length > 0)
{
do
{
if (_random.Next(0, 2) > 0)
{
sb.Append(_random.Next(2, 10));
}
else
{
sb.Append(_enTextArr[_random.Next(0, _enTextArr.Length)]);
}
}
while (--length > 0);
}
return sb.ToString();
}
/// <summary>
/// 英文字母+数字组合验证码
/// </summary>
/// <param name="text"></param>
/// <returns>验证码图片字节数组</returns>
public byte[] GetEnDigitalCodeByte(string text)
{
using (Image<Rgba32> img = getEnDigitalCodeImage(text))
{
return img.ToGifArray();
}
}
/// <summary>
/// 生成一个数组组合验证码素材Image
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
private Image<Rgba32> getEnDigitalCodeImage(string text)
{
Image<Rgba32> img = new Image<Rgba32>(_imageWidth, _imageHeight);
var colorTextHex = _colorHexArr[_random.Next(0, _colorHexArr.Length)];
var lignthColorHex = _lightColorHexArr[_random.Next(0, _lightColorHexArr.Length)];
img.Mutate(ctx => ctx
.Fill(Rgba32.ParseHex(_lightColorHexArr[_random.Next(0, _lightColorHexArr.Length)]))
.Glow(Rgba32.ParseHex(lignthColorHex))
//.DrawingGrid(_imageWidth, _imageHeight, Rgba32.ParseHex(lignthColorHex), 8, 1)
.DrawText(text, _fontArr[0], Rgba32.ParseHex(_colorHexArr[0]), new PointF(0, 0))
//.DrawingEnText(, text, _colorHexArr, _fontArr)
.GaussianBlur(0.4f)
//.DrawingCircles(_imageWidth, _imageHeight, 15, _miniCircleR, _maxCircleR, Color.White)
);
return img;
}
/// <summary>
/// 初始化字体池
/// </summary>
/// <param name="fontSize">一个初始大小</param>
private void initFonts(int fontSize)
{
if (_fontArr == null)
{
var assembly = Assembly.GetExecutingAssembly();
var names = assembly.GetManifestResourceNames();
if (names?.Length > 0 == true)
{
var fontList = new List<Font>();
var fontCollection = new FontCollection();
foreach (var name in names)
{
fontList.Add(new Font(fontCollection.Add(assembly.GetManifestResourceStream(name)), fontSize));
}
_fontArr = fontList.ToArray();
}
else
{
throw new Exception($"绘制验证码字体文件加载失败");
}
}
}
}
}

View File

@@ -0,0 +1,65 @@
using Furion.DependencyInjection;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.Processing;
namespace Yi.Framework.Module.ImageSharp;
public class ImageSharpManager:ISingleton
{
public void ImageCompress(string fileName, Stream stream, string savePath)
{
var extensionName = Path.GetExtension(fileName).ToLower();
if (extensionName == ".png")
{
PngImageCompress(stream, savePath);
}
else if (extensionName == ".jpg" || extensionName == ".jpeg")
{
JpgImageCompress(stream, savePath);
}
else
{
using (var fileStream = new FileStream(savePath, FileMode.Create, FileAccess.Write))
{
stream.CopyTo(fileStream);
}
}
}
public void PngImageCompress(Stream stream, string savePath)
{
using (var image = Image.Load(stream))
{
var encoder = new PngEncoder()
{
CompressionLevel = PngCompressionLevel.Level6,
};
if (image.Width > 300)
{
image.Mutate(a => a.Resize(image.Width / 2, image.Height / 2));
}
image.Save(savePath, encoder);
}
}
public void JpgImageCompress(Stream stream, string savePath)
{
using (var image = Image.Load(stream))
{
var encoder = new JpegEncoder()
{
Quality = 30
};
if (image.Width > 300)
{
image.Mutate(a => a.Resize(image.Width / 2, image.Height / 2));
}
image.Save(savePath, encoder);
}
}
}

View File

@@ -0,0 +1,93 @@
using IPTools.Core;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;
using Yi.Framework.Infrastructure.AspNetCore;
using Yi.Framework.Infrastructure.CurrentUsers;
using Yi.Framework.Infrastructure.Ddd.Repositories;
using Yi.Framework.Infrastructure.Helper;
namespace Yi.Framework.Module.OperLogManager
{
public class GlobalOperLogAttribute : ActionFilterAttribute
{
private ILogger<GlobalOperLogAttribute> _logger;
private IRepository<OperationLogEntity> _repository;
private ICurrentUser _currentUser;
//注入一个日志服务
public GlobalOperLogAttribute(ILogger<GlobalOperLogAttribute> logger, IRepository<OperationLogEntity> repository, ICurrentUser currentUser)
{
_logger = logger;
_repository = repository;
_currentUser = currentUser;
}
public override async void OnResultExecuted(ResultExecutedContext context)
{
//判断标签是在方法上
if (context.ActionDescriptor is not ControllerActionDescriptor controllerActionDescriptor) return;
//查找标签,获取标签对象
OperLogAttribute? operLogAttribute = controllerActionDescriptor.MethodInfo.GetCustomAttributes(inherit: true)
.FirstOrDefault(a => a.GetType().Equals(typeof(OperLogAttribute))) as OperLogAttribute;
//空对象直接返回
if (operLogAttribute is null) return;
////获取控制器名
//string controller = context.RouteData.Values["Controller"].ToString();
////获取方法名
//string action = context.RouteData.Values["Action"].ToString();
//获取Ip
string ip = context.HttpContext.GetClientIp();
//根据ip获取地址
var ipTool = IpTool.Search(ip);
string location = ipTool.Province + " " + ipTool.City;
//日志服务插入一条操作记录即可
var logEntity = new OperationLogEntity();
logEntity.Id = SnowflakeHelper.NextId;
logEntity.OperIp = ip;
//logEntity.OperLocation = location;
logEntity.OperType = operLogAttribute.OperType;
logEntity.Title = operLogAttribute.Title;
logEntity.RequestMethod = context.HttpContext.Request.Method;
logEntity.Method = context.HttpContext.Request.Path.Value;
logEntity.OperLocation = location;
logEntity.OperUser = _currentUser.UserName;
if (operLogAttribute.IsSaveResponseData)
{
if (context.Result is ContentResult result && result.ContentType == "application/json")
{
logEntity.RequestResult = result.Content?.Replace("\r\n", "").Trim();
}
if (context.Result is JsonResult result2)
{
logEntity.RequestResult = result2.Value?.ToString();
}
if (context.Result is ObjectResult result3)
{
logEntity.RequestResult = JsonHelper.ObjToStr(result3.Value);
}
}
if (operLogAttribute.IsSaveRequestData)
{
//logEntity.RequestParam = context.HttpContext.GetRequestValue(logEntity.RequestMethod);
}
await _repository.InsertAsync(logEntity);
}
}
}

View File

@@ -0,0 +1,12 @@
using Yi.Framework.Infrastructure.Ddd.Services.Abstract;
namespace Yi.Framework.Module.OperLogManager
{
/// <summary>
/// OperationLog服务抽象
/// </summary>
public interface IOperationLogService : ICrudAppService<OperationLogGetListOutputDto, long, OperationLogGetListInputVo>
{
}
}

Some files were not shown because too many files have changed in this diff Show More