Просмотр исходного кода

!1019 后端优化
👍Merge pull request !1019 from Hans/hans_0405

zuohuaijun 2 лет назад
Родитель
Сommit
3f37b9b164

+ 62 - 0
Admin.NET/Admin.NET.Core/Enum/HttpMethodEnum.cs

@@ -0,0 +1,62 @@
+// 大名科技(天津)有限公司 版权所有
+//
+// 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
+//
+// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动
+//
+// 任何基于本项目二次开发而产生的一切法律纠纷和责任,均与作者无关
+
+namespace Admin.NET.Core;
+
+/// <summary>
+/// HTTP请求方法
+/// </summary>
+[Description("HTTP请求方法")]
+public enum HttpMethodEnum
+{
+    /// <summary>
+    ///  HTTP "CONNECT" method.
+    /// </summary>
+    [Description("HTTP \"CONNECT\" method.")]
+    Connect,
+    /// <summary>
+    /// HTTP "DELETE" method.
+    /// </summary>
+    [Description("HTTP \"DELETE\" method.")]
+    Delete,
+    /// <summary>
+    ///  HTTP "GET" method.
+    /// </summary>
+    [Description("HTTP \"GET\" method.")]
+    Get,
+    /// <summary>
+    /// HTTP "HEAD" method.
+    /// </summary>
+    [Description("HTTP \"HEAD\" method.")]
+    Head,
+    /// <summary>
+    /// HTTP "OPTIONS" method.
+    /// </summary>
+    [Description("HTTP \"OPTIONS\" method.")]
+    Options,
+    /// <summary>
+    /// HTTP "PATCH" method. 
+    /// </summary>    
+    [Description("HTTP \"PATCH\" method. ")]
+    Patch,
+    /// <summary>
+    ///  HTTP "POST" method.
+    /// </summary>    
+    [Description("HTTP \"POST\" method.")]
+    Post,
+    /// <summary>
+    /// HTTP "PUT" method.
+    /// </summary>    
+    [Description(" HTTP \"PUT\" method.")]
+    Put,
+    /// <summary>
+    /// HTTP "TRACE" method.
+    /// </summary>
+    [Description(" HTTP \"TRACE\" method.")]
+    Trace
+}

+ 32 - 0
Admin.NET/Admin.NET.Core/Service/OpenAccess/SysOpenAccessService.cs

@@ -7,6 +7,7 @@
 // 任何基于本项目二次开发而产生的一切法律纠纷和责任,均与作者无关
 
 using System.Security.Claims;
+using System.Security.Cryptography;
 
 namespace Admin.NET.Core.Service;
 
@@ -29,6 +30,37 @@ public class SysOpenAccessService : IDynamicApiController, ITransient
         _sysCacheService = sysCacheService;
     }
 
+    /// <summary>
+    /// 获取生成的签名
+    /// </summary>
+    /// <param name="appSecret">密钥</param>
+    /// <param name="accessKey">身份标识</param>
+    /// <param name="method">请求方法</param>
+    /// <param name="url">请求接口地址</param>
+    /// <returns></returns>
+    [HttpGet]
+    [DisplayName("获取生成的签名")]
+    public string GetGenerateSignature([FromQuery] string appSecret, string accessKey, HttpMethodEnum method, string url)
+    {
+        // 密钥
+        var appSecretByte = Encoding.UTF8.GetBytes(appSecret);
+        // 时间戳,精确到秒
+        DateTimeOffset currentTime = DateTimeOffset.UtcNow;
+        var timestamp = currentTime.ToUnixTimeSeconds();
+        // 唯一随机数,可以使用guid或者雪花id,以下是sqlsugar提供获取雪花id的方法
+        var nonce = YitIdHelper.NextId();
+        // 请求方式
+        var sMethod = method.ToString();
+        // 拼接参数
+        var parameter = $"{method}&{url}&{accessKey}&{timestamp}&{nonce}";
+        //使用 HMAC-SHA256 协议创建基于哈希的消息身份验证代码 (HMAC),以appSecretByte 作为密钥,对上面拼接的参数进行计算签名,所得签名进行 Base-64 编码
+        using HMAC hmac = new HMACSHA256();
+        hmac.Key = appSecretByte;
+        var sign = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(parameter)));
+        return sign;
+    }
+
+
     /// <summary>
     /// 获取开放接口身份分页列表 🔖
     /// </summary>

+ 79 - 0
Admin.NET/Admin.NET.Core/SqlSugar/ISqlSugarRepository.cs

@@ -0,0 +1,79 @@
+// 大名科技(天津)有限公司 版权所有
+//
+// 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
+//
+// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动
+//
+// 任何基于本项目二次开发而产生的一切法律纠纷和责任,均与作者无关
+
+namespace Admin.NET.Core;
+
+public interface ISqlSugarRepository<T> : ISugarRepository, ISimpleClient<T> where T : class, new()
+{
+    /// <summary>
+    /// 分表批量创建数据
+    /// </summary>
+    /// <param name="input">数据</param>
+    /// <returns></returns>
+    Task<bool> SplitTableInsertAsync(List<T> input);
+    /// <summary>
+    /// 分表创建数据
+    /// </summary>
+    /// <param name="input">数据</param>
+    /// <returns></returns>
+    Task<bool> SplitTableInsertAsync(T input);
+    /// <summary>
+    /// 分表批量删除数据
+    /// </summary>
+    /// <param name="input">数据</param>
+    /// <returns></returns>
+    Task<bool> SplitTableDeleteableAsync(List<T> input);
+    /// <summary>
+    /// 分表删除数据
+    /// </summary>
+    /// <param name="input">数据</param>
+    /// <returns></returns>
+    Task<bool> SplitTableDeleteableAsync(T input);
+    /// <summary>
+    /// 分表批量更新数据
+    /// </summary>
+    /// <param name="input">数据</param>
+    /// <returns></returns>
+    Task<bool> SplitTableUpdateAsync(List<T> input);
+    /// <summary>
+    /// 分表更新数据
+    /// </summary>
+    /// <param name="input">数据</param>
+    /// <returns></returns>
+    Task<bool> SplitTableUpdateAsync(T input);
+    /// <summary>
+    /// 分表判断是否存在
+    /// </summary>
+    /// <param name="whereExpression"></param>
+    /// <returns></returns>
+    Task<bool> SplitTableIsAnyAsync(Expression<Func<T, bool>> whereExpression);
+    /// <summary>
+    /// 分表获取列表
+    /// </summary>
+    /// <param name="whereExpression">条件</param>
+    /// <returns></returns>
+    Task<List<T>> SplitTableGetListAsync(Expression<Func<T, bool>> whereExpression);
+    /// <summary>
+    /// 分表获取列表
+    /// </summary>
+    /// <returns></returns>
+    Task<List<T>> SplitTableGetListAsync();
+    /// <summary>
+    /// 分表GetFirstAsyn
+    /// </summary>
+    /// <param name="whereExpression"></param>
+    /// <returns></returns>
+    Task<T> SplitTableGetFirstAsync(Expression<Func<T, bool>> whereExpression);
+    /// <summary>
+    /// 分表获取列表
+    /// </summary>
+    /// <param name="whereExpression">条件</param>
+    /// <param name="tableNames">分表表名</param>
+    /// <returns></returns>
+    Task<List<T>> SplitTableGetListAsync(Expression<Func<T, bool>> whereExpression, string[] tableNames);
+}

+ 48 - 2
Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarRepository.cs

@@ -12,7 +12,7 @@ namespace Admin.NET.Core;
 /// SqlSugar 实体仓储
 /// </summary>
 /// <typeparam name="T"></typeparam>
-public class SqlSugarRepository<T> : SimpleClient<T> where T : class, new()
+public class SqlSugarRepository<T> : SimpleClient<T>, ISqlSugarRepository<T> where T : class, new()
 {
     public SqlSugarRepository()
     {
@@ -47,4 +47,50 @@ public class SqlSugarRepository<T> : SimpleClient<T> where T : class, new()
         if (sqlSugarScopeProviderTenant == null) return;
         base.Context = sqlSugarScopeProviderTenant;
     }
-}
+    #region 分表操作
+    public async Task<bool> SplitTableInsertAsync(T input)
+    {
+        return await base.AsInsertable(input).SplitTable().ExecuteCommandAsync() > 0;
+    }
+    public async Task<bool> SplitTableInsertAsync(List<T> input)
+    {
+        return await base.AsInsertable(input).SplitTable().ExecuteCommandAsync() > 0;
+    }
+    public async Task<bool> SplitTableUpdateAsync(T input)
+    {
+        return await base.AsUpdateable(input).SplitTable().ExecuteCommandAsync() > 0;
+    }
+    public async Task<bool> SplitTableUpdateAsync(List<T> input)
+    {
+        return await base.AsUpdateable(input).SplitTable().ExecuteCommandAsync() > 0;
+    }
+    public async Task<bool> SplitTableDeleteableAsync(T input)
+    {
+        return await base.Context.Deleteable(input).SplitTable().ExecuteCommandAsync() > 0;
+    }
+    public async Task<bool> SplitTableDeleteableAsync(List<T> input)
+    {
+        return await base.Context.Deleteable(input).SplitTable().ExecuteCommandAsync() > 0;
+    }
+    public Task<T> SplitTableGetFirstAsync(Expression<Func<T, bool>> whereExpression)
+    {
+        return base.AsQueryable().SplitTable().FirstAsync(whereExpression);
+    }
+    public Task<bool> SplitTableIsAnyAsync(Expression<Func<T, bool>> whereExpression)
+    {
+        return base.Context.Queryable<T>().Where(whereExpression).SplitTable().AnyAsync();
+    }
+    public Task<List<T>> SplitTableGetListAsync()
+    {
+        return Context.Queryable<T>().SplitTable().ToListAsync();
+    }
+    public Task<List<T>> SplitTableGetListAsync(Expression<Func<T, bool>> whereExpression)
+    {
+        return Context.Queryable<T>().Where(whereExpression).SplitTable().ToListAsync();
+    }
+    public Task<List<T>> SplitTableGetListAsync(Expression<Func<T, bool>> whereExpression, string[] tableNames)
+    {
+        return Context.Queryable<T>().Where(whereExpression).SplitTable(t => t.InTableNames(tableNames)).ToListAsync();
+    }
+    #endregion
+}