重构代码

重构代码
This commit is contained in:
chenchun
2022-04-02 17:44:50 +08:00
parent c6371ba72d
commit f5fb2ea17b
105 changed files with 1489 additions and 4654 deletions

View File

@@ -7,7 +7,6 @@ using System.Security.Claims;
using System.Threading.Tasks;
using Yi.Framework.Common.Const;
using Yi.Framework.Core;
using Yi.Framework.DTOModel;
using Yi.Framework.Model.Models;
namespace Yi.Framework.WebCore.AuthorizationPolicy
@@ -45,15 +44,15 @@ namespace Yi.Framework.WebCore.AuthorizationPolicy
//现在只需要登录的时候把用户的api路径添加到redis去
//每次访问的时候进行redis判断一下即可
//注意一下redis不能一直保存和jwt一样搞一个期限
var menuList=_cacheClientDB.Get<List<menuDto>>(RedisConst.userMenusApi+":"+currentUserId);
foreach (var k in menuList)
{
if (k.mould != null)
{
dicMenueDictionary.Add(k.mould?.id.ToString(), "/api"+ k.mould?.url);
}
//var menuList=_cacheClientDB.Get<List<menuDto>>(RedisConst.userMenusApi+":"+currentUserId);
//foreach (var k in menuList)
//{
// if (k.mould != null)
// {
// dicMenueDictionary.Add(k.mould?.id.ToString(), "/api"+ k.mould?.url);
// }
}
//}
if (dicMenueDictionary.ContainsValue(httpcontext.Request.Path))
{

View File

@@ -30,7 +30,7 @@ namespace Yi.Framework.WebCore
/// </summary>
/// <param name="httpContext"></param>
/// <returns></returns>
public static user GetCurrentUserInfo(this HttpContext httpContext, out List<int> menuIds)
public static User GetCurrentUserInfo(this HttpContext httpContext, out List<int> menuIds)
{
IEnumerable<Claim> claimlist = httpContext.AuthenticateAsync().Result.Principal.Claims;
@@ -40,22 +40,22 @@ namespace Yi.Framework.WebCore
menuIds = claimlist.Where(u => u.Type == "menuIds").ToList().Select(u => Convert.ToInt32(u.Value)).ToList();
return new user()
return new User()
{
id = resId,
username = claimlist.FirstOrDefault(u => u.Type == ClaimTypes.Name).Value ?? "匿名"
Id = resId,
Username = claimlist.FirstOrDefault(u => u.Type == ClaimTypes.Name).Value ?? "匿名"
};
}
public static user GetCurrentUserInfo(this HttpContext httpContext)
public static User GetCurrentUserInfo(this HttpContext httpContext)
{
IEnumerable<Claim> claimlist = httpContext.AuthenticateAsync().Result.Principal.Claims;
Int32.TryParse(claimlist.FirstOrDefault(u => u.Type == ClaimTypes.Sid).Value, out int resId);
return new user()
return new User()
{
id = resId,
username = claimlist.FirstOrDefault(u => u.Type == ClaimTypes.Name).Value ?? "匿名"
Id = resId,
Username = claimlist.FirstOrDefault(u => u.Type == ClaimTypes.Name).Value ?? "匿名"
};
}
}

View File

@@ -1,78 +0,0 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Model.ModelFactory;
using Yi.Framework.Model.Models;
namespace Yi.Framework.WebCore.Init
{
public class DataSeed
{
public async static Task SeedAsync(IDbContextFactory _DbFactory)
{
var _Db = _DbFactory.ConnWriteOrRead(Common.Enum.WriteAndReadEnum.Write);
if (!_Db.Set<user>().Any())
{
List<menu> menus = new List<menu>{
new menu{ id=1, menu_name="根",is_show=1,is_top=1},
new menu{ id=2,icon="mdi-view-dashboard", menu_name="首页",is_show=1,is_top=0,router="/",parentId=1},
new menu{id=3,icon="mdi-account-box-multiple", menu_name="用户角色管理",is_show=1,is_top=0,parentId=1},
new menu{id=4,icon="mdi-account-box", menu_name="用户管理",router="/AdmUser/",is_show=1,is_top=0,parentId=3},
new menu{id=5, menu_name="get",is_show=0,is_top=0,parentId=4,mould=new mould{mould_name="get",url="/user/getuser" } },
new menu{id=6, menu_name="update",is_show=0,is_top=0,parentId=4,mould=new mould{mould_name="update",url="/user/updateuser" } },
new menu{id=7, menu_name="del",is_show=0,is_top=0,parentId=4,mould=new mould{mould_name="del",url="/user/dellistUser" } },
new menu{id=8, menu_name="add",is_show=0,is_top=0,parentId=4,mould=new mould{mould_name="add",url="/role/adduser" } },
new menu{ id=9,icon="mdi-account-circle", menu_name="角色管理",router="/admrole/",is_show=1,is_top=0,parentId=3},
new menu{id=10, menu_name="get",is_show=0,is_top=0,parentId=9,mould=new mould{mould_name="get",url="/role/getrole" } },
new menu{id=11, menu_name="update",is_show=0,is_top=0,parentId=9,mould=new mould{mould_name="update",url="/role/updaterole" } },
new menu{id=12, menu_name="del",is_show=0,is_top=0,parentId=9,mould=new mould{mould_name="del",url="/role/dellistrole" } },
new menu{id=13, menu_name="add",is_show=0,is_top=0,parentId=9,mould=new mould{mould_name="add",url="/role/addrole" } },
new menu{ id=14,icon="mdi-account-cash", menu_name="角色接口管理",is_show=1,is_top=0,parentId=1},
new menu{ id=15,icon="mdi-clipboard-check-multiple", menu_name="菜单管理",router="/AdmMenu/",is_show=1,is_top=0,parentId=14},
new menu{id=16, menu_name="get",is_show=0,is_top=0,parentId=15,mould=new mould{mould_name="get",url="/menu/getmenu" } },
new menu{id=17, menu_name="update",is_show=0,is_top=0,parentId=15,mould=new mould{mould_name="update",url="/menu/updatemenu" } },
new menu{id=18, menu_name="del",is_show=0,is_top=0,parentId=15,mould=new mould{mould_name="del",url="/menu/dellistmenu" } },
new menu{id=19, menu_name="add",is_show=0,is_top=0,parentId=15,mould=new mould{mould_name="add",url="/menu/addmenu" } },
new menu{ id=20,icon="mdi-circle-slice-8", menu_name="接口管理",router="/admMould/",is_show=1,is_top=0,parentId=14},
new menu{id=21, menu_name="get",is_show=0,is_top=0,parentId=20,mould=new mould{mould_name="get",url="/Mould/getMould" } },
new menu{id=22, menu_name="update",is_show=0,is_top=0,parentId=20,mould=new mould{mould_name="update",url="/Mould/updateMould" } },
new menu{id=23, menu_name="del",is_show=0,is_top=0,parentId=20,mould=new mould{mould_name="del",url="/Mould/dellistMould" } },
new menu{id=24, menu_name="add",is_show=0,is_top=0,parentId=20,mould=new mould{mould_name="add",url="/Mould/addMould" } },
new menu{ id=25,icon="mdi-clipboard-account", menu_name="角色菜单分配管理",router="/admRoleMenu/",is_show=1,is_top=0,parentId=14},
new menu{ id=26,icon="mdi-clipboard-flow-outline", menu_name="路由管理",is_show=1,is_top=0,parentId=1},
new menu{ id=27,icon="mdi-account-eye", menu_name="用户信息",router="/userinfo/",is_show=1,is_top=0,parentId=26},
};
List<role> roles = new List<role>() {
new role(){role_name="普通用户" },
new role(){role_name="管理员",menus= menus}
};
List<user> users = new List<user>() {
new user(){ username="admin",password="123",roles=roles}
};
await _Db.Set<user>().AddRangeAsync(users);
await _Db.SaveChangesAsync();
Console.WriteLine(nameof(DbContext) + ":数据库初始成功!");
}
}
}
}

View File

@@ -1,31 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Common.Const;
using Yi.Framework.Core;
using Yi.Framework.DTOModel;
namespace Yi.Framework.WebCore.Init
{
public class RedisInit
{
public static void Seed(CacheClientDB _cacheClientDB)
{
var setDto = Common.Helper.JsonHelper.ObjToStr(new SettingDto()
{
ImageList =new List<string> { "默认图片", "默认图片" },
InitRole = "普通用户",
Title = "YiFramework",
InitIcon = "默认头像"
});
if (_cacheClientDB.Get<SettingDto>(RedisConst.key)==null)
{
_cacheClientDB.Add(RedisConst.key,setDto) ;
}
Console.WriteLine(nameof(RedisInit) + ":Redis初始成功");
}
}
}

View File

@@ -1,24 +0,0 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Common.IOCOptions;
using Yi.Framework.Model;
using Yi.Framework.Model.ModelFactory;
namespace Yi.Framework.WebCore.MiddlewareExtend
{
public static class DbExtend
{
public static IServiceCollection AddDbService(this IServiceCollection services)
{
DbContextFactory.MutiDB_Enabled = Appsettings.appBool("MutiDB_Enabled");
DataContext.DbSelect = Appsettings.app("DbSelect");
DataContext._connStr = Appsettings.app("DbConn", "WriteUrl");
services.Configure<DbConnOptions>(Appsettings.appConfiguration("DbConn"));
return services;
}
}
}

View File

@@ -1,36 +0,0 @@
using log4net;
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Model.ModelFactory;
using Yi.Framework.WebCore.Init;
namespace Yi.Framework.WebCore.MiddlewareExtend
{
public static class DbSeedInitExtend
{
private static readonly ILog log = LogManager.GetLogger(typeof(DbSeedInitExtend));
public static void UseDbSeedInitService(this IApplicationBuilder app, IDbContextFactory _DbFactory)
{
if (Appsettings.appBool("DbSeed_Enabled"))
{
if (app == null) throw new ArgumentNullException(nameof(app));
try
{
DataSeed.SeedAsync(_DbFactory).Wait();
}
catch (Exception e)
{
log.Error($"Error occured seeding the Database.\n{e.Message}");
throw;
}
}
}
}
}

View File

@@ -5,7 +5,6 @@ using Microsoft.Extensions.DependencyInjection;
using System;
using System.IO;
using Yi.Framework.Model;
using Yi.Framework.Model.ModelFactory;
namespace Yi.Framework.WebCore.MiddlewareExtend
{
@@ -20,10 +19,7 @@ namespace Yi.Framework.WebCore.MiddlewareExtend
//配置文件使用配置
#endregion
services.AddSingleton(new Appsettings(configuration));
#region
//数据库配置
#endregion
services.AddTransient<DbContext, DataContext>();
return services;
}

View File

@@ -7,8 +7,6 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Core;
using Yi.Framework.Model.ModelFactory;
using Yi.Framework.WebCore.Init;
namespace Yi.Framework.WebCore.MiddlewareExtend
{
@@ -24,7 +22,7 @@ namespace Yi.Framework.WebCore.MiddlewareExtend
try
{
RedisInit.Seed(_cacheClientDB);
//RedisInit.Seed(_cacheClientDB);
}
catch (Exception e)
{

View File

@@ -12,7 +12,6 @@ using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Yi.Framework.Job;
using Yi.Framework.Model.ModelFactory;
using Yi.Framework.WebCore.Utility;
using Module = Autofac.Module;
@@ -35,7 +34,7 @@ namespace Yi.Framework.WebCore.Utility
protected override void Load(ContainerBuilder containerBuilder)
{
containerBuilder.RegisterType<DbContextFactory>().As<IDbContextFactory>().InstancePerDependency().EnableInterfaceInterceptors();
//containerBuilder.RegisterType<DbContextFactory>().As<IDbContextFactory>().InstancePerDependency().EnableInterfaceInterceptors();
containerBuilder.RegisterType< HttpContextAccessor>().As<IHttpContextAccessor>().SingleInstance();

View File

@@ -32,4 +32,8 @@
<ProjectReference Include="..\Yi.Framework.Model\Yi.Framework.Model.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Init\" />
</ItemGroup>
</Project>