feat: 搭建双token

fix: 修复签到记录问题
This commit is contained in:
橙子
2024-01-23 23:35:38 +08:00
parent e0fa97f7a2
commit c18334002c
7 changed files with 87 additions and 32 deletions

View File

@@ -9,6 +9,7 @@ using Volo.Abp.Application.Services;
using Volo.Abp.Users;
using Yi.Framework.Bbs.Application.Contracts.Dtos.Integral;
using Yi.Framework.Bbs.Domain.Managers;
using Yi.Framework.Rbac.Domain.Authorization;
namespace Yi.Framework.Bbs.Application.Services.Integral
{
@@ -42,33 +43,34 @@ namespace Yi.Framework.Bbs.Application.Services.Integral
[HttpGet("integral/sign-in/record")]
public async Task<SignInDto> GetSignInRecordAsync()
{
var output = new SignInDto();
DateTime lastMonth = DateTime.Now.AddMonths(-1);
DateTime lastDayOfMonth = new DateTime(lastMonth.Year, lastMonth.Month, 1).AddMonths(1).AddDays(-1);
DateTime startOfLastDay = new DateTime(lastDayOfMonth.Year, lastDayOfMonth.Month, lastDayOfMonth.Day, 0, 0, 0);
var output = new SignInDto();
DateTime lastMonth = DateTime.Now.AddMonths(-1);
DateTime lastDayOfMonth = new DateTime(lastMonth.Year, lastMonth.Month, 1).AddMonths(1).AddDays(-1);
DateTime startOfLastDay = new DateTime(lastDayOfMonth.Year, lastDayOfMonth.Month, lastDayOfMonth.Day, 0, 0, 0);
//获取当前用户本月的数据+上个月最后一天的数据
var entities = await _integralManager._signInRepository.GetListAsync(x => x.CreatorId == CurrentUser.Id
&& x.CreationTime >= startOfLastDay);
//获取当前用户本月的数据+上个月最后一天的数据
var entities = await _integralManager._signInRepository.GetListAsync(x => x.CreatorId == CurrentUser.Id
&& x.CreationTime >= startOfLastDay);
if (entities is null)
{
//返回默认值
if (entities.Count() == 0)
{
//返回默认值
return output;
}
//拿到最末尾的数据
var lastEntity = entities.OrderBy(x => x.CreationTime).LastOrDefault();
//判断当前时间和最后时间是否为连续的
if (lastEntity.CreationTime.Day >= DateTime.Now.AddDays(-1).Day)
{
output.CurrentContinuousNumber = lastEntity.ContinuousNumber;
}
//去除上个月查询的数据
output.SignInItem = entities.Where(x => x.CreationTime.Month == DateTime.Now.Month).Select(x => new SignInItemDto { Id = x.Id, CreationTime = x.CreationTime }).OrderBy(x => x.CreationTime).ToList();
return output;
}
//拿到最末尾的数据
var lastEntity = entities.OrderBy(x => x.CreationTime).LastOrDefault();
//判断当前时间和最后时间是否为连续的
if (lastEntity.CreationTime.Day >= DateTime.Now.AddDays(-1).Day)
{
output.CurrentContinuousNumber = lastEntity.ContinuousNumber;
}
//去除上个月查询的数据
output.SignInItem = entities.Where(x=>x.CreationTime.Month==DateTime.Now.Month) .Select(x => new SignInItemDto { Id = x.Id, CreationTime = x.CreationTime }).OrderBy(x=>x.CreationTime).ToList();
return output;
}
}

View File

@@ -12,6 +12,11 @@ namespace Yi.Framework.Rbac.Domain.Shared.Options
public string Audience { get; set; }
public string RefreshIssuer { get; set; }
public string RefreshAudience { get; set; }
public string SecurityKey { get; set; }
public long ExpiresMinuteTime { get; set; }

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Rbac.Domain.Shared.Options
{
/// <summary>
/// 刷新token
/// </summary>
public class RefreshJwtOptions: JwtOptions
{
}
}

View File

@@ -16,7 +16,7 @@ namespace Yi.Framework.Rbac.Domain.Shared
{
var configuration = context.Services.GetConfiguration();
Configure<JwtOptions>(configuration.GetSection(nameof(JwtOptions)));
Configure<RefreshJwtOptions>(configuration.GetSection(nameof(RefreshJwtOptions)));
Configure<RbacOptions>(configuration.GetSection(nameof(RbacOptions)));
}
}

View File

@@ -3,14 +3,11 @@ using System.Security.Claims;
using System.Text;
using Mapster;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json;
using TencentCloud.Tdmq.V20200217.Models;
using Volo.Abp;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Domain.Services;
using Volo.Abp.EventBus.Local;
using Volo.Abp.Security.Claims;
@@ -38,11 +35,13 @@ namespace Yi.Framework.Rbac.Domain.Managers
private IHttpContextAccessor _httpContextAccessor;
private UserManager _userManager;
private ISqlSugarRepository<RoleEntity> _roleRepository;
private RefreshJwtOptions _refreshJwtOptions;
public AccountManager(IUserRepository repository
, IHttpContextAccessor httpContextAccessor
, IOptions<JwtOptions> jwtOptions
, ILocalEventBus localEventBus
, UserManager userManager
,IOptions<RefreshJwtOptions> refreshJwtOptions
, ISqlSugarRepository<RoleEntity> roleRepository)
{
_repository = repository;
@@ -51,6 +50,7 @@ namespace Yi.Framework.Rbac.Domain.Managers
_localEventBus = localEventBus;
_userManager = userManager;
_roleRepository = roleRepository;
_refreshJwtOptions= refreshJwtOptions.Value;
}
/// <summary>
@@ -86,6 +86,7 @@ namespace Yi.Framework.Rbac.Domain.Managers
//将用户信息添加到缓存中,需要考虑的是更改了用户、角色、菜单等整个体系都需要将缓存进行刷新,看具体业务进行选择
var accessToken = CreateToken(this.UserInfoToClaim(userInfo));
return accessToken;
}
@@ -111,7 +112,23 @@ namespace Yi.Framework.Rbac.Domain.Managers
return returnToken;
}
private string CreateRefreshToken()
{
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_refreshJwtOptions.SecurityKey));
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
var claims =new List<Claim> { new Claim("Refresh", "true") } ;
var token = new JwtSecurityToken(
issuer: _refreshJwtOptions.Issuer,
audience: _refreshJwtOptions.Audience,
claims: claims,
expires: DateTime.Now.AddMinutes(_refreshJwtOptions.ExpiresMinuteTime),
notBefore: DateTime.Now,
signingCredentials: creds);
string returnToken = new JwtSecurityTokenHandler().WriteToken(token);
return returnToken;
}
/// <summary>
/// 登录效验
/// </summary>