完善控制器接口
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -338,3 +338,4 @@ ASALocalRun/
|
||||
|
||||
# BeatPulse healthcheck temp database
|
||||
healthchecksdb
|
||||
Yi.Framework/Yi.Framework.ApiMicroservice/YIDB.db-shm
|
||||
|
||||
@@ -129,8 +129,37 @@ namespace Yi.Framework.ApiMicroservice.Controllers
|
||||
var _user = HttpContext.GetCurrentUserInfo();
|
||||
return Result.Success().SetData(await _userService.GetUserInfoById(_user.id));
|
||||
}
|
||||
|
||||
|
||||
[HttpGet]
|
||||
public async Task<Result> GetMenuByUserId()
|
||||
{
|
||||
var _user = HttpContext.GetCurrentUserInfo();
|
||||
return Result.Success().SetData(await _userService.GetMenuById(_user.id));
|
||||
}
|
||||
[HttpPost]
|
||||
public async Task<Result> GetRouterByUserId( AxiosUrlsModel urlsModel,string router)
|
||||
{
|
||||
var _user = HttpContext.GetCurrentUserInfo();
|
||||
var menuList= await _userService.GetMenuById(_user.id);
|
||||
var menu_data= menuList.Where(u => u.router == router).FirstOrDefault();
|
||||
foreach(var _menu in menu_data.children)
|
||||
if (_menu.mould.mould_name == "get")
|
||||
{
|
||||
urlsModel.get= _menu.mould.url;
|
||||
}
|
||||
else if (_menu.mould.mould_name == "del")
|
||||
{
|
||||
urlsModel.del= _menu.mould.url ;
|
||||
}
|
||||
else if (_menu.mould.mould_name == "update")
|
||||
{
|
||||
urlsModel.update= _menu.mould.url ;
|
||||
}
|
||||
else if (_menu.mould.mould_name == "add")
|
||||
{
|
||||
urlsModel.add = _menu.mould.url ;
|
||||
}
|
||||
return Result.Success().SetData(urlsModel);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.ApiMicroservice.Controllers
|
||||
{
|
||||
[Route("api/[controller]/[action")]
|
||||
[ApiController]
|
||||
public class VisitController : ControllerBase
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Quartz" Version="3.3.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -71,36 +71,36 @@ namespace Yi.Framework.Common.Helper
|
||||
/// <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();
|
||||
//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;
|
||||
}
|
||||
// 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);
|
||||
}
|
||||
// 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();
|
||||
// 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","");
|
||||
}
|
||||
// if (selectFiles.Count > 0)
|
||||
// {
|
||||
// return selectFiles.FirstOrDefault().Name.Replace(".log","");
|
||||
// }
|
||||
|
||||
return $@"{prefix}_{DateTime.Now.DateToTimeStamp()}";
|
||||
}
|
||||
// return $@"{prefix}_{DateTime.Now.DateToTimeStamp()}";
|
||||
//}
|
||||
#endregion
|
||||
|
||||
#region 写文件
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace Yi.Framework.Common.Helper
|
||||
|
||||
{
|
||||
/// <summary>
|
||||
/// 加密用的
|
||||
/// </summary>
|
||||
public class MD5Helper
|
||||
{
|
||||
/// <summary>
|
||||
/// MD5 加密字符串
|
||||
/// </summary>
|
||||
/// <param name="content">源字符串</param>
|
||||
/// <returns>加密后字符串</returns>
|
||||
public static string MD5EncodingOnly(string content)
|
||||
{
|
||||
// 创建MD5类的默认实例:MD5CryptoServiceProvider
|
||||
MD5 md5 = MD5.Create();
|
||||
byte[] bs = Encoding.UTF8.GetBytes(content);
|
||||
byte[] hs = md5.ComputeHash(bs);
|
||||
StringBuilder stb = new StringBuilder();
|
||||
foreach (byte b in hs)
|
||||
{
|
||||
// 以十六进制格式格式化
|
||||
stb.Append(b.ToString("x2"));
|
||||
}
|
||||
return stb.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// MD5盐值加密
|
||||
/// </summary>
|
||||
/// <param name="content">源字符串</param>
|
||||
/// <param name="salt">盐值</param>
|
||||
/// <returns>加密后字符串</returns>
|
||||
public static string MD5EncodingWithSalt(string content, string salt)
|
||||
{
|
||||
if (salt == null) return content;
|
||||
return MD5EncodingOnly(content + "{" + salt.ToString() + "}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ using System.Text;
|
||||
|
||||
namespace Yi.Framework.Common.Helper
|
||||
{
|
||||
public class MD5Helper
|
||||
public class MD5Helper
|
||||
{
|
||||
/// <summary>
|
||||
/// 16位MD5加密
|
||||
|
||||
@@ -4,4 +4,8 @@
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -45,10 +45,10 @@ namespace Yi.Framework.Interface
|
||||
Task<List<mould>> GetMouldByUser(user _user);
|
||||
|
||||
/// <summary>
|
||||
/// 给单个用户设置多个角色
|
||||
/// 给多个用户设置多个角色
|
||||
/// </summary>
|
||||
/// <param name="roleIds"></param>
|
||||
/// <param name="userId"></param>
|
||||
/// <param name="userIds"></param>
|
||||
/// <returns></returns>
|
||||
Task<bool> SetRolesByUser(List<int> roleIds, List<int> userIds);
|
||||
/// <summary>
|
||||
@@ -64,11 +64,22 @@ namespace Yi.Framework.Interface
|
||||
/// <returns></returns>
|
||||
Task<List<menu>> GetMenuByUser(user _user);
|
||||
/// <summary>
|
||||
/// 通过用户id,得到该用户的所有信息,关联角色
|
||||
/// 通过用户id,得到该用户的所有信息,关联角色,过滤迭代
|
||||
/// </summary>
|
||||
/// <param name="user_id"></param>
|
||||
/// <returns></returns>
|
||||
Task<user> GetUserInfoById(int user_id);
|
||||
/// <summary>
|
||||
/// 通过用户id,得到该用户的所有信息,关联角色
|
||||
/// </summary>
|
||||
/// <param name="user_id"></param>
|
||||
/// <returns></returns>
|
||||
Task<user> GetUserById(int user_id);
|
||||
/// <summary>
|
||||
/// 通过http获取用户id,得到该用户所有的菜单(递归的那种),把所有children为[]的值全部过滤成null,不要绑定mould
|
||||
/// </summary>
|
||||
/// <param name="user_id"></param>
|
||||
/// <returns></returns>
|
||||
Task<List<menu>> GetMenuById(int user_id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,11 +14,25 @@ namespace Yi.Framework.Model.DbInit
|
||||
{
|
||||
if (!_Db.Set<user>().Any())
|
||||
{
|
||||
await _Db.Set<user>().AddAsync(new user { username = "admin", password = "123" });
|
||||
await _Db.Set<user>().AddAsync(new user
|
||||
{
|
||||
username = "admin",
|
||||
password = "123",
|
||||
roles = new List<role>()
|
||||
{
|
||||
new role()
|
||||
{
|
||||
menus = new List<menu>()
|
||||
{
|
||||
new menu() { mould=new mould()}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
await _Db.SaveChangesAsync();
|
||||
|
||||
Console.WriteLine(nameof(DbContext) + ":数据库用户初始成功!");
|
||||
Console.WriteLine(nameof(DbContext) + ":数据库初始成功!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,7 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.HttpsPolicy;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Interface;
|
||||
using Yi.Framework.Service;
|
||||
using Yi.Framework.WebCore.MiddlewareExtend;
|
||||
|
||||
@@ -59,7 +59,7 @@ namespace Yi.Framework.Service
|
||||
public async Task<menu> GetMenuMouldByMenu(menu _menu)
|
||||
{
|
||||
var menu_data = await _Db.Set<menu>().Include(u => u.children).Include(u=>u.mould)
|
||||
.Where(u=>u.id==_menu.id&& u.is_delete == (short)Common.Enum.DelFlagEnum.Normal).FirstOrDefaultAsync();
|
||||
.Where(u=>u.id==_menu.id&& u.is_delete == (short)Common.Enum.DelFlagEnum.Normal && u.is_delete == (short)Common.Enum.ShowFlagEnum.Show).FirstOrDefaultAsync();
|
||||
return menu_data;
|
||||
}
|
||||
|
||||
@@ -74,7 +74,7 @@ namespace Yi.Framework.Service
|
||||
{
|
||||
var menu_data= await _Db.Set<menu>().Include(u=>u.mould).Include(u => u.children)
|
||||
.ThenInclude(u => u.children).ThenInclude(u => u.children).ThenInclude(u => u.children)
|
||||
.Where(u =>u.is_delete == (short)Common.Enum.DelFlagEnum.Normal && u.is_top == (short)Common.Enum.TopFlagEnum.Top)
|
||||
.Where(u =>u.is_delete == (short)Common.Enum.DelFlagEnum.Normal && u.is_top == (short)Common.Enum.ShowFlagEnum.Show)
|
||||
.ToListAsync();
|
||||
return TopMenuBuild(menu_data);
|
||||
}
|
||||
|
||||
@@ -134,5 +134,40 @@ namespace Yi.Framework.Service
|
||||
.Where(u => u.id == user_id && u.is_delete == (short)Common.Enum.DelFlagEnum.Normal).FirstOrDefaultAsync();
|
||||
return user_data;
|
||||
}
|
||||
public async Task<List<menu>> GetMenuById(int user_id)
|
||||
{
|
||||
var user_data = await _Db.Set<user>().Include(u => u.roles).ThenInclude(u => u.menus).ThenInclude(u => u.children)
|
||||
.ThenInclude(u => u.children).ThenInclude(u => u.children).ThenInclude(u => u.mould)
|
||||
.Where(u => u.id == user_id && u.is_delete == (short)Common.Enum.DelFlagEnum.Normal).FirstOrDefaultAsync();
|
||||
var role_data = user_data.roles.ToList();
|
||||
List<menu> menu_data = new();
|
||||
foreach (var role in role_data)
|
||||
{
|
||||
var menu = await _roleService.GetMenusByRole(role);
|
||||
menu.ForEach(u => u.roles = null);
|
||||
menu_data = menu_data.Concat(menu).OrderByDescending(u => u.sort).ToList();
|
||||
}
|
||||
return TopMenuBuild(menu_data);
|
||||
}
|
||||
private List<menu> TopMenuBuild(List<menu> menu_data)
|
||||
{
|
||||
|
||||
for (int i = menu_data.Count() - 1; i >= 0; i--)
|
||||
{
|
||||
if (menu_data[i].is_delete == (short)Common.Enum.DelFlagEnum.Deleted|| menu_data[i].is_delete == (short)Common.Enum.ShowFlagEnum.NoShow)
|
||||
{
|
||||
menu_data.Remove(menu_data[i]);
|
||||
}
|
||||
else if (menu_data[i].children == null)
|
||||
{
|
||||
menu_data[i].children =null;
|
||||
}
|
||||
else if (menu_data[i].children != null)
|
||||
{
|
||||
menu_data[i].children = TopMenuBuild(menu_data[i].children.ToList());
|
||||
}
|
||||
}
|
||||
return menu_data;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,49 +23,22 @@ namespace Yi.Framework.WebCore.FilterExtend
|
||||
/// <summary>
|
||||
/// 防重复提交周期 单位秒
|
||||
/// </summary>
|
||||
public int TimeOut = 1;
|
||||
//public int TimeOut = 1;
|
||||
|
||||
#region Identity
|
||||
private readonly ILogger<CustomAction2CommitFilterAttribute> _logger;
|
||||
private readonly CacheClientDB _cacheClientDB;
|
||||
private static string KeyPrefix = "2CommitFilter";
|
||||
//#region Identity
|
||||
//private readonly ILogger<CustomAction2CommitFilterAttribute> _logger;
|
||||
//private readonly CacheClientDB _cacheClientDB;
|
||||
//private static string KeyPrefix = "2CommitFilter";
|
||||
|
||||
public CustomAction2CommitFilterAttribute(ILogger<CustomAction2CommitFilterAttribute> logger, CacheClientDB cacheClientDB)
|
||||
{
|
||||
this._logger = logger;
|
||||
this._cacheClientDB = cacheClientDB;
|
||||
}
|
||||
#endregion
|
||||
// public CustomAction2CommitFilterAttribute(ILogger<CustomAction2CommitFilterAttribute> logger, CacheClientDB cacheClientDB)
|
||||
// {
|
||||
// this._logger = logger;
|
||||
// this._cacheClientDB = cacheClientDB;
|
||||
// }
|
||||
// #endregion
|
||||
|
||||
|
||||
public override void OnActionExecuting(ActionExecutingContext context)
|
||||
{
|
||||
string url = context.HttpContext.Request.Path.Value;
|
||||
string argument = JsonConvert.SerializeObject(context.ActionArguments);
|
||||
string ip = context.HttpContext.Connection.RemoteIpAddress.ToString();
|
||||
string agent = context.HttpContext.Request.Headers["User-Agent"];
|
||||
string sInfo = $"{url}-{argument}-{ip}-{agent}";
|
||||
string summary = MD5Helper.MD5EncodingOnly(sInfo);
|
||||
|
||||
string totalKey = $"{KeyPrefix}-{summary}";
|
||||
|
||||
string result = this._cacheClientDB.Get<string>(totalKey);
|
||||
if (string.IsNullOrEmpty(result))
|
||||
{
|
||||
this._cacheClientDB.Add(totalKey, "1", TimeSpan.FromSeconds(3));//3秒有效期
|
||||
this._logger.LogInformation($"CustomAction2CommitFilterAttribute:{sInfo}");
|
||||
}
|
||||
else
|
||||
{
|
||||
//已存在
|
||||
this._logger.LogWarning($"CustomAction2CommitFilterAttribute重复请求:{sInfo}");
|
||||
context.Result = new JsonResult(Result.Error("请勿重复提交"));
|
||||
}
|
||||
|
||||
//CurrentUser currentUser = context.HttpContext.GetCurrentUserBySession();
|
||||
//if (currentUser == null)
|
||||
//{
|
||||
// //if (this.IsAjaxRequest(context.HttpContext.Request))
|
||||
//if (this.IsAjaxRequest(context.HttpContext.Request))
|
||||
// //{ }
|
||||
// context.Result = new RedirectResult("~/Fourth/Login");
|
||||
//}
|
||||
@@ -73,11 +46,38 @@ namespace Yi.Framework.WebCore.FilterExtend
|
||||
//{
|
||||
// this._logger.LogDebug($"{currentUser.Name} 访问系统");
|
||||
//}
|
||||
}
|
||||
private bool IsAjaxRequest(HttpRequest request)
|
||||
{
|
||||
string header = request.Headers["X-Requested-With"];
|
||||
return "XMLHttpRequest".Equals(header);
|
||||
}
|
||||
//} public override void OnActionExecuting(ActionExecutingContext context)
|
||||
//{
|
||||
// string url = context.HttpContext.Request.Path.Value;
|
||||
// string argument = JsonConvert.SerializeObject(context.ActionArguments);
|
||||
// string ip = context.HttpContext.Connection.RemoteIpAddress.ToString();
|
||||
// string agent = context.HttpContext.Request.Headers["User-Agent"];
|
||||
// string sInfo = $"{url}-{argument}-{ip}-{agent}";
|
||||
// string summary = MD5Helper.MD5EncodingOnly(sInfo);
|
||||
|
||||
// string totalKey = $"{KeyPrefix}-{summary}";
|
||||
|
||||
// string result = this._cacheClientDB.Get<string>(totalKey);
|
||||
// if (string.IsNullOrEmpty(result))
|
||||
// {
|
||||
// this._cacheClientDB.Add(totalKey, "1", TimeSpan.FromSeconds(3));//3秒有效期
|
||||
// this._logger.LogInformation($"CustomAction2CommitFilterAttribute:{sInfo}");
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// //已存在
|
||||
// this._logger.LogWarning($"CustomAction2CommitFilterAttribute重复请求:{sInfo}");
|
||||
// context.Result = new JsonResult(Result.Error("请勿重复提交"));
|
||||
// }
|
||||
|
||||
// //CurrentUser currentUser = context.HttpContext.GetCurrentUserBySession();
|
||||
// //if (currentUser == null)
|
||||
// //{
|
||||
// // //
|
||||
//private bool IsAjaxRequest(HttpRequest request)
|
||||
//{
|
||||
// string header = request.Headers["X-Requested-With"];
|
||||
// return "XMLHttpRequest".Equals(header);
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ namespace Yi.Framework.WebCore.MiddlewareExtend
|
||||
#endregion
|
||||
services.AddScoped<DbContext, DataContext>();
|
||||
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user