0
0
Pārlūkot izejas kodu

新增缓存管理,修复同步api异常

xiaoxue 5 gadi atpakaļ
vecāks
revīzija
b03a19319f

+ 7 - 2
Admin.Core.Common/Cache/CacheKey.cs

@@ -1,4 +1,6 @@
 
+using System.ComponentModel;
+
 namespace Admin.Core.Common.Cache
 {
     /// <summary>
@@ -9,16 +11,19 @@ namespace Admin.Core.Common.Cache
         /// <summary>
         /// 验证码 admin:verify:code:guid
         /// </summary>
+        [Description("验证码")]
         public const string VerifyCodeKey = "admin:verify:code:{0}";
 
         /// <summary>
-        /// 密码 admin:password:guid
+        /// 密码加密 admin:password:encrypt:guid
         /// </summary>
-        public const string PassWordKey = "admin:password:{0}";
+        [Description("密码加密")]
+        public const string PassWordEncryptKey = "admin:password:{0}";
 
         /// <summary>
         /// 用户权限 admin:user:用户主键:permissions
         /// </summary>
+        [Description("用户权限")]
         public const string UserPermissions = "admin:user:{0}:permissions";
     }
 }

+ 5 - 0
Admin.Core.Common/Configs/AppConfig.cs

@@ -16,6 +16,11 @@
         public string Urls { get; set; } = "http://*:8081";
 
         public AopConfig Aop { get; set; }
+
+        /// <summary>
+        /// 同步缓存键
+        /// </summary>
+        public bool SyncCacheKey { get; set; }
     }
 
     public class AopConfig

+ 0 - 2
Admin.Core.Common/Helpers/ConsoleHelper.cs

@@ -1,6 +1,4 @@
 using System;
-using System.Collections.Generic;
-using System.Text;
 
 namespace Admin.Core.Common.Helpers
 {

+ 6 - 6
Admin.Core.Common/Helpers/UtilConvert.cs

@@ -21,7 +21,7 @@ namespace Admin.Core
         
         public static int ToInt(this object thisValue, int errorValue)
         {
-            int reval = 0;
+            int reval;
             if (thisValue != null && thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out reval))
             {
                 return reval;
@@ -40,7 +40,7 @@ namespace Admin.Core
         
         public static double ToMoney(this object thisValue)
         {
-            double reval = 0;
+            double reval;
             if (thisValue != null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out reval))
             {
                 return reval;
@@ -50,7 +50,7 @@ namespace Admin.Core
         
         public static double ToMoney(this object thisValue, double errorValue)
         {
-            double reval = 0;
+            double reval;
             if (thisValue != null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out reval))
             {
                 return reval;
@@ -72,7 +72,7 @@ namespace Admin.Core
         
         public static decimal ToDecimal(this object thisValue)
         {
-            Decimal reval = 0;
+            decimal reval;
             if (thisValue != null && thisValue != DBNull.Value && decimal.TryParse(thisValue.ToString(), out reval))
             {
                 return reval;
@@ -82,7 +82,7 @@ namespace Admin.Core
         
         public static decimal ToDecimal(this object thisValue, decimal errorValue)
         {
-            Decimal reval = 0;
+            decimal reval;
             if (thisValue != null && thisValue != DBNull.Value && decimal.TryParse(thisValue.ToString(), out reval))
             {
                 return reval;
@@ -102,7 +102,7 @@ namespace Admin.Core
         
         public static DateTime ToDate(this object thisValue, DateTime errorValue)
         {
-            DateTime reval = DateTime.MinValue;
+            DateTime reval;
             if (thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out reval))
             {
                 return reval;

+ 1 - 1
Admin.Core.Model/Admin/ApiEntity.cs

@@ -14,7 +14,7 @@ namespace Admin.Core.Model.Admin
         /// <summary>
         /// 所属模块
         /// </summary>
-		public long? ParentId { get; set; }
+		public long ParentId { get; set; }
 
         /// <summary>
         /// 接口命名

+ 1 - 1
Admin.Core.Model/Admin/ViewEntity.cs

@@ -14,7 +14,7 @@ namespace Admin.Core.Model.Admin
         /// <summary>
         /// 所属节点
         /// </summary>
-		public long? ParentId { get; set; }
+		public long ParentId { get; set; }
 
         /// <summary>
         /// 视图命名

+ 6 - 0
Admin.Core.Repository/Base/IRepositoryBase.cs

@@ -6,6 +6,12 @@ namespace Admin.Core.Repository
 {
     public interface IRepositoryBase<TEntity, TKey> : IBaseRepository<TEntity, TKey> where TEntity : class
     {
+        /// <summary>
+        /// 获得Dto
+        /// </summary>
+        /// <typeparam name="TDto"></typeparam>
+        /// <param name="id"></param>
+        /// <returns></returns>
         Task<TDto> GetAsync<TDto>(TKey id);
 
         /// <summary>

+ 3 - 0
Admin.Core.Services/Admin.Core.Service.csproj

@@ -11,8 +11,11 @@
   </PropertyGroup>
 
   <ItemGroup>
+    <Compile Remove="Admin\Cache\Output\**" />
     <Compile Remove="BASE\**" />
+    <EmbeddedResource Remove="Admin\Cache\Output\**" />
     <EmbeddedResource Remove="BASE\**" />
+    <None Remove="Admin\Cache\Output\**" />
     <None Remove="BASE\**" />
   </ItemGroup>
 

+ 1 - 1
Admin.Core.Services/Admin/Api/ApiService.cs

@@ -183,7 +183,7 @@ namespace Admin.Core.Service.Admin.Api
                         label = labels != null && labels.Count > 0 ? labels[0] : string.Empty;
                         desc = labels != null && labels.Count > 1 ? string.Join("\r\n", labels.GetRange(1, labels.Count() - 1)) : string.Empty;
 
-                        a.ParentId = pa?.Id;
+                        a.ParentId = pa.Id;
                         a.Label = label;
                         a.Description = desc;
                         a.HttpMethods = api.HttpMethods;

+ 10 - 23
Admin.Core.Services/Admin/Auth/AuthService.cs

@@ -37,19 +37,6 @@ namespace Admin.Core.Service.Admin.Auth
 
         public async Task<IResponseOutput> LoginAsync(AuthLoginInput input)
         {
-            if (input.UserName.IsNull())
-            {
-                return ResponseOutput.NotOk("用户名不能为空!");
-            }
-            if (input.Password.IsNull())
-            {
-                return ResponseOutput.NotOk("密码不能为空!");
-            }
-            if (input.VerifyCode.IsNull())
-            {
-                return ResponseOutput.NotOk("验证码不能为空!");
-            }
-
             #region 验证码校验
             var verifyCodeKey = string.Format(CacheKey.VerifyCodeKey, input.VerifyCodeKey);
             var exists = await _cache.ExistsAsync(verifyCodeKey);
@@ -81,17 +68,17 @@ namespace Admin.Core.Service.Admin.Auth
             #region 解密
             if (input.PasswordKey.NotNull())
             {
-                var passwordKey = string.Format(CacheKey.PassWordKey, input.PasswordKey);
-                var existsPasswordKey = await _cache.ExistsAsync(passwordKey);
+                var passwordEncryptKey = string.Format(CacheKey.PassWordEncryptKey, input.PasswordKey);
+                var existsPasswordKey = await _cache.ExistsAsync(passwordEncryptKey);
                 if (existsPasswordKey)
                 {
-                    var secretKey = await _cache.GetAsync(passwordKey);
-                    if (passwordKey.IsNull())
+                    var secretKey = await _cache.GetAsync(passwordEncryptKey);
+                    if (passwordEncryptKey.IsNull())
                     {
                         return ResponseOutput.NotOk("解密失败!",1);
                     }
                     input.Password = DesEncrypt.Decrypt(input.Password, secretKey);
-                    await _cache.DelAsync(passwordKey);
+                    await _cache.DelAsync(passwordEncryptKey);
                 }
                 else
                 {
@@ -181,14 +168,14 @@ namespace Admin.Core.Service.Admin.Auth
             return ResponseOutput.Ok(data);
         }
 
-        public async Task<IResponseOutput> GetPassWordKeyAsync()
+        public async Task<IResponseOutput> GetPassWordEncryptKeyAsync()
         {
             //写入Redis
             var guid = Guid.NewGuid().ToString("N");
-            var key = string.Format(CacheKey.PassWordKey, guid);
-            var secretKey = StringHelper.GenerateRandom(8);
-            await _cache.SetAsync(key, secretKey, TimeSpan.FromMinutes(5));
-            var data = new { key = guid, secretKey };
+            var key = string.Format(CacheKey.PassWordEncryptKey, guid);
+            var encyptKey = StringHelper.GenerateRandom(8);
+            await _cache.SetAsync(key, encyptKey, TimeSpan.FromMinutes(5));
+            var data = new { key = guid, encyptKey };
 
             return ResponseOutput.Ok(data);
         }

+ 1 - 1
Admin.Core.Services/Admin/Auth/IAuthService.cs

@@ -16,6 +16,6 @@ namespace Admin.Core.Service.Admin.Auth
 
         Task<IResponseOutput> GetVerifyCodeAsync(string lastKey);
 
-        Task<IResponseOutput> GetPassWordKeyAsync();
+        Task<IResponseOutput> GetPassWordEncryptKeyAsync();
     }
 }

+ 6 - 1
Admin.Core.Services/Admin/Auth/Input/AuthLoginInput.cs

@@ -1,4 +1,6 @@
-namespace Admin.Core.Service.Admin.Auth.Input
+using System.ComponentModel.DataAnnotations;
+
+namespace Admin.Core.Service.Admin.Auth.Input
 {
     /// <summary>
     /// 登录信息
@@ -8,11 +10,13 @@
         /// <summary>
         /// 账号
         /// </summary>
+        [Required(ErrorMessage = "用户名不能为空!")]
         public string UserName { get; set; }
 
         /// <summary>
         /// 密码
         /// </summary>
+        [Required(ErrorMessage = "密码不能为空!")]
         public string Password { get; set; }
 
         /// <summary>
@@ -23,6 +27,7 @@
         /// <summary>
         /// 验证码
         /// </summary>
+        [Required(ErrorMessage = "验证码不能为空!")]
         public string VerifyCode { get; set; }
 
         /// <summary>

+ 52 - 0
Admin.Core.Services/Admin/Cache/CacheService.cs

@@ -0,0 +1,52 @@
+using Admin.Core.Common.Auth;
+using Admin.Core.Common.Cache;
+using Admin.Core.Model.Output;
+using Microsoft.Extensions.Logging;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.Linq;
+using System.Reflection;
+using System.Threading.Tasks;
+
+namespace Admin.Core.Service.Admin.Cache
+{
+    public class CacheService : ICacheService
+    {
+        private readonly ICache _cache;
+        private readonly IUser _user;
+        private readonly ILogger<CacheService> _logger;
+        public CacheService(ICache cache, IUser user, ILogger<CacheService> logger)
+        {
+            _cache = cache;
+            _user = user;
+            _logger = logger;
+        }
+
+        public IResponseOutput List()
+        {
+            var list = new List<object>();
+            var cacheKey = typeof(CacheKey);
+            var fields = cacheKey.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
+            foreach (var field in fields)
+            {
+                var descriptionAttribute = field.GetCustomAttributes(typeof(DescriptionAttribute),false).FirstOrDefault() as DescriptionAttribute;
+
+                list.Add(new 
+                {
+                    field.Name,
+                    Value = field.GetRawConstantValue().ToString(),
+                    descriptionAttribute?.Description
+                });
+            }
+
+            return ResponseOutput.Ok(list);
+        }
+
+        public async Task<IResponseOutput> ClearAsync(string cacheKey)
+        {
+            _logger.LogWarning($"{_user.Id}.{_user.Name}清除缓存[{cacheKey}]");
+            await _cache.DelByPatternAsync(cacheKey);
+            return ResponseOutput.Ok();
+        }
+    }
+}

+ 26 - 0
Admin.Core.Services/Admin/Cache/ICacheService.cs

@@ -0,0 +1,26 @@
+
+
+using Admin.Core.Model.Output;
+using System.Threading.Tasks;
+
+namespace Admin.Core.Service.Admin.Cache
+{
+    /// <summary>
+    /// 뻠닸륩蛟
+    /// </summary>	
+    public interface ICacheService
+    {
+        /// <summary>
+        /// 뻠닸죗깊
+        /// </summary>
+        /// <returns></returns>
+        IResponseOutput List();
+
+        /// <summary>
+        /// 헌뇜뻠닸
+        /// </summary>
+        /// <param name="cacheKey"></param>
+        /// <returns></returns>
+        Task<IResponseOutput> ClearAsync(string cacheKey);
+    }
+}

+ 1 - 1
Admin.Core.Services/Admin/View/ViewService.cs

@@ -187,7 +187,7 @@ namespace Admin.Core.Service.Admin.View
                         label = labels != null && labels.Count > 0 ? labels[0] : string.Empty;
                         desc = labels != null && labels.Count > 1 ? string.Join("\r\n", labels.GetRange(1, labels.Count() - 1)) : string.Empty;
 
-                        a.ParentId = pa?.Id;
+                        a.ParentId = pa.Id;
                         a.Label = label;
                         a.Description = desc;
                         a.Enabled = true;

+ 18 - 0
Admin.Core/Admin.Core.Service.xml

@@ -237,6 +237,24 @@
             映射配置
             </summary>
         </member>
+        <member name="T:Admin.Core.Service.Admin.Cache.ICacheService">
+            <summary>
+            缓存服务
+            </summary>	
+        </member>
+        <member name="M:Admin.Core.Service.Admin.Cache.ICacheService.List">
+            <summary>
+            缓存列表
+            </summary>
+            <returns></returns>
+        </member>
+        <member name="M:Admin.Core.Service.Admin.Cache.ICacheService.ClearAsync(System.String)">
+            <summary>
+            清除缓存
+            </summary>
+            <param name="cacheKey"></param>
+            <returns></returns>
+        </member>
         <member name="T:Admin.Core.Service.Admin.Dictionary.Input.DictionaryAddInput">
             <summary>
             添加

+ 21 - 3
Admin.Core/Admin.Core.xml

@@ -21,7 +21,7 @@
         </member>
         <member name="T:Admin.Core.Attributes.VersionRouteAttribute">
             <summary>
-            自定义路由 /api/{version}/[controler]/[action]
+            自定义路由 /api/{version}/[area]/[controler]/[action]
             </summary>
         </member>
         <member name="T:Admin.Core.Auth.IPermissionHandler">
@@ -135,7 +135,7 @@
             <param name="lastKey">上次验证码键</param>
             <returns></returns>
         </member>
-        <member name="M:Admin.Core.Controllers.Admin.AuthController.GetPassWordKey">
+        <member name="M:Admin.Core.Controllers.Admin.AuthController.GetPassWordEncryptKey">
             <summary>
             获取密钥
             </summary>
@@ -155,6 +155,24 @@
             <param name="input">登录信息</param>
             <returns></returns>
         </member>
+        <member name="T:Admin.Core.Controllers.Admin.CacheController">
+            <summary>
+            缓存管理
+            </summary>
+        </member>
+        <member name="M:Admin.Core.Controllers.Admin.CacheController.List">
+            <summary>
+            获取缓存列表
+            </summary>
+            <returns></returns>
+        </member>
+        <member name="M:Admin.Core.Controllers.Admin.CacheController.Clear(System.String)">
+            <summary>
+            清除缓存
+            </summary>
+            <param name="cacheKey"></param>
+            <returns></returns>
+        </member>
         <member name="T:Admin.Core.Controllers.Admin.DictionaryController">
             <summary>
             数据字典
@@ -261,7 +279,7 @@
         </member>
         <member name="M:Admin.Core.Controllers.Admin.PermissionController.GetPermissionList">
             <summary>
-            查询权限分配界面权限列表
+            查询角色权限-权限列表
             </summary>
             <returns></returns>
         </member>

+ 2 - 2
Admin.Core/Attributes/VersionRouteAttribute.cs

@@ -6,14 +6,14 @@ using Microsoft.AspNetCore.Mvc.ApiExplorer;
 namespace Admin.Core.Attributes
 {
     /// <summary>
-    /// 自定义路由 /api/{version}/[controler]/[action]
+    /// 自定义路由 /api/{version}/[area]/[controler]/[action]
     /// </summary>
     [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
     public class VersionRouteAttribute : RouteAttribute, IApiDescriptionGroupNameProvider
     {
         public string GroupName { get; set; }
 
-        public VersionRouteAttribute(ApiVersion version, string actionName = "") : base($"/api/{version.ToString()}/[controller]/{actionName}")
+        public VersionRouteAttribute(ApiVersion version, string actionName = "") : base($"/api/{version.ToString()}/[area]/[controller]/{actionName}")
         {
             GroupName = version.ToString();
         }

+ 2 - 2
Admin.Core/Controllers/Admin/AuthController.cs

@@ -39,9 +39,9 @@ namespace Admin.Core.Controllers.Admin
         /// <returns></returns>
         [HttpGet]
         [AllowAnonymous]
-        public async Task<IResponseOutput> GetPassWordKey()
+        public async Task<IResponseOutput> GetPassWordEncryptKey()
         {
-            return await _authServices.GetPassWordKeyAsync();
+            return await _authServices.GetPassWordEncryptKeyAsync();
         }
 
         /// <summary>

+ 42 - 0
Admin.Core/Controllers/Admin/CacheController.cs

@@ -0,0 +1,42 @@
+using System.Threading.Tasks;
+using Admin.Core.Model.Output;
+using Admin.Core.Service.Admin.Cache;
+using Microsoft.AspNetCore.Mvc;
+
+namespace Admin.Core.Controllers.Admin
+{
+    /// <summary>
+    /// 缓存管理
+    /// </summary>
+    public class CacheController : AreaController
+    {
+        
+        private readonly ICacheService _cacheServices;
+        
+        public CacheController(ICacheService cacheServices)
+        {
+            _cacheServices = cacheServices;
+        }
+
+        /// <summary>
+        /// 获取缓存列表
+        /// </summary>
+        /// <returns></returns>
+        [HttpGet]
+        public IResponseOutput List()
+        {
+            return _cacheServices.List();
+        }
+
+        /// <summary>
+        /// 清除缓存
+        /// </summary>
+        /// <param name="cacheKey"></param>
+        /// <returns></returns>
+        [HttpDelete]
+        public async Task<IResponseOutput> Clear(string cacheKey)
+        {
+            return await _cacheServices.ClearAsync(cacheKey);
+        }
+    }
+}

+ 1 - 1
Admin.Core/Controllers/Admin/PermissionController.cs

@@ -66,7 +66,7 @@ namespace Admin.Core.Controllers.Admin
         }
 
         /// <summary>
-        /// 查询权限分配界面权限列表
+        /// 查询角色权限-权限列表
         /// </summary>
         /// <returns></returns>
         [HttpGet]

Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 0 - 0
Admin.Core/Db/Data/data.json


+ 4 - 1
Admin.Core/configs/appconfig.json

@@ -3,8 +3,11 @@
   "swagger": false,
   //Api地址
   "urls": "http://*:8081",
+  //面向切面编程
   "aop": {
     //事物
     "transaction": false
-  }
+  },
+  //同步缓存键到数据库
+  "syncCacheKey": true
 }

Daži faili netika attēloti, jo izmaiņu fails ir pārāk liels