refactor: 抽象操作日志模块

This commit is contained in:
陈淳
2023-02-22 16:49:24 +08:00
parent fa4e0b3752
commit 99aa7d3361
43 changed files with 415 additions and 212 deletions

View File

@@ -120,25 +120,6 @@
临时服务,之后用其他模块代替
</summary>
</member>
<member name="M:Yi.BBS.Application.GlobalSetting.TempService.PostLoginAsync">
<summary>
登录
</summary>
<returns></returns>
<exception cref="T:System.NotImplementedException"></exception>
</member>
<member name="M:Yi.BBS.Application.GlobalSetting.TempService.PostLogged">
<summary>
判断是否有登录
</summary>
<returns></returns>
</member>
<member name="M:Yi.BBS.Application.GlobalSetting.TempService.PostlogOut">
<summary>
退出登录
</summary>
<returns></returns>
</member>
<member name="M:Yi.BBS.Application.GlobalSetting.TempService.GetUserInfoByIdAsync(System.Int64)">
<summary>
获取用户信息

View File

@@ -10,6 +10,7 @@ using Yi.Framework.Core.Autofac;
using Yi.RBAC.Application;
using Yi.Framework.AspNetCore;
using Yi.Framework.Data.Json;
using Yi.Framework.OperLog;
namespace Yi.BBS.Web
{
@@ -31,7 +32,8 @@ namespace Yi.BBS.Web
{
//NETServiceTest所在程序集添加进动态api配置
opt.CreateConventional(typeof(YiBBSApplicationModule).Assembly, option => option.RootPath = string.Empty);
//opt.CreateConventional(typeof(YiRBACApplicationModule).Assembly, option => option.RootPath = string.Empty);
opt.CreateConventional(typeof(YiRBACApplicationModule).Assembly, option => option.RootPath = string.Empty);
opt.CreateConventional(typeof(YiFrameworkOperLogModule).Assembly, option => option.RootPath = string.Empty);
});
//添加swagger

View File

@@ -16,7 +16,7 @@
"DbConnOptions": {
"Url": "DataSource=yi-sqlsugar-dev.db",
"DbType": "Sqlite",
"EnabledDbSeed": false,
"EnabledDbSeed": true,
"EnabledReadWrite": false,
"EnabledCodeFirst": true,
"EntityAssembly": null,

View File

@@ -120,25 +120,6 @@
临时服务,之后用其他模块代替
</summary>
</member>
<member name="M:Yi.BBS.Application.GlobalSetting.TempService.PostLoginAsync">
<summary>
登录
</summary>
<returns></returns>
<exception cref="T:System.NotImplementedException"></exception>
</member>
<member name="M:Yi.BBS.Application.GlobalSetting.TempService.PostLogged">
<summary>
判断是否有登录
</summary>
<returns></returns>
</member>
<member name="M:Yi.BBS.Application.GlobalSetting.TempService.PostlogOut">
<summary>
退出登录
</summary>
<returns></returns>
</member>
<member name="M:Yi.BBS.Application.GlobalSetting.TempService.GetUserInfoByIdAsync(System.Int64)">
<summary>
获取用户信息

View File

@@ -14,43 +14,43 @@ namespace Yi.BBS.Application.GlobalSetting
[AppService]
public class TempService : ApplicationService, IAutoApiService
{
/// <summary>
/// 登录
/// </summary>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
[Route("/api/account/login")]
public Task<LoginDto> PostLoginAsync()
{
bool loginSucces = true;
if (!loginSucces)
{
throw new UserFriendlyException("登录失败", (int)BbsHttpStatusEnum.LoginFailed, "用户或者密码错误");
}
var dto = new LoginDto("token");
dto.User = new LoginUserInfoDto { Icon = "", Id = 0, Level = 1, UserName = "橙子" };
return Task.FromResult(dto);
}
///// <summary>
///// 登录
///// </summary>
///// <returns></returns>
///// <exception cref="NotImplementedException"></exception>
//[Route("/api/account/login")]
//public Task<LoginDto> PostLoginAsync()
//{
// bool loginSucces = true;
// if (!loginSucces)
// {
// throw new UserFriendlyException("登录失败", (int)BbsHttpStatusEnum.LoginFailed, "用户或者密码错误");
// }
// var dto = new LoginDto("token");
// dto.User = new LoginUserInfoDto { Icon = "", Id = 0, Level = 1, UserName = "橙子" };
// return Task.FromResult(dto);
//}
/// <summary>
/// 判断是否有登录
/// </summary>
/// <returns></returns>
[Route("/api/account/logged")]
public Task<bool> PostLogged()
{
return Task.FromResult(true);
}
///// <summary>
///// 判断是否有登录
///// </summary>
///// <returns></returns>
//[Route("/api/account/logged")]
//public Task<bool> PostLogged()
//{
// return Task.FromResult(true);
//}
/// <summary>
/// 退出登录
/// </summary>
/// <returns></returns>
[Route("/api/account/logout")]
public Task PostlogOut()
{
return Task.CompletedTask;
}
///// <summary>
///// 退出登录
///// </summary>
///// <returns></returns>
//[Route("/api/account/logout")]
//public Task PostlogOut()
//{
// return Task.CompletedTask;
//}
/// <summary>
/// 获取用户信息

View File

@@ -10,6 +10,7 @@ using Yi.Framework.Core.Autofac;
using Yi.RBAC.Application;
using Yi.Framework.AspNetCore;
using Yi.Framework.Data.Json;
using Yi.Framework.OperLog;
namespace Yi.BBS.Web
{
@@ -31,7 +32,8 @@ namespace Yi.BBS.Web
{
//NETServiceTest所在程序集添加进动态api配置
opt.CreateConventional(typeof(YiBBSApplicationModule).Assembly, option => option.RootPath = string.Empty);
//opt.CreateConventional(typeof(YiRBACApplicationModule).Assembly, option => option.RootPath = string.Empty);
opt.CreateConventional(typeof(YiRBACApplicationModule).Assembly, option => option.RootPath = string.Empty);
opt.CreateConventional(typeof(YiFrameworkOperLogModule).Assembly, option => option.RootPath = string.Empty);
});
//添加swagger

View File

@@ -16,7 +16,7 @@
"DbConnOptions": {
"Url": "DataSource=yi-sqlsugar-dev.db",
"DbType": "Sqlite",
"EnabledDbSeed": false,
"EnabledDbSeed": true,
"EnabledReadWrite": false,
"EnabledCodeFirst": true,
"EntityAssembly": null,

View File

@@ -94,16 +94,61 @@
登录信息
</summary>
</member>
<member name="T:Yi.RBAC.Application.Contracts.Logs.IOperationLogService">
<summary>
OperationLog服务抽象
</summary>
</member>
<member name="T:Yi.RBAC.Application.Contracts.Setting.Dtos.ConfigCreateInputVo">
<summary>
Config输入创建对象
</summary>
</member>
<member name="T:Yi.RBAC.Application.Contracts.Setting.Dtos.ConfigGetListInputVo">
<summary>
配置查询参数
</summary>
</member>
<member name="P:Yi.RBAC.Application.Contracts.Setting.Dtos.ConfigGetListInputVo.ConfigName">
<summary>
配置名称
</summary>
</member>
<member name="P:Yi.RBAC.Application.Contracts.Setting.Dtos.ConfigGetListInputVo.ConfigKey">
<summary>
配置键
</summary>
</member>
<member name="P:Yi.RBAC.Application.Contracts.Setting.Dtos.ConfigGetListOutputDto.ConfigName">
<summary>
配置名称
</summary>
</member>
<member name="P:Yi.RBAC.Application.Contracts.Setting.Dtos.ConfigGetListOutputDto.ConfigKey">
<summary>
配置主键
</summary>
</member>
<member name="P:Yi.RBAC.Application.Contracts.Setting.Dtos.ConfigGetListOutputDto.ConfigValue">
<summary>
配置值
</summary>
</member>
<member name="P:Yi.RBAC.Application.Contracts.Setting.Dtos.ConfigGetListOutputDto.ConfigType">
<summary>
配置类型
</summary>
</member>
<member name="P:Yi.RBAC.Application.Contracts.Setting.Dtos.ConfigGetListOutputDto.OrderNum">
<summary>
排序字段
</summary>
</member>
<member name="P:Yi.RBAC.Application.Contracts.Setting.Dtos.ConfigGetListOutputDto.Remark">
<summary>
备注
</summary>
</member>
<member name="P:Yi.RBAC.Application.Contracts.Setting.Dtos.ConfigGetListOutputDto.CreationTime">
<summary>
创建时间
</summary>
</member>
<member name="T:Yi.RBAC.Application.Contracts.Setting.IConfigService">
<summary>
Config服务抽象

View File

@@ -0,0 +1,24 @@
using Mapster;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.RBAC.Domain.Shared.Identity.EnumClasses;
namespace Yi.RBAC.Application.Contracts.Identity.Dtos.User
{
public class ProfileUpdateInputVo
{
public string? Name { get; set; }
public int? Age { get; set; }
public string? Icon { get; set; }
public string? Nick { get; set; }
public string? Email { get; set; }
public string? Address { get; set; }
public long? Phone { get; set; }
public string? Introduction { get; set; }
public string? Remark { get; set; }
public SexEnum? Sex { get; set; }
}
}

View File

@@ -1,24 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Ddd.Dtos;
using Yi.RBAC.Domain.Shared.Logs;
namespace Yi.RBAC.Application.Contracts.Logs.Dtos
{
public class OperationLogGetListInputVo : PagedAllResultRequestDto
{
public string? Title { get; set; }
public OperEnum OperType { get; set; }
public string? RequestMethod { get; set; }
public string? OperUser { get; set; }
public string? OperIp { get; set; }
public string? OperLocation { get; set; }
public string? Method { get; set; }
public string? RequestParam { get; set; }
public string? RequestResult { get; set; }
public DateTime CreationTime { get; set; }
}
}

View File

@@ -1,25 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Ddd.Dtos;
using Yi.RBAC.Domain.Shared.Logs;
namespace Yi.RBAC.Application.Contracts.Logs.Dtos
{
public class OperationLogGetListOutputDto : IEntityDto<long>
{
public long Id { get; set; }
public string? Title { get; set; }
public OperEnum OperType { get; set; }
public string? RequestMethod { get; set; }
public string? OperUser { get; set; }
public string? OperIp { get; set; }
public string? OperLocation { get; set; }
public string? Method { get; set; }
public string? RequestParam { get; set; }
public string? RequestResult { get; set; }
public DateTime CreationTime { get; set; }
}
}

View File

@@ -1,18 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.RBAC.Application.Contracts.Logs.Dtos;
using Yi.Framework.Ddd.Services.Abstract;
namespace Yi.RBAC.Application.Contracts.Logs
{
/// <summary>
/// OperationLog服务抽象
/// </summary>
public interface IOperationLogService : ICrudAppService<OperationLogGetListOutputDto, long, OperationLogGetListInputVo >
{
}
}

View File

@@ -7,10 +7,20 @@ using Yi.Framework.Ddd.Dtos;
namespace Yi.RBAC.Application.Contracts.Setting.Dtos
{
/// <summary>
/// <20><><EFBFBD>ò<EFBFBD>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD>
/// </summary>
public class ConfigGetListInputVo : PagedAllResultRequestDto
{
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
public string? ConfigName { get; set; }
/// <summary>
/// <20><><EFBFBD>ü<EFBFBD>
/// </summary>
public string? ConfigKey { get; set; }
public DateTime CreationTime { get; set; }
}
}

View File

@@ -10,12 +10,36 @@ namespace Yi.RBAC.Application.Contracts.Setting.Dtos
public class ConfigGetListOutputDto : IEntityDto<long>
{
public long Id { get; set; }
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
public string ConfigName { get; set; } = string.Empty;
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
public string ConfigKey { get; set; } = string.Empty;
/// <summary>
/// <20><><EFBFBD><EFBFBD>ֵ
/// </summary>
public string ConfigValue { get; set; } = string.Empty;
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
public string? ConfigType { get; set; }
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD>ֶ<EFBFBD>
/// </summary>
public int OrderNum { get; set; }
/// <summary>
/// <20><>ע
/// </summary>
public string? Remark { get; set; }
/// <summary>
/// <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
/// </summary>
public DateTime CreationTime { get; set; }
}
}

View File

@@ -171,6 +171,13 @@
<param name="input"></param>
<returns></returns>
</member>
<member name="M:Yi.RBAC.Application.Identity.UserService.UpdateProfileAsync(Yi.RBAC.Application.Contracts.Identity.Dtos.User.ProfileUpdateInputVo)">
<summary>
更新个人中心
</summary>
<param name="input"></param>
<returns></returns>
</member>
<member name="M:Yi.RBAC.Application.Identity.UserService.UpdateStateAsync(System.Int64,System.Boolean)">
<summary>
更新状态
@@ -179,15 +186,17 @@
<param name="state"></param>
<returns></returns>
</member>
<member name="T:Yi.RBAC.Application.Logs.OperationLogService">
<summary>
OperationLog服务实现
</summary>
</member>
<member name="T:Yi.RBAC.Application.Setting.ConfigService">
<summary>
Config服务实现
</summary>
</member>
<member name="M:Yi.RBAC.Application.Setting.ConfigService.GetListAsync(Yi.RBAC.Application.Contracts.Setting.Dtos.ConfigGetListInputVo)">
<summary>
多查
</summary>
<param name="input"></param>
<returns></returns>
</member>
</members>
</doc>

View File

@@ -12,6 +12,8 @@ using SqlSugar;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Yi.Framework.Auth.JwtBearer.Authorization;
using Yi.RBAC.Application.Contracts.Identity.Dtos.User;
using Yi.Framework.Core.CurrentUsers;
namespace Yi.RBAC.Application.Identity
{
@@ -31,6 +33,9 @@ namespace Yi.RBAC.Application.Identity
[Autowired]
private IUserRepository _userRepository { get; set; }
[Autowired]
private ICurrentUser _currentUser { get; set; }
/// <summary>
/// 查询用户
/// </summary>
@@ -135,6 +140,20 @@ namespace Yi.RBAC.Application.Identity
return await MapToGetOutputDtoAsync(entity);
}
/// <summary>
/// 更新个人中心
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task<UserGetOutputDto> UpdateProfileAsync(ProfileUpdateInputVo input)
{
var entity = await _repository.GetByIdAsync(_currentUser.Id);
_mapper.Map(input, entity);
await _repository.UpdateAsync(entity);
var dto = _mapper.Map<UserGetOutputDto>(entity);
return dto;
}
/// <summary>
/// 更新状态
/// </summary>
@@ -142,7 +161,7 @@ namespace Yi.RBAC.Application.Identity
/// <param name="state"></param>
/// <returns></returns>
[Route("/api/user/{id}/{state}")]
public async Task<UserGetOutputDto> UpdateStateAsync([FromRoute] long id,[FromRoute] bool state)
public async Task<UserGetOutputDto> UpdateStateAsync([FromRoute] long id, [FromRoute] bool state)
{
var entity = await _repository.GetByIdAsync(id);
if (entity is null)

View File

@@ -1,23 +0,0 @@
using Yi.RBAC.Application.Contracts.Logs;
using NET.AutoWebApi.Setting;
using Yi.RBAC.Application.Contracts.Logs.Dtos;
using Yi.RBAC.Domain.Logs.Entities;
using Yi.Framework.Ddd.Services;
using Yi.Framework.Model.RABC.Entitys;
using Yi.Framework.Ddd.Dtos;
namespace Yi.RBAC.Application.Logs
{
/// <summary>
/// OperationLog服务实现
/// </summary>
[AppService]
public class OperationLogService : CrudAppService<OperationLogEntity, OperationLogGetListOutputDto, long, OperationLogGetListInputVo >,
IOperationLogService, IAutoApiService
{
public override Task<PagedResultDto<OperationLogGetListOutputDto>> GetListAsync(OperationLogGetListInputVo input)
{
return base.GetListAsync(input);
}
}
}

View File

@@ -19,6 +19,11 @@ namespace Yi.RBAC.Application.Setting
public class ConfigService : CrudAppService<ConfigEntity, ConfigGetOutputDto, ConfigGetListOutputDto, long, ConfigGetListInputVo, ConfigCreateInputVo, ConfigUpdateInputVo>,
IConfigService, IAutoApiService
{
/// <summary>
/// 多查
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public override async Task<PagedResultDto<ConfigGetListOutputDto>> GetListAsync(ConfigGetListInputVo input)
{
var entity = await MapToEntityAsync(input);

View File

@@ -1,21 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.RBAC.Domain.Shared.Logs
{
public enum OperEnum
{
Insert = 1,
Update = 2,
Delete = 3,
Auth = 4,
Export = 5,
Import = 6,
ForcedOut = 7,
GenerateCode = 8,
ClearData = 9
}
}

View File

@@ -1,38 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.RBAC.Domain.Shared.Logs
{
[AttributeUsage(AttributeTargets.Method)]
public class OperLogAttribute : System.Attribute
{
/// <summary>
/// 操作类型
/// </summary>
public OperEnum OperType { get; set; }
/// <summary>
/// 日志标题(模块)
/// </summary>
public string Title { get; set; }
/// <summary>
/// 是否保存请求数据
/// </summary>
public bool IsSaveRequestData { get; set; } = true;
/// <summary>
/// 是否保存返回数据
/// </summary>
public bool IsSaveResponseData { get; set; } = true;
public OperLogAttribute(string title, OperEnum operationType)
{
this.Title = title;
this.OperType = operationType;
}
}
}

View File

@@ -0,0 +1,68 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Data.DataSeeds;
using Yi.Framework.Ddd.Repositories;
using Yi.RBAC.Domain.Identity.Entities;
using Yi.RBAC.Domain.Shared.Identity.EnumClasses;
namespace Yi.RBAC.Domain.DataSeeds
{
[AppService(typeof(IDataSeed))]
public class UserDataSeed : AbstractDataSeed<UserEntity>
{
public UserDataSeed(IRepository<UserEntity> repository) : base(repository)
{
}
public override List<UserEntity> GetSeedData()
{
var entities=new List<UserEntity>();
UserEntity user1 = new UserEntity()
{
Id = SnowFlakeSingle.Instance.NextId(),
Name = "大橙子",
UserName = "cc",
Nick = "橙子",
Password = "123456",
Email = "454313500@qq.com",
Phone = 13800000000,
Sex = SexEnum.Male,
Address = "深圳",
Age = 20,
Introduction = "还有谁?",
OrderNum = 999,
Remark = "描述是什么呢?",
State = true
};
user1.BuildPassword();
entities.Add(user1);
UserEntity user2 = new UserEntity()
{
Id = SnowFlakeSingle.Instance.NextId(),
Name = "大测试",
UserName = "test",
Nick = "测试",
Password = "123456",
Email = "454313500@qq.com",
Phone = 15900000000,
Sex = SexEnum.Woman,
Address = "深圳",
Age = 18,
Introduction = "还有我!",
OrderNum = 1,
Remark = "我没有描述!",
State = true
};
user2.BuildPassword();
entities.Add(user2);
return entities;
}
}
}

View File

@@ -790,55 +790,5 @@
描述
</summary>
</member>
<member name="T:Yi.Framework.Model.RABC.Entitys.OperationLogEntity">
<summary>
操作日志表
</summary>
</member>
<member name="P:Yi.Framework.Model.RABC.Entitys.OperationLogEntity.Title">
<summary>
操作模块
</summary>
</member>
<member name="P:Yi.Framework.Model.RABC.Entitys.OperationLogEntity.OperType">
<summary>
操作类型
</summary>
</member>
<member name="P:Yi.Framework.Model.RABC.Entitys.OperationLogEntity.RequestMethod">
<summary>
请求方法
</summary>
</member>
<member name="P:Yi.Framework.Model.RABC.Entitys.OperationLogEntity.OperUser">
<summary>
操作人员
</summary>
</member>
<member name="P:Yi.Framework.Model.RABC.Entitys.OperationLogEntity.OperIp">
<summary>
操作Ip
</summary>
</member>
<member name="P:Yi.Framework.Model.RABC.Entitys.OperationLogEntity.OperLocation">
<summary>
操作地点
</summary>
</member>
<member name="P:Yi.Framework.Model.RABC.Entitys.OperationLogEntity.Method">
<summary>
操作方法
</summary>
</member>
<member name="P:Yi.Framework.Model.RABC.Entitys.OperationLogEntity.RequestParam">
<summary>
请求参数
</summary>
</member>
<member name="P:Yi.Framework.Model.RABC.Entitys.OperationLogEntity.RequestResult">
<summary>
请求结果
</summary>
</member>
</members>
</doc>

View File

@@ -1,70 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
using SqlSugar;
using Yi.Framework.Data.Auditing;
using Yi.Framework.Ddd.Entities;
using Yi.RBAC.Domain.Shared.Logs;
namespace Yi.Framework.Model.RABC.Entitys
{
/// <summary>
/// 操作日志表
///</summary>
[SugarTable("OperationLog")]
public class OperationLogEntity : IEntity<long>, ICreationAuditedObject
{
[SugarColumn(ColumnName = "Id", IsPrimaryKey = true)]
public long Id { get; set; }
/// <summary>
/// 操作模块
///</summary>
[SugarColumn(ColumnName = "Title")]
public string? Title { get; set; }
/// <summary>
/// 操作类型
///</summary>
[SugarColumn(ColumnName = "OperType")]
public OperEnum OperType { get; set; }
/// <summary>
/// 请求方法
///</summary>
[SugarColumn(ColumnName = "RequestMethod")]
public string? RequestMethod { get; set; }
/// <summary>
/// 操作人员
///</summary>
[SugarColumn(ColumnName = "OperUser")]
public string? OperUser { get; set; }
/// <summary>
/// 操作Ip
///</summary>
[SugarColumn(ColumnName = "OperIp")]
public string? OperIp { get; set; }
/// <summary>
/// 操作地点
///</summary>
[SugarColumn(ColumnName = "OperLocation")]
public string? OperLocation { get; set; }
/// <summary>
/// 操作方法
///</summary>
[SugarColumn(ColumnName = "Method")]
public string? Method { get; set; }
/// <summary>
/// 请求参数
///</summary>
[SugarColumn(ColumnName = "RequestParam")]
public string? RequestParam { get; set; }
/// <summary>
/// 请求结果
///</summary>
[SugarColumn(ColumnName = "RequestResult")]
public string? RequestResult { get; set; }
public DateTime CreationTime { get; set; }
public long? CreatorId { get; set; }
}
}

View File

@@ -1,99 +0,0 @@
using IPTools.Core;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.AspNetCore.Extensions;
using Yi.Framework.Core.CurrentUsers;
using Yi.Framework.Ddd.Repositories;
using Yi.Framework.Model.RABC.Entitys;
using Yi.RBAC.Domain.Shared.Logs;
namespace Yi.RBAC.Domain.Logs
{
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

@@ -12,12 +12,12 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Hei.Captcha" Version="0.3.0" />
<PackageReference Include="IPTools.China" Version="1.6.0" />
<PackageReference Include="UAParser" Version="3.1.47" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\framework\Yi.Framework.Data\Yi.Framework.Data.csproj" />
<ProjectReference Include="..\..\..\module\Yi.Framework.EventBus\Yi.Framework.EventBus.csproj" />
<ProjectReference Include="..\..\..\module\Yi.Framework.OperLog\Yi.Framework.OperLog.csproj" />
<ProjectReference Include="..\..\..\module\Yi.Framework.ThumbnailSharp\Yi.Framework.ThumbnailSharp.csproj" />
<ProjectReference Include="..\Yi.RBAC.Domain.Shared\Yi.RBAC.Domain.Shared.csproj" />
</ItemGroup>

View File

@@ -10,6 +10,7 @@ using System.Threading.Tasks;
using Yi.Framework.Core.Attributes;
using Yi.Framework.Data;
using Yi.Framework.EventBus;
using Yi.Framework.OperLog;
using Yi.Framework.ThumbnailSharp;
using Yi.RBAC.Domain.Logs;
using Yi.RBAC.Domain.Shared;
@@ -20,7 +21,8 @@ namespace Yi.RBAC.Domain
typeof(YiRBACDomainSharedModule),
typeof(YiFrameworkDataModule),
typeof(YiFrameworkThumbnailSharpModule),
typeof(YiFrameworkEventBusModule)
typeof(YiFrameworkEventBusModule),
typeof(YiFrameworkOperLogModule)
)]
public class YiRBACDomainModule : IStartupModule
{
@@ -31,10 +33,7 @@ namespace Yi.RBAC.Domain
public void ConfigureServices(IServiceCollection services, ConfigureServicesContext context)
{
services.AddHeiCaptcha();
services.AddControllers(options => {
options.Filters.Add<GlobalOperLogAttribute>();
});
services.AddSingleton<GlobalOperLogAttribute>();
}
}
}

View File

@@ -7,6 +7,7 @@ using Yi.Framework.Core;
using Yi.Framework.Core.Attributes;
using Yi.Framework.Core.Autofac;
using Yi.Framework.Data.Json;
using Yi.Framework.OperLog;
using Yi.RBAC.Application;
using Yi.RBAC.Sqlsugar;
@@ -32,6 +33,7 @@ namespace Yi.RBAC.Web
{
//NETServiceTest所在程序集添加进动态api配置
opt.CreateConventional(typeof(YiRBACApplicationModule).Assembly, option => option.RootPath = string.Empty);
opt.CreateConventional(typeof(YiFrameworkOperLogModule).Assembly, option => option.RootPath = string.Empty);
});
//添加swagger