V 1.1.0
新增数据信息 完善数据信息得权限问题
This commit is contained in:
parent
647f2b75c9
commit
aaebbb9104
@ -8,6 +8,16 @@ public enum OptionTypeEnum
|
||||
Boolean = 4
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// option的分类
|
||||
/// </summary>
|
||||
public enum OptionCategory
|
||||
{
|
||||
System = 1,
|
||||
LaiTool = 2,
|
||||
NanFengAI = 3,
|
||||
}
|
||||
|
||||
public static class OptionKeyName
|
||||
{
|
||||
/// <summary>
|
||||
|
||||
@ -5,6 +5,7 @@ using LMS.Repository.Models.DB;
|
||||
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
||||
//using Newtonsoft.Json;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace LMS.DAO
|
||||
@ -69,6 +70,17 @@ namespace LMS.DAO
|
||||
);
|
||||
modelBuilder.Entity<UserSoftware>()
|
||||
.HasKey(us => new { us.UserId, us.SoftwareId });
|
||||
|
||||
// Options表的RoleIds字段使用JSON格式存储
|
||||
modelBuilder.Entity<Options>(entity =>
|
||||
{
|
||||
entity.Property(e => e.RoleIds)
|
||||
.HasConversion(
|
||||
v => Newtonsoft.Json.JsonConvert.SerializeObject(v), // 将 List<int> 序列化为 JSON 字符串
|
||||
v => Newtonsoft.Json.JsonConvert.DeserializeObject<List<long>>(v ?? "[]") ?? new List<long>() // 反序列化
|
||||
)
|
||||
.HasColumnType("json"); // 指定MySQL字段类型为JSON
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,11 +1,14 @@
|
||||
using LMS.Repository.Models.DB;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace LMS.DAO.UserDAO
|
||||
{
|
||||
public class UserBasicDao(UserManager<User> userManager)
|
||||
public class UserBasicDao(UserManager<User> userManager, ApplicationDbContext dbContext)
|
||||
{
|
||||
private readonly UserManager<User> _userManager = userManager;
|
||||
private readonly ApplicationDbContext _dbContext = dbContext;
|
||||
|
||||
/// <summary>
|
||||
/// 检查用户是否存在,通过用户ID
|
||||
/// </summary>
|
||||
@ -120,6 +123,27 @@ namespace LMS.DAO.UserDAO
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取用户的所有角色ID
|
||||
/// </summary>
|
||||
/// <param name="userId"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="Exception"></exception>
|
||||
public async Task<List<long>> GetUserRoleIds(long userId)
|
||||
{
|
||||
// 查找用户,若不存在直接抛异常
|
||||
User user = await _userManager.FindByIdAsync(userId.ToString())
|
||||
?? throw new Exception("用户不存在");
|
||||
|
||||
// 直接查询 UserRoles 表获取所有关联的 RoleId
|
||||
var roleIds = await _dbContext.UserRoles
|
||||
.Where(ur => ur.UserId == user.Id)
|
||||
.Select(ur => ur.RoleId)
|
||||
.ToListAsync();
|
||||
|
||||
return roleIds;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -7,6 +7,7 @@ namespace LMS.Repository.DB;
|
||||
public class Options
|
||||
{
|
||||
[Key]
|
||||
[Required]
|
||||
public required string Key { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
@ -14,9 +15,17 @@ public class Options
|
||||
/// </summary>
|
||||
public string? Value { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
public OptionTypeEnum Type { get; set; } = OptionTypeEnum.String;
|
||||
|
||||
// 写一个字段,映射Value,判断是不是json字符串,是的话就解析成对象
|
||||
[Required]
|
||||
public OptionCategory Category { get; set; } = OptionCategory.System;
|
||||
|
||||
[Required]
|
||||
public List<long> RoleIds { get; set; } = [];
|
||||
|
||||
public DateTime CreatedTime { get; set; }
|
||||
|
||||
// 写一个字段,映射Value,判断是不是json字符串,是的话就解析成对象
|
||||
public T? GetValueObject<T>()
|
||||
{
|
||||
|
||||
21
LMS.Repository/DTO/OptionDto/OptionSimpleDto.cs
Normal file
21
LMS.Repository/DTO/OptionDto/OptionSimpleDto.cs
Normal file
@ -0,0 +1,21 @@
|
||||
using LMS.Common.Enums;
|
||||
|
||||
namespace LMS.Repository.DTO.OptionDto
|
||||
{
|
||||
public class OptionSimpleDto
|
||||
{
|
||||
public required string Key { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Value of the option,这个值是一个json字符串
|
||||
/// </summary>
|
||||
public string? Value { get; set; } = string.Empty;
|
||||
|
||||
|
||||
public OptionTypeEnum Type { get; set; } = OptionTypeEnum.String;
|
||||
|
||||
public OptionCategory Category { get; set; } = OptionCategory.System;
|
||||
|
||||
public List<string> RoleNames { get; set; } = [];
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,6 @@
|
||||
using LMS.Common.Enums;
|
||||
|
||||
namespace LMS.Repository.DTO;
|
||||
namespace LMS.Repository.DTO.OptionDto;
|
||||
|
||||
public class OptionsDto
|
||||
{
|
||||
26
LMS.Repository/Options/AddOptionModel .cs
Normal file
26
LMS.Repository/Options/AddOptionModel .cs
Normal file
@ -0,0 +1,26 @@
|
||||
|
||||
using LMS.Common.Enums;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace LMS.Repository.Options;
|
||||
|
||||
public class AddOptionModel
|
||||
{
|
||||
|
||||
[Required]
|
||||
public required string Key { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Value of the option,这个值是一个json字符串
|
||||
/// </summary>
|
||||
public string? Value { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
public OptionTypeEnum Type { get; set; } = OptionTypeEnum.String;
|
||||
|
||||
[Required]
|
||||
public OptionCategory Category { get; set; } = OptionCategory.System;
|
||||
|
||||
[Required]
|
||||
public List<string> RoleNames { get; set; }
|
||||
}
|
||||
20
LMS.Repository/Options/ModifyOptionByKeyModel.cs
Normal file
20
LMS.Repository/Options/ModifyOptionByKeyModel.cs
Normal file
@ -0,0 +1,20 @@
|
||||
|
||||
using LMS.Common.Enums;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace LMS.Repository.Options;
|
||||
|
||||
public class ModifyOptionByKeyModel
|
||||
{
|
||||
/// <summary>
|
||||
/// Value of the option,这个值是一个json字符串
|
||||
/// </summary>
|
||||
|
||||
public required string Value { get; set; } = string.Empty;
|
||||
|
||||
public required OptionTypeEnum Type { get; set; } = OptionTypeEnum.String;
|
||||
|
||||
public required OptionCategory Category { get; set; } = OptionCategory.System;
|
||||
[Required]
|
||||
public required List<string> RoleNames { get; set; } = [];
|
||||
}
|
||||
@ -1,6 +1,7 @@
|
||||
using AutoMapper;
|
||||
using LMS.Repository.DB;
|
||||
using LMS.Repository.DTO;
|
||||
using LMS.Repository.DTO.OptionDto;
|
||||
using LMS.Repository.DTO.PromptDto;
|
||||
using LMS.Repository.DTO.PromptTypeDto;
|
||||
using LMS.Repository.DTO.UserDto;
|
||||
|
||||
@ -1,11 +1,13 @@
|
||||
using LMS.Common.Extensions;
|
||||
using LMS.Repository.DB;
|
||||
using LMS.Repository.DTO;
|
||||
using LMS.Repository.DTO.OptionDto;
|
||||
using LMS.Repository.Models.DB;
|
||||
using LMS.Repository.Options;
|
||||
using LMS.service.Service;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using static LMS.Common.Enums.ResponseCodeEnum;
|
||||
|
||||
namespace LMS.service.Controllers
|
||||
@ -29,6 +31,7 @@ namespace LMS.service.Controllers
|
||||
[HttpGet("{optionsKey}")]
|
||||
public async Task<ActionResult<APIResponseModel<List<OptionsDto>>>> GetSimpleOptions(string optionsKey)
|
||||
{
|
||||
long userId = ConvertExtension.ObjectToLong(HttpContext.Items["UserId"] ?? 0);
|
||||
return await _optionsService.GetSimpleOptions(optionsKey);
|
||||
}
|
||||
|
||||
@ -73,5 +76,113 @@ namespace LMS.service.Controllers
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Laitool 的配置项控制器
|
||||
/// </summary>
|
||||
/// <param name="optionsService"></param>
|
||||
[Route("lms/[controller]/[action]")]
|
||||
[ApiController]
|
||||
public class OptionsController(OptionsService optionsService) : ControllerBase
|
||||
{
|
||||
private readonly OptionsService _optionsService = optionsService;
|
||||
|
||||
#region 获取指定的配置项
|
||||
/// <summary>
|
||||
/// 获取简单的配置项,无需权限
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("{userId}/{category}/{optionsKey}")]
|
||||
public async Task<ActionResult<APIResponseModel<OptionsDto>>> GetOptionsByKey(long userId, int category, string optionsKey)
|
||||
{
|
||||
return await _optionsService.GetOptionsByKey(userId, category, optionsKey);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 获取指定的配置项,全部
|
||||
/// <summary>
|
||||
/// 获取简单的配置项,无需权限
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("{category}/{optionsKey}")]
|
||||
[Authorize]
|
||||
public async Task<ActionResult<APIResponseModel<OptionSimpleDto>>> GetAllMessageOptionsByKey(int category, string optionsKey)
|
||||
{
|
||||
long userId = ConvertExtension.ObjectToLong(HttpContext.Items["UserId"] ?? 0);
|
||||
return await _optionsService.GetAllMessageOptionsByKey(category, optionsKey, userId);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region 获取指定条件的数据信息集合
|
||||
/// <summary>
|
||||
/// 获取简单的配置项,无需权限
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
[Authorize]
|
||||
public async Task<ActionResult<APIResponseModel<CollectionResponse<OptionSimpleDto>>>> QueryOptionCollection([Required] int page, [Required] int pageSize, string? key, int? type, int? category, [FromQuery] string[]? roleNames)
|
||||
{
|
||||
|
||||
long userId = ConvertExtension.ObjectToLong(HttpContext.Items["UserId"] ?? 0);
|
||||
return await _optionsService.QueryOptionCollection(page, pageSize, key, type, category, roleNames, userId);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 修改数据配置项
|
||||
|
||||
[HttpPost("{key}")]
|
||||
[Authorize]
|
||||
public async Task<ActionResult<APIResponseModel<string>>> ModifyOptionsByKey(string key, [FromBody] ModifyOptionByKeyModel model)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.ParameterError);
|
||||
}
|
||||
long userId = ConvertExtension.ObjectToLong(HttpContext.Items["UserId"] ?? 0);
|
||||
return await _optionsService.ModifyOptionsByKey(key, model, userId);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 新增指定的数据项
|
||||
|
||||
[HttpPost]
|
||||
[Authorize]
|
||||
public async Task<ActionResult<APIResponseModel<string>>> AddOptions([FromBody] AddOptionModel option)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.ParameterError);
|
||||
}
|
||||
long userId = ConvertExtension.ObjectToLong(HttpContext.Items["UserId"] ?? 0);
|
||||
return await _optionsService.AddOptions(option, userId);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 删除指定的key的数据
|
||||
|
||||
[HttpDelete("{category}/{key}")]
|
||||
[Authorize]
|
||||
public async Task<ActionResult<APIResponseModel<string>>> DeleteOptionsByKey(int category, string key)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.ParameterError);
|
||||
}
|
||||
long userId = ConvertExtension.ObjectToLong(HttpContext.Items["UserId"] ?? 0);
|
||||
return await _optionsService.DeleteOptionsByKey(category, key, userId);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@ -13,6 +13,7 @@
|
||||
<PackageReference Include="AspNetCoreRateLimit" Version="5.0.0" />
|
||||
<PackageReference Include="AutoMapper" Version="14.0.0" />
|
||||
<PackageReference Include="Betalgo.Ranul.OpenAI" Version="8.9.0" />
|
||||
<PackageReference Include="LinqKit" Version="1.3.8" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.8" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.8" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.8">
|
||||
@ -35,6 +36,7 @@
|
||||
<PackageReference Include="Serilog.Settings.Configuration" Version="9.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.6.4" />
|
||||
<PackageReference Include="System.Runtime" Version="4.3.1" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
using AutoMapper;
|
||||
using LMS.Common.Dictionary;
|
||||
using LMS.Common.Enums;
|
||||
using LMS.Common.Templates;
|
||||
using LMS.DAO;
|
||||
using LMS.DAO.UserDAO;
|
||||
@ -11,14 +12,22 @@ using LMS.service.Extensions.Mail;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System.Linq;
|
||||
using static LMS.Common.Enums.ResponseCodeEnum;
|
||||
using Options = LMS.Repository.DB.Options;
|
||||
using System.Linq.Dynamic.Core;
|
||||
using LinqKit;
|
||||
using LMS.Repository.DTO.OptionDto;
|
||||
using LMS.Common.Extensions;
|
||||
|
||||
namespace LMS.service.Service
|
||||
{
|
||||
public class OptionsService(ApplicationDbContext context, UserManager<User> userManager, IMapper mapper, UserBasicDao userBasicDao, EmailService emailService)
|
||||
public class OptionsService(ApplicationDbContext context, UserManager<User> userManager, RoleManager<Role> roleManager, IMapper mapper, UserBasicDao userBasicDao, EmailService emailService)
|
||||
{
|
||||
private readonly ApplicationDbContext _context = context;
|
||||
private readonly UserManager<User> _userManager = userManager;
|
||||
private readonly RoleManager<Role> _roleManager = roleManager;
|
||||
private readonly IMapper _mapper = mapper;
|
||||
private readonly UserBasicDao _userBasicDao = userBasicDao;
|
||||
private readonly EmailService _emailService = emailService;
|
||||
@ -129,12 +138,7 @@ namespace LMS.service.Service
|
||||
using var transaction = await _context.Database.BeginTransactionAsync();
|
||||
try
|
||||
{
|
||||
User? user = await _userManager.FindByIdAsync(userId.ToString());
|
||||
if (user == null)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.FindUserByIdFail);
|
||||
}
|
||||
bool isAdminOrSuperAdmin = await _userManager.IsInRoleAsync(user, "Admin") || await _userManager.IsInRoleAsync(user, "Super Admin");
|
||||
bool isAdminOrSuperAdmin = await _userBasicDao.CheckUserIsAdminOrSuperAdmin(userId);
|
||||
// 判断用户是不是管理员
|
||||
if (!isAdminOrSuperAdmin)
|
||||
{
|
||||
@ -200,5 +204,392 @@ namespace LMS.service.Service
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 获取指定的配置项
|
||||
/// <summary>
|
||||
/// 获取指定的配置项,需要判断权限
|
||||
/// </summary>
|
||||
/// <param name="userId"></param>
|
||||
/// <param name="category"></param>
|
||||
/// <param name="optionsKey"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<ActionResult<APIResponseModel<OptionsDto>>> GetOptionsByKey(long userId, int category, string optionsKey)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 判断分类是不是在对应的enum里面
|
||||
if (!Enum.IsDefined(typeof(OptionCategory), category))
|
||||
{
|
||||
return APIResponseModel<OptionsDto>.CreateErrorResponseModel(ResponseCode.ParameterError, "该分类不存在");
|
||||
}
|
||||
|
||||
Options? options = await _context.Options.FirstOrDefaultAsync(x => x.Key == optionsKey && (OptionCategory)category == x.Category) ?? throw new Exception("数据不存在");
|
||||
|
||||
List<long> roleIds = [];
|
||||
if (userId != 0)
|
||||
{
|
||||
roleIds = await _userBasicDao.GetUserRoleIds(userId);
|
||||
}
|
||||
|
||||
// 数据存在 对比权限
|
||||
if (options.RoleIds.Count == 0)
|
||||
{
|
||||
// 不需要权限 直接返回
|
||||
return APIResponseModel<OptionsDto>.CreateSuccessResponseModel(_mapper.Map<OptionsDto>(options));
|
||||
}
|
||||
else
|
||||
{
|
||||
// 需要判断权限 判断 roleIds 是不是有权限在 options.RoleIds 里面
|
||||
bool hasPermission = false;
|
||||
foreach (var roleId in roleIds)
|
||||
{
|
||||
if (options.RoleIds.Contains(roleId))
|
||||
{
|
||||
hasPermission = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!hasPermission)
|
||||
{
|
||||
return APIResponseModel<OptionsDto>.CreateErrorResponseModel(ResponseCode.NotPermissionAction);
|
||||
}
|
||||
else
|
||||
{
|
||||
return APIResponseModel<OptionsDto>.CreateSuccessResponseModel(_mapper.Map<OptionsDto>(options));
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return APIResponseModel<OptionsDto>.CreateErrorResponseModel(ResponseCode.SystemError, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 获取指定的数据项
|
||||
internal async Task<ActionResult<APIResponseModel<string>>> ModifyOptionsByKey(string key, ModifyOptionByKeyModel model, long userId)
|
||||
{
|
||||
try
|
||||
{
|
||||
bool isAdminOrSuperAdmin = await _userBasicDao.CheckUserIsAdminOrSuperAdmin(userId);
|
||||
// 判断type 和 category 是不是在enum中
|
||||
|
||||
// 判断分类是不是在对应的enum里面
|
||||
if (!Enum.IsDefined(typeof(OptionCategory), model.Category))
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.ParameterError, "该分类不存在");
|
||||
}
|
||||
|
||||
if (!Enum.IsDefined(typeof(OptionTypeEnum), model.Type))
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.ParameterError, "该类型不存在");
|
||||
}
|
||||
|
||||
// 判断用户是不是管理员
|
||||
if (!isAdminOrSuperAdmin)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.NotPermissionAction);
|
||||
}
|
||||
Options? options = await _context.Options.FirstOrDefaultAsync(x => x.Key == key);
|
||||
if (options == null)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.FindOptionsFail);
|
||||
}
|
||||
|
||||
List<long> ids = [];
|
||||
// 判断所有的roleId是不是都存在
|
||||
if (model.RoleNames != null && model.RoleNames.Count > 0)
|
||||
{
|
||||
foreach (var roleId in model.RoleNames)
|
||||
{
|
||||
Role? role = await _roleManager.FindByNameAsync(roleId.ToString());
|
||||
if (role == null)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.ParameterError, "角色列表中有部分ID不存在");
|
||||
}
|
||||
ids.Add(role.Id);
|
||||
}
|
||||
}
|
||||
|
||||
// 开始修改配置项
|
||||
options.Value = model.Value;
|
||||
options.RoleIds = ids;
|
||||
options.Type = model.Type;
|
||||
options.Category = model.Category;
|
||||
_context.Options.Update(options);
|
||||
await _context.SaveChangesAsync();
|
||||
return APIResponseModel<string>.CreateSuccessResponseModel("修改成功");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.SystemError, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 查询数据信息的集合
|
||||
|
||||
/// <summary>
|
||||
/// 查询数据信息的集合
|
||||
/// </summary>
|
||||
/// <param name="page"></param>
|
||||
/// <param name="pageSize"></param>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="category"></param>
|
||||
/// <param name="roleIds"></param>
|
||||
/// <param name="userId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<ActionResult<APIResponseModel<CollectionResponse<OptionSimpleDto>>>> QueryOptionCollection(int page, int pageSize, string? key, int? type, int? category, string[]? roleNames, long userId)
|
||||
{
|
||||
try
|
||||
{
|
||||
bool isAdminOrSuperAdmin = await _userBasicDao.CheckUserIsAdminOrSuperAdmin(userId);
|
||||
// 判断用户是不是管理员
|
||||
if (!isAdminOrSuperAdmin)
|
||||
{
|
||||
return APIResponseModel<CollectionResponse<OptionSimpleDto>>.CreateErrorResponseModel(ResponseCode.NotPermissionAction);
|
||||
}
|
||||
IQueryable<Options> query = _context.Options;
|
||||
if (!string.IsNullOrWhiteSpace(key))
|
||||
{
|
||||
query = query.Where(x => x.Key.Contains(key));
|
||||
}
|
||||
|
||||
if (type != null)
|
||||
{
|
||||
if (!Enum.IsDefined(typeof(OptionTypeEnum), type))
|
||||
{
|
||||
return APIResponseModel<CollectionResponse<OptionSimpleDto>>.CreateErrorResponseModel(ResponseCode.ParameterError, "该类型不存在");
|
||||
}
|
||||
query = query.Where(x => x.Type == (OptionTypeEnum)type);
|
||||
}
|
||||
|
||||
if (category != null)
|
||||
{
|
||||
if (!Enum.IsDefined(typeof(OptionCategory), category))
|
||||
{
|
||||
return APIResponseModel<CollectionResponse<OptionSimpleDto>>.CreateErrorResponseModel(ResponseCode.ParameterError, "该分类不存在");
|
||||
}
|
||||
query = query.Where(x => x.Category == (OptionCategory)category);
|
||||
}
|
||||
|
||||
List<long> ids = [];
|
||||
if (roleNames != null && roleNames.Length > 0)
|
||||
{
|
||||
// 判断所有的roleId是不是都存在
|
||||
foreach (var roleId in roleNames)
|
||||
{
|
||||
Role? role = await _roleManager.FindByNameAsync(roleId.ToString());
|
||||
if (role != null)
|
||||
{
|
||||
ids.Add(role.Id);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ids.Count > 0)
|
||||
{
|
||||
var predicate = PredicateBuilder.New<Options>(false);
|
||||
foreach (var roleId in ids)
|
||||
{
|
||||
var localId = roleId; // 闭包捕获
|
||||
predicate = predicate.Or(x => EF.Functions.JsonContains(x.RoleIds, localId.ToString()));
|
||||
// 如果你的 RoleIds 存数字,localId.ToString(),否则加引号
|
||||
// predicate = predicate.Or(x => EF.Functions.JsonContains(x.RoleIds, $"\"{localId}\""));
|
||||
}
|
||||
query = query.Where(predicate);
|
||||
}
|
||||
|
||||
// 查询总数
|
||||
int total = await query.CountAsync();
|
||||
|
||||
// 创建时间倒叙
|
||||
query = query.OrderByDescending(x => x.CreatedTime);
|
||||
|
||||
// 分页
|
||||
query = query.Skip((page - 1) * pageSize).Take(pageSize);
|
||||
|
||||
|
||||
|
||||
List<Options>? options = await query.ToListAsync();
|
||||
List<Role> roles = await _context.Roles.ToListAsync();
|
||||
List<OptionSimpleDto> optionDtos = new List<OptionSimpleDto>();
|
||||
foreach (var option in options)
|
||||
{
|
||||
OptionSimpleDto optionDto = new()
|
||||
{
|
||||
Key = option.Key,
|
||||
Value = option.Value,
|
||||
Type = option.Type,
|
||||
Category = option.Category,
|
||||
RoleNames = option.RoleIds.Select(x => roles.FirstOrDefault(r => r.Id == x)).Where(r => r != null).Select(r => r.Name).ToList()
|
||||
};
|
||||
optionDtos.Add(optionDto);
|
||||
}
|
||||
|
||||
|
||||
return APIResponseModel<CollectionResponse<OptionSimpleDto>>.CreateSuccessResponseModel(new CollectionResponse<OptionSimpleDto>
|
||||
{
|
||||
Total = total,
|
||||
Collection = optionDtos,
|
||||
Current = page
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return APIResponseModel<CollectionResponse<OptionSimpleDto>>.CreateErrorResponseModel(ResponseCode.SystemError, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 新增指定的数据项
|
||||
public async Task<ActionResult<APIResponseModel<string>>> AddOptions(AddOptionModel option, long userId)
|
||||
{
|
||||
try
|
||||
{
|
||||
bool isSuperAdmin = await _userBasicDao.CheckUserIsSuperAdmin(userId);
|
||||
|
||||
if (!isSuperAdmin)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.NotPermissionAction);
|
||||
}
|
||||
// 判断type 和 category 是不是在enum中
|
||||
|
||||
// 判断分类是不是在对应的enum里面
|
||||
if (!Enum.IsDefined(typeof(OptionCategory), option.Category))
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.ParameterError, "该分类不存在");
|
||||
}
|
||||
|
||||
if (!Enum.IsDefined(typeof(OptionTypeEnum), option.Type))
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.ParameterError, "该类型不存在");
|
||||
}
|
||||
|
||||
List<long> ids = [];
|
||||
// 判断所有的roleId是不是都存在
|
||||
if (option.RoleNames != null && option.RoleNames.Count > 0)
|
||||
{
|
||||
foreach (var roleId in option.RoleNames)
|
||||
{
|
||||
Role? role = await _roleManager.FindByNameAsync(roleId.ToString());
|
||||
if (role == null)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.ParameterError, "角色列表中有部分ID不存在");
|
||||
}
|
||||
ids.Add(role.Id);
|
||||
}
|
||||
}
|
||||
// 判断当前的key是不是已经存在
|
||||
Options? options = await _context.Options.FirstOrDefaultAsync(x => x.Key == option.Key && x.Category == option.Category);
|
||||
if (options != null)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.ParameterError, "当前分类的key已经存在");
|
||||
}
|
||||
if (string.IsNullOrWhiteSpace(option.Value))
|
||||
{
|
||||
option.Value = string.Empty;
|
||||
}
|
||||
|
||||
Options options1 = new()
|
||||
{
|
||||
Key = option.Key,
|
||||
Value = option.Value,
|
||||
Type = option.Type,
|
||||
Category = option.Category,
|
||||
RoleIds = ids,
|
||||
CreatedTime = BeijingTimeExtension.GetBeijingTime()
|
||||
};
|
||||
// 添加数据
|
||||
await _context.Options.AddAsync(options1);
|
||||
await _context.SaveChangesAsync();
|
||||
return APIResponseModel<string>.CreateSuccessResponseModel("添加成功");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.SystemError, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 删除指定的key的数据
|
||||
/// <summary>
|
||||
/// 删除指定的数据
|
||||
/// </summary>
|
||||
/// <param name="category"></param>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="userId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<ActionResult<APIResponseModel<string>>> DeleteOptionsByKey(int category, string key, long userId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var isSuperAdmin = await _userBasicDao.CheckUserIsSuperAdmin(userId);
|
||||
// 判断用户是不是管理员
|
||||
if (!isSuperAdmin)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.NotPermissionAction);
|
||||
}
|
||||
Options? options = await _context.Options.FirstOrDefaultAsync(x => x.Key == key && x.Category == (OptionCategory)category);
|
||||
if (options == null)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.FindOptionsFail);
|
||||
}
|
||||
// 删除数据
|
||||
_context.Options.Remove(options);
|
||||
await _context.SaveChangesAsync();
|
||||
return APIResponseModel<string>.CreateSuccessResponseModel("删除成功");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.SystemError, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 管理员获取完整数据
|
||||
internal async Task<ActionResult<APIResponseModel<OptionSimpleDto>>> GetAllMessageOptionsByKey(int category, string optionsKey, long userId)
|
||||
{
|
||||
try
|
||||
{
|
||||
bool isAdminOrSuperAdmin = await _userBasicDao.CheckUserIsAdminOrSuperAdmin(userId);
|
||||
// 判断用户是不是管理员
|
||||
if (!isAdminOrSuperAdmin)
|
||||
{
|
||||
return APIResponseModel<OptionSimpleDto>.CreateErrorResponseModel(ResponseCode.NotPermissionAction);
|
||||
}
|
||||
// 判断分类是不是在对应的enum里面
|
||||
if (!Enum.IsDefined(typeof(OptionCategory), category))
|
||||
{
|
||||
return APIResponseModel<OptionSimpleDto>.CreateErrorResponseModel(ResponseCode.ParameterError, "该分类不存在");
|
||||
}
|
||||
|
||||
Options? options = await _context.Options.FirstOrDefaultAsync(x => x.Key == optionsKey && (OptionCategory)category == x.Category) ?? throw new Exception("数据不存在");
|
||||
|
||||
OptionSimpleDto optionSimpleDto = new()
|
||||
{
|
||||
Key = options.Key,
|
||||
Value = options.Value,
|
||||
Type = options.Type,
|
||||
Category = options.Category,
|
||||
RoleNames = options.RoleIds.Select(x => _roleManager.Roles.FirstOrDefault(r => r.Id == x)).Where(r => r != null).Select(r => r.Name).ToList()
|
||||
};
|
||||
// 不需要权限 直接返回
|
||||
return APIResponseModel<OptionSimpleDto>.CreateSuccessResponseModel(optionSimpleDto);
|
||||
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return APIResponseModel<OptionSimpleDto>.CreateErrorResponseModel(ResponseCode.SystemError, ex.Message);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -63,9 +63,9 @@ namespace LMS.service.Service.UserService
|
||||
|
||||
|
||||
//设置Token的过期时间
|
||||
//DateTime expires = DateTime.Now.AddHours(hours);
|
||||
DateTime expires = DateTime.Now.AddHours(hours);
|
||||
// 设置过期时间为三分钟
|
||||
DateTime expires = DateTime.Now.AddMinutes(3);
|
||||
//DateTime expires = DateTime.Now.AddMinutes(3);
|
||||
byte[] secBytes = Encoding.UTF8.GetBytes(key);
|
||||
var secKey = new SymmetricSecurityKey(secBytes);
|
||||
|
||||
|
||||
@ -68,6 +68,6 @@
|
||||
}
|
||||
]
|
||||
},
|
||||
"Version": "1.0.9",
|
||||
"Version": "1.1.0",
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user